Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PythoncTypesError-TypeError:需要一个整数(获取类型LP\u c\u long)_Python_Ctypes - Fatal编程技术网

PythoncTypesError-TypeError:需要一个整数(获取类型LP\u c\u long)

PythoncTypesError-TypeError:需要一个整数(获取类型LP\u c\u long),python,ctypes,Python,Ctypes,我正在尝试使用某些Windows API来使用Python和ctypes。我有一段代码失败了,我不知道为什么。我已经按照MSDN文档设置了所有argtypes和return类型。我得到的错误是:TypeError:需要一个整数(get type LP\u c\u long)。您可以在输出部分看到所有错误。就我所知,argtypes和return类型是正确的,我不确定需要修复什么 from ctypes import * from ctypes import wintypes as w user

我正在尝试使用某些Windows API来使用Python和ctypes。我有一段代码失败了,我不知道为什么。我已经按照MSDN文档设置了所有argtypes和return类型。我得到的错误是:
TypeError:需要一个整数(get type LP\u c\u long)
。您可以在输出部分看到所有错误。就我所知,argtypes和return类型是正确的,我不确定需要修复什么

from ctypes import *
from ctypes import wintypes as w

user32 = windll.user32
kernel32 = windll.kernel32

HOOKPROC = WINFUNCTYPE(HRESULT, c_int, w.WPARAM, w.LPARAM)  # Callback function prototype

user32.SetWindowsHookExW.argtypes = [c_int, HOOKPROC, w.HINSTANCE, w.DWORD]
user32.SetWindowsHookExW.restype = w.HHOOK
   
kernel32.GetModuleHandleW.argtypes = [w.LPCWSTR]
kernel32.GetModuleHandleW.restype = w.HANDLE

user32.GetMessageW.argtypes = [w.LPMSG, w.HWND, w.UINT, w.UINT]
user32.GetMessageW.restype = w.BOOL

user32.CallNextHookEx.argtypes = [w.HHOOK, c_int, w.WPARAM, w.LPARAM]
user32.CallNextHookEx.restype = w.LPLONG

def hook_procedure(nCode, wParam, lParam):
   print("Hello...")
   #return user32.CallNextHookEx(hook, nCode, wParam, lParam)
   return user32.CallNextHookEx(hook, nCode, wParam, c_lParam)
   
ptr = HOOKPROC(hook_procedure)
        
hook = user32.SetWindowsHookExW(
    13,
    ptr,
    kernel32.GetModuleHandleW(None),
    0
)

msg = w.MSG()                             # MSG data structure
user32.GetMessageW(byref(msg), 0, 0, 0) # Wait for messages to be posted


输出:

Hello...
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 262, in 'converting callback result'
TypeError: an integer is required (got type LP_c_long)
Exception ignored in: <function hook_procedure at 0x0000025C469551F0>
你好。。。
回溯(最近一次呼叫最后一次):
文件“\u ctypes/callbacks.c”,第262行,“转换回调结果”
TypeError:需要一个整数(获取类型LP\u c\u long)
在中忽略异常:
问题在于:

user32.CallNextHookEx.restype=w.LPLONG
CallnextHookEx
返回一个
LRESULT
,该值在C中定义为
LONG\u PTR
。这不是指针,而是指针大小的整数(32位系统为4字节,64位系统为8字节)
wintypes
没有该类型,但
LPARAM
具有相同的定义(LONG_PTR),因此您可以使用以下定义来定义32位和64位Python:

从ctypes将wintypes导入为w
LRESULT=w.LPARAM
...
user32.CallNextHookEx.restype=LRESULT

Ah好的。我只是假设名称中带有PTR的任何东西都是指针。@user1720897从32位操作系统转换到64位操作系统时,Windows的命名错误
LPLONG
是一个长指针
LONG_PTR
是与指针大小相同的长指针。Win32 API,其中整数必须在64位上增长刚刚添加了
\u PTR
(例如INT\u PTR、LONG\u PTR、DWORD\u PTR)。在32位窗口上为4字节,在64位窗口上为8字节。