将静态链接库转换为动态DLL [英] Converting static link library to dynamic dll

查看:155
本文介绍了将静态链接库转换为动态DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有.lib文件与其标题(.h)文件。这个文件有一些需要在C#应用程序中使用的功能。



在谷歌搜索之后,我发现我需要从这个静态库创建一个动态DLL,并调用这个动态我从C#代码中使用互操作的DLL。


  1. 我已经创建了一个win32项目并选择了类型的DLL。

  2. 包含头文件,并添加了.lib到其他依赖项。



    我可以看到静态库中定义的函数(当我按ctrl +空格)


作为一个全新手,我不知道如何导出函数,这是在.lib与以下签名:

  void testfun(char * inp_buff,unsigned short * inp_len,char * buffer_decomp,unsigned * output_len,unsigned short *我的动态DLL中使用不同的名称,我想要相同的签名。

$ b




$ b

在头文件和.cpp文件中写什么?

解决方案

如果您可以重新编译您的lib,只需将 __ declspec(dllexport)添加到要导出的所有函数的签名。

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

如果你不能这样做,那么你可以通过编写一个.def文件来导出它们。使用def文件,甚至可以在导出时更改函数的名称。
http://msdn.microsoft.com/en-us/图书馆/ 28d6s79h.aspx



---- mylib.def ----

的内容

  LIBRARY 

EXPORTS
testfun
newname = testfun2

然后链接dll时,包含mylib.def

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



Edit2:



请注意,pinvoke假设您导入的函数将具有_stdcall调用约定,除非另有说明。因此,您可能需要在C#代码中执行此操作。

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

或者,您可以将C ++代码更改为__stdcall

  void __declspec(dllexport)__stdcall testfun(char * inp_buff,... 


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?

解决方案

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, ...

这篇关于将静态链接库转换为动态DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆