C语言中的函数默认是__cdecl调用,C++中可用__stdcall来显示声明调用,但也可以用extern “C”
用python调用dll时需要根据不同的调用约定而使用不同的函数。但是不管什么调用,最后都必须用extern “C”来防止名字粉碎。
dll源文件:
#include <iostream>
extern "C" _declspec(dllexport) int __stdcall stdAdd(int a, int b)
{
std::cout << "This is __stdcall dll" << std::endl;
return a + b;
}
extern "C" _declspec(dllexport) int cdeAdd(int a, int b)
{
std::cout << "This is __cdecl dll" << std::endl;
return a + b;
}
python代码:
#!/usr/bin/python
from ctypes import *
dll = windll.LoadLibrary('D:/CodeFile/Monitor/Debug/CppDll.dll')
print(dll.stdAdd(2, 4