繁体   English   中英

Windows 64位Ctypes模块上的Python 32位模块错误

[英]Python 32bit on Windows 64bit Ctypes module error

在python 3.4.3和2.7.9中,当我尝试从内核库调用任何函数时。

从64位Windows上的32位版本的python打印错误消息:

from ctypes import *
path=create_string_buffer(256) 
rs=cdll.Kernel32.GetModuleFileNameA(0,path,256)
print (path)

错误如下:

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

异常消息告诉您答案:

ValueError:调用过程的参数不足 (缺少12个字节)或调用约定错误

参数的数量是正确的,因此也必须是另一个:您使用了错误的调用约定。 调用约定是编译器将C中的三个参数映射为在调用函数时将实际值存储在内存中的一种方式(还有其他一些事情)。 用于GetModuleFileAMSDN文档上,您可以找到以下签名

DWORD WINAPI GetModuleFileName(
  _In_opt_ HMODULE hModule,
  _Out_    LPTSTR  lpFilename,
  _In_     DWORD   nSize
);

WINAPI告诉编译器使用stdcall调用约定。 您的ctypes代码使用cdll ,另一方面,它假定cdecl调用对流。 解决方案很简单:将cdll更改为windll

from ctypes import *
path=create_string_buffer(256) 
rs=windll.Kernel32.GetModuleFileNameA(0,path,256)
print (path)

访问.dll的ctypes文档进行比较,其中明确显示了kernel32使用windll

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM