Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting static link library to dynamic dll

I have .lib file with its header (.h) file. This file have a few functions that need to be used in C# application.

After googling I found that I need to create a dynamic DLL from this static library and call this dynamic DLL from C# code using interop.

  1. I have created a win32 project and selected type DLL.

  2. Included header file and added .lib to additional dependencies.

    I am able to see the functions defined in the static library (when I press ctrl + space).

As a total newbie I do not know how I can export the function, which is, in .lib with following signature:

void testfun( char* inp_buff, unsigned short* inp_len, char* buffer_decomp,unsigned *output_len,unsigned short *errorCode)

I want same signature in my dynamic DLL with a different name.

What to write in header file and .cpp file?

like image 604
Manjoor Avatar asked Mar 05 '10 06:03

Manjoor


People also ask

Can a DLL link to static library?

The DLL can link to the same MFC static link libraries used by applications. There is no longer a separate version of the static link libraries for DLLs.

Can a exe link to DLL and static library at the same time?

Yes, the Core and Utils code will be duplicated. Instead of building them as static libs you can build them as dlls and use anywhere.


3 Answers

This is what you can do

  1. Add the following code to you .H file. rename "MYPROJECT" to your project name

    #ifdef MYPROJECT_EXPORTS
    #define MYPROJECT_API __declspec(dllexport)
    #else
    #define MYPROJECT_API _declspec(dllimport)
    #endif
    
  2. Go to Properties->C++->Preprocessor and Add the defenition - MYPROJECT_EXPORTS

  3. Add MYPROJECT_API to all the functions you want the dll to expose eg:

    MYPROJECT_API void Test();
    
  4. Go to Project properties General -> Configuration Type change it to Dynamic Dll

You are done

like image 43
SysAdmin Avatar answered Oct 15 '22 10:10

SysAdmin


If you can recompile your lib, just add __declspec(dllexport) to the signatures of all of the functions you want to be exported.

void __declspec(dllexport) testfun( char* inp_buff, unsigned short* inp_len, char* buffer_decomp,unsigned *output_len,unsigned short *errorCode)

If you can't do that, then you can export them by writing a .def file instead. Using def files you can even change the name of a function as it is exported. http://msdn.microsoft.com/en-us/library/28d6s79h.aspx

---- contents of mylib.def ----

LIBRARY

EXPORTS
   testfun
   newname=testfun2

Then when you link the dll, include mylib.def

link /dll /machine:x86 /def:mylib.def  mylib.lib

Edit2:

note that pinvoke assumes that the functions you import will have _stdcall calling convention unless you say otherwise. So you might need to do this as well, in your C# code.

[DllImport("mylib.dll", CallingConvention=CallingConvention.Cdecl)]

Or, you could change your C++ code to be __stdcall

void __declspec(dllexport) __stdcall testfun( char* inp_buff, ...
like image 54
John Knoeller Avatar answered Oct 15 '22 11:10

John Knoeller


Create new Dll project using Visual Studio Application Wizard, and check "Exports Symbols" in one of the Wizard steps. It creates sample Dll which exports class, function and variable. You can learn from this sample, how to do this. Generally, every exported function is declared as __declspec(dllexport). In a client project it is declared as __declspec(dllimport). Dll code uses the constant which is defiled as __declspec(dllexport) inside of Dll project, and __declspec(dllimport) in any other place.

like image 21
Alex F Avatar answered Oct 15 '22 10:10

Alex F