第一,强调一下,或许调用C++的dll函是可以实现的,但是我没有查到,很多都是包了一层而且不注明,
第二,调用的时候,传递参数的时候会出现windows erro或者无访问权限的问题,需要通过c_int,c_char,c_char_p等进行转换并且调用encode函数,才能把函数传递到C语言包着的api里面,
下面是具体流程
dll可以通过vs新建的时候选择动态链接库,vs+qt也要选择动态链接库,
在接口api最前面加上__declspec(dllexport)
如
Test.h
class Test{
Test();
void generator();
}
Test.cpp
Test::Test()
{
cout<<"Test print"<<endl;
}
void Test::generator()
{
cout<<"generator"<<endl;
}
extern "C" {
__declspec(dllexport) void generator(char *filepath)
{
Test a;
cout<< filepath<<endl;
a.generator();
}
}
python
import ctypes
generator = ctypes.cdll.LoadLibrary("Test.dll")
generator.generator(c_char_p('E:\\head'.encode()))