Converting static link library to dynamic dll

2019-01-16 13:29发布

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?

7条回答
ら.Afraid
2楼-- · 2019-01-16 14:04

The issue here is not how the code is decorated, it's the extra step of creating a static library that contains all of the entry points, and trying to build the dll out of that.

If you go with the __delcspec approach and build the static library first, then try to link to it when building a DLL, you'll have to solve the dead-code stripping problem.

When you link, the obj srcs are used to find all of the decorated exports and dependencies are resolved, everything else is stripped. If you have no DLL src, so no obj files (except maybe a dll main), all of the code in the lib you want to export will be stripped (regardless of the attributes).

So, you either have to:

  1. Tell the linker not to strip unused code, which is probably going to give you a lot of stuff you don't want.
  2. Use a def file to expose the exports manually
  3. Link the dll against the obj files used to create the lib instead of linking to the lib directly.
  4. Or maybe create dummy code that references the functions you want to export from something you are exporting.
查看更多
登录 后发表回答