1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > delphi 调用dll 整形返回值_VS 编写C++ DLL库及C++ C# python 调用

delphi 调用dll 整形返回值_VS 编写C++ DLL库及C++ C# python 调用

时间:2024-04-02 21:04:17

相关推荐

delphi 调用dll 整形返回值_VS 编写C++ DLL库及C++  C# python 调用

VS生成c++dllC++ 程序调用 dllC# 程序调用 dll

1. VS 生成C++dll

可以有两种方法

通过_declspec(dllexport)

extern "C" __declspec(dllexport) int __stdcall add(int a, int b);

嫌上面太麻烦,windows系统下可用 .def文件

其中:extern "c" 防止导出的函数名字是乱码

使用 def导出dll步骤如下:

新建Login.h

//登录接口extern "C" int _stdcall Login(char* username, char* password);//退出接口 无返回值extern "C" bool _stdcall Logout();

新建Login.cpp

int _stdcall Login(char* name, char* pwd) {}

新建模块定义文件Login.def

LIBRARY LogindllEXPORTSLogin @ 1Logout @ 2Heartbeat @3

生成文件Debug 和Release下 Login.dll Login.lib

2.C++ 调用

需要3个文件 Login.h Login.dll Login.lib

根据Debug和Release 分别把三个文件拷贝到对应工程目录下

步骤:

右键添加现有项 选择Login.h右键添加现有项 选择Login.lib在使用的地方 #include “Login.h”直接使用函数

{Login("user","pwd");}

3.C# 调用

需要1个文件 Login.dll

将Login.dll 拷贝到相对应的debug或release目录下使用时代码如下

[DllImport("Logindll.dll", EntryPoint = "Login", CallingConvention = CallingConvention.StdCall)]extern static int Login(string name,string pwd);

4. python调用

需要2个文件 Login.dll Login.lib

将上面两个文件拷贝到.py文件同名目录通过ctypes调用注意python是x86 x64 和dll的版本保持一致

from ctypes import *import osCUR_PATH=os.path.dirname(__file__)dllPath=os.path.join(CUR_PATH,"Logindll.dll")pDll=cdll.LoadLibrary(dllPath)pResutl= pDll.Logout()print (pResutl)

注意:

x86 x64 debug release 库的对应关系

参考:

CSDN-专业IT技术社区-登录​在VS中用C++编写可被其它语言调用的动态库DLL - 优秀afa - 博客园​

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。