gpt4 book ai didi

dll - 将静态链接库转换为动态dll

转载 作者:行者123 更新时间:2023-12-03 01:01:27 25 4
gpt4 key购买 nike

我有 .lib 文件及其头文件 (.h)。该文件有一些需要在 C# 应用程序中使用的函数。

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

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

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

    我能够看到静态库中定义的函数(当我按 ctrl + space 时)。

作为一个新手,我不知道如何导出该函数,即在 .lib 中具有以下签名:

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

我希望动态 DLL 中具有相同的签名,但名称不同。

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

最佳答案

如果您可以重新编译您的库,只需将 __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/library/28d6s79h.aspx

---- mylib.def 的内容 ----

LIBRARY

EXPORTS
testfun
newname=testfun2

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

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

编辑2:

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

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

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

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

关于dll - 将静态链接库转换为动态dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2384932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com