[英]Python 32bit on Windows 64bit Ctypes module error
in both python 3.4.3 and 2.7.9 when I try to call any function from kernel library. 在python 3.4.3和2.7.9中,当我尝试从内核库调用任何函数时。
from 32bit version of python on 64bit windows, an error message is printed: 从64位Windows上的32位版本的python打印错误消息:
from ctypes import *
path=create_string_buffer(256)
rs=cdll.Kernel32.GetModuleFileNameA(0,path,256)
print (path)
the error is as following : 错误如下:
Traceback (most recent call last):
File "test-ctypes.py", line 3, in <module>
ValueError: Procedure called with not enough arguments (12 bytes missing) or wrong calling convention
The exception message tells you the answer: 异常消息告诉您答案:
ValueError: Procedure called with not enough arguments (12 bytes missing) or wrong calling convention ValueError:调用过程的参数不足 (缺少12个字节)或调用约定错误
The number of arguments is right, so it must be the other: You are using the wrong calling convention. 参数的数量是正确的,因此也必须是另一个:您使用了错误的调用约定。 The calling convention is the way the compiler maps the three arguments in C into a way to store the actual values in memory when calling the function (among a few other things). 调用约定是编译器将C中的三个参数映射为在调用函数时将实际值存储在内存中的一种方式(还有其他一些事情)。 On the MSDN documentation for GetModuleFileA you find the following signature 在用于GetModuleFileA的MSDN文档上,您可以找到以下签名
DWORD WINAPI GetModuleFileName(
_In_opt_ HMODULE hModule,
_Out_ LPTSTR lpFilename,
_In_ DWORD nSize
);
The WINAPI
tells the compiler to use the stdcall
calling convention. WINAPI
告诉编译器使用stdcall
调用约定。 Your ctypes code uses cdll
which on the other hand assumes cdecl
calling convetion. 您的ctypes代码使用cdll
,另一方面,它假定cdecl
调用对流。 The solution is simple: change cdll
to windll
: 解决方案很简单:将cdll
更改为windll
:
from ctypes import *
path=create_string_buffer(256)
rs=windll.Kernel32.GetModuleFileNameA(0,path,256)
print (path)
Compare with the ctypes documentation for accessing .dll's , where kernel32
is explicitely shown to use windll
. 与访问.dll的ctypes文档进行比较,其中明确显示了kernel32
使用windll
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.