我有一个DLL,其中以下是标头和.c文件:
//demo.h
#define vers 0x1
__declspec export void getversion(ulong * version);
//demo.c
#include"demo.h"
void getversion(ulong * version)
{
*version=(ulong)vers;
}
我为使用ctypes调用DLL而编写的python脚本如下:
//python script
import ctypes
import sys
from ctypes import *
vers=0x1
def getversion(version):
mydll=cdll.LoadLibrary('demo.dll')
mydll.getversion.argtypes=[POINTER(c_ulong)]
mydll.getversion.restype=None
mydll.getversion(version)
version=c_ulong()
print("Version=",version.value)
但是我得到的版本值为0。输出应该改为1。谁能帮我以下代码?我不知道我要去哪里错了?
您的代码没有调用getversion
。您的代码将创建一个默认值ulong()
(初始化为零)并进行打印。
version=c_ulong()
print("Version=",version.value)
这是工作代码:
from ctypes import *
mydll = CDLL('demo.dll')
mydll.getversion.argtypes = [POINTER(c_ulong)]
mydll.getversion.restype = None
version=c_ulong()
mydll.getversion(byref(version)) # call function with address of version
print("Version=",version.value)
这是我测试过的DLL(您将无法编译...没有定义ulong
),并使用Microsoft Visual Studio进行了编译。
__declspec(dllexport) void getversion(unsigned long* version)
{
*version = 1;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句