DLL导出函数的两种方法和存在的坑 2021-10-11

DLL导出函数的两种方法和存在的坑

使用dllexport导出函数

函数声明如下:
分几种情况讨论,是否使用extern “C”和__stdcall,共分为以下四种情况,

_declspec(dllexport) int  fun1();
_declspec(dllexport) int __stdcall fun2();
extern "C" _declspec(dllexport) int  fun3();
extern "C" _declspec(dllexport) int __stdcall fun4();

其导出函数如图所示
在这里插入图片描述
只有当添加extern "C"且不使用__stdcall时其导出函数的函数名不会发生改变,其他情况都会改变

使用.def文件导出函数

LIBRARY DllTest_Def

EXPORTS
fun1
fun2 @ 2 //注意空格

其中DllTest_Def是生成的dll名称,fun1是导出的函数名称,也可以在fun2后面添加@1,导出的函数名为依旧为fun2,@后面的数字表示导出函数的序号为n
可参考下列链接:https://www.cnblogs.com/findumars/p/8660427.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSharp 调用C++ DLL; 参数为指针类型导出函数 c# Csharp调用 c++库 参数为导入和导出指针两种 包含C++ DLL源码 如fun(cont char* A,char*B) A为输入参数,B为输出参数-C# CSharp call C++ DLL lib dll function param use export and import eg: fun(cont char* A,char*B) A IN,B OUT TestDll\Debug\TestCallDll.exe .......\.....\TestCallDll.vshost.exe .......\.....\TestCallDll.vshost.exe.manifest .......\.....\TestDll.dll .......\.....\TestDll.lib .......\TestCallDll\Form1.cs .......\...........\Form1.Designer.cs .......\...........\Form1.resx .......\...........\obj\Debug\TestCallDll.csproj.FileListAbsolute.txt .......\...........\...\.....\TestCallDll.csproj.GenerateResource.Cache .......\...........\...\.....\TestCallDll.exe .......\...........\...\.....\TestCallDll.Form1.resources .......\...........\...\.....\TestCallDll.pdb .......\...........\...\.....\TestCallDll.Properties.Resources.resources .......\...........\Program.cs .......\...........\...perties\AssemblyInfo.cs .......\...........\..........\Resources.Designer.cs .......\...........\..........\Resources.resx .......\...........\..........\Settings.Designer.cs .......\...........\..........\Settings.settings .......\...........\TestCallDll.csproj .......\....Dll\dllmain.cpp .......\.......\ReadMe.txt .......\.......\stdafx.cpp .......\.......\stdafx.h .......\.......\targetver.h .......\.......\TestDll.cpp .......\.......\TestDll.def .......\.......\TestDll.h .......\.......\TestDll.vcproj .......\.......\TestDll.vcproj.PC-201008261742.Administrator.user .......\TestDll.sln .......\TestDll.suo .......\....CallDll\obj\Debug\TempPE .......\...........\...\Debug .......\...........\obj .......\...........\Properties .......\Debug .......\TestCallDll .......\TestDll TestDll
MSVC vs. MinGW 之 (lib,dll,def,obj,exe) vs (a,dll,def,o,exe) 玩转攻略手记 一份粗糙的研究记录,有待补完和整理。 MinGW: c -> o gcc -c a.c c -> exe gcc a.c libs.o -o a.exe (从主程序a.c,附加libs,生成a.exe) o -> exe gcc a.o b.o ... -o main.exe c -> dll,def,a gcc a.c -shared -o a.dll -Wl,--output-def,a.def,--out-implib,liba.a a -> dll a2dll liba.a dll -> a: dlltool --dllname a.dll --def a.def --output-lib liba.a (需要def文件) a -> def: dumpbin /exports lib.a > lib.def (在windows上调用,def需要修改) dll -> def : pexports a.dll -o > a.def (这里的-o是指给函数标序号) lib -> def : reimp -d a.lib lib -> a: (for __cdecl functions in most case) reimp a.lib; (for __stdcall functions) MSVC: c -> lib cl /LD a.c (注意已经定义了export列表) c -> dll cl /LD a.c c -> obj cl /c a.c c -> exe cl a.c /out:a.exe dll ->lib lib /machine:ix86 /def:a.def /out:a.lib (需要def文件) obj ->lib lib a.obj b.obj... /out:mylib.lib dll ->def DUMPBIN a.dll /EXPORTS /OUT:a.def (生成的def需要做修正) lib ->def reimp -d a.lib (这个要在MSYS+MinGW下用) 关于这些工具的适用范围可以很容易的理解和记忆。 dll和exe都是PE文件,所以可以使用pexports. lib和a是静态库文件,都是归档类型,不是PE格式。所以不能使用pexports. dll可以使用dlltool. lib可以使用lib, 和reimp(lib->a工具) 所有的bin文件,包括dll,exe,lib,a都可以使用dumpbin. 参考: http://hi.baidu.com/kaien_space/blog/item/5e77fafa2ba9ff16a8d3110a.html Mingw官网文档: http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs http://oldwiki.mingw.org/index.php/CreateImportLibraries http://www.mingw.org/wiki/FAQ http://hi.baidu.com/opaquefog/blog/item/9b21b6deb324e25dccbf1ab7.html http://qzone.qq.com/blog/8330936-1238659272 http://hi.baidu.com/jzinfo/blog/item/b0aa1d308de99f9da8018e00.html 本篇测试用代码: 1. main.cpp #include #include #include "mylib.h" using namespace std; int main() { char str[]="Hello world!"; printhello(str); return 0; } 2. mylib.cpp #include #include #include "mylib.h" using namespace std; void EXPORT printhello(char *str) { cout << str << endl; } 3. mylib.h #define EXPORT __declspec(

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值