使用ctypes函数中的空指针

时间:2018-04-18 14:00:59

标签: python c++ hook ctypes

我需要将minHook.dll用于我的项目。 提供的示例似乎很简单,但我无法让它工作。 我不明白这一部分:

MESSAGEBOXW fpMessageBoxW = NULL;

// Create a hook for MessageBoxW, in disabled state.
if (MH_CreateHook(&MessageBoxW, &DetourMessageBoxW, 
    reinterpret_cast<LPVOID*>(&fpMessageBoxW)) != MH_OK)
{
    return 1;
}

这是我的Python代码(适用于我需要挂钩的功能):

def _callback_pointer(handler):
    """Create and return C-pointer"""
    print('handler=',cmp_func(handler))
    return cmp_func(handler)

def callback(*args, **kwargs):
    print(args, kwargs)
    res = windll.user32.CallNextHookEx(*args, **kwargs)
    print(res)
    return res
cmp_func = CFUNCTYPE(c_int, c_int, wintypes.HINSTANCE, POINTER(c_void_p))
hinstDLL = ctypes.windll.LoadLibrary('Gdi32')._handle
hkprcSysMsg = ctypes.windll.kernel32.GetProcAddress(hinstDLL, "TextOutW".encode(encoding='ascii'))
minHook = ctypes.cdll.LoadLibrary('lib/minHook.dll')
minHook.MH_Initialize()
orig_fp = None
minHook.MH_CreateHook(hkprcSysMsg, _callback_pointer(callback), byref(orig_fp))

最终出现错误:

  

TypeError:byref()参数必须是ctypes实例,而不是'NoneType'

我应该做些什么来至少让MH_CreateHook返回1

更新:以下是MH_CreateHook签名:

MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID *ppOriginal)

0 个答案:

没有答案