我正在尝试编写一个可以在 Python (2.7) 中导入的 DLL,但我在“使其工作”时遇到了困难。WinDLL()
当我使用或在 Python 中加载库windll.LoadLibrary()
并测试导出的函数时,我得到的输出为空。如果我向它添加一个参数,TestFunction()
则会引发一个ValueError
声明可能有很多参数(实际上不是)。
蟒蛇文件:
from ctypes import *
x = windll.LoadLibrary('./pymod.dll')
print x.TestFunction(123) #Should return 123.
主.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
#define DLL_EXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C"
{
#endif
int DLL_EXPORT TestFunction(int data);
#ifdef __cplusplus
}
#endif
#endif
和main.cpp:
#include "main.h"
int DLL_EXPORT TestFunction(int x = 0) {
return x;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
switch (fdwReason){
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
已解决:问题是错误的调用约定。