0

I would like to use a c++ library given by a company. I only have the .lib and the .h files. I would like to use it in unity. So I need a DLL. So I want to create a DLL that permit me to access the functions I need in the .lib .

To do so I created a visual studio c++ DLL project with a c++ file that call the desired functions from the library. The .lib is linked to visual studio in linker/input/additional dependences.

In order to make it simple I am trying to call fonctions of my DLL from a normal c++ file instead of unity.

My test file look like this :

#include <windows.h>
#include <iostream>

void CallMyDLL(void)
{
 HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\path\\museDLL.dll");
 FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"startConnection");
   typedef void (__stdcall * pICFUNC)();
   pICFUNC startConnection;
   startConnection = pICFUNC(lpfnGetProcessID);

  //std::cout << test<< std::endl;

  startConnection(); 

  FreeLibrary(hGetProcIDDLL);

}
int main () {
CallMyDLL();
}

and my main DLL file look like this :

#include "dllmain.h"  
#include <iostream>
#include <fstream>
#include "main_muse.h"
namespace museDLL
{
    MainPage* mp;
      void startConnection()
    {
           mp = new  MainPage();
    }

    double getValue()
    {   
        return mp->getValueEEG();
    }

    double test()
    {
        return 1.2f;
    } 

}

dllmain.h

#pragma once  

#ifdef DLL_EXPORTS  
#define DLL_API __declspec(dllexport)   
#else  
#define DLL_API __declspec(dllimport)   
#endif  

namespace museDLL
{
    extern "C" {
        DLL_API void __cdecl startConnection();
        DLL_API double __cdecl getValue();
        DLL_API double __cdecl test();

    }
}

If I call the test function in my DLL it works. However If I call the startConnection function that use functions from the original .lib it crashes ( no error messages ) .

so to make it clear , I want test file-> calling my custom DLL -> calling the downloaded .lib .

I don't understand what I am doing wrong?

I wonder if the initial .lib is included in the .DLL or do I need to link it somehow?

tiblop
  • 17
  • 8
  • Your caller has `__stdcall`, but your DLL doesn't. Also, since you didn't use `extern "C"`, it is incredible that `GetProcAddress("startConnection")` would find the function `void museDLL::startConnection(void)` -- the actual name in the DLL will be highly mangled. – Ben Voigt Jun 06 '17 at 14:06
  • Make sure to test the value returned by `GetProcAddress` before trying to cast and call it. – Ben Voigt Jun 06 '17 at 14:06
  • @BenVoigt I call the test function with this exact same process and it work correctly ( send me back the 1.2 value ) . So I doubt the problem is here. I use external "C" in my .h – tiblop Jun 06 '17 at 14:22
  • Now that you've shown the header file, it is clear that you have a mismatch in calling convention. That's going to matter an awful lot when you start having functions with more than zero parameters. – Ben Voigt Jun 06 '17 at 14:26
  • what changes should I do ? ( I know for sure the startConnection is called correctly because if I remove everything in it it works fine, its just when it call functions from the .lib that it crashes ) – tiblop Jun 06 '17 at 14:28
  • I wonder if the initial .lib is included in the .DLL or do I need to link it somehow? – tiblop Jun 06 '17 at 14:37

1 Answers1

0

You only have the .lib file and header files. It seems, you have a static library. So you could be able to build your application and link the library as static library. In other words, you are not able to somehow create a .dll library from the given .lib until you do not have an access to a source code of the library. Just include the header files, set the linker options and call the functions in your application.

On the other hand, the .lib file could contain symbols to allow the linker to link to a .dll, but as I mentioned above, you need the .dll - then the .lib file is useless. As you can't build the .dll without original source code of the library, you can write your own library (using the given header) and build it as .dll.

More about the libraries you can find under What's the differences between .dll , .lib, .h files?

Microsoft has some walkthrough guides as well:
Walkthrough: Creating and Using a Static Library (C++):
Walkthrough: Creating and Using a Dynamic Link Library (C++)

Dom
  • 532
  • 1
  • 9
  • 23
  • "Just include the header files, set the linker options and call the functions in your application." yes I can use the .lib as this but the problem is that it is a c++ library and I need to use it in unity ( c#) , this is why I wanted to wrap it in a c# DLL . Is there no way to create a DLL that call this lib? I could give the link to both the DLL and the lib to unity ?! I don't understand what I have to do? – tiblop Jun 07 '17 at 08:50
  • Read more about lib and dll ;-). .lib is not .dll and you can not use it in C#... Unfortunately, the lib is useless for you than. – Dom Jun 07 '17 at 09:10