ctypes问题

问题描述 投票:0回答:1
from ctypes import *


user32 = windll.user32

def get_current_process():
    # get a handle to the foreground window
    hwnd = user32.GetForegroundWindow()

    # find the process ID
    pid = c_ulong(0)
    user32.GetWindowThreadProcessId(hwnd, byref(pid))

    # store the current process ID
    process_id = "%d" % pid.value

    # grab the executable
    executable = create_string_buffer("\x00" * 512)
    h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid)

    print(str(executable))

    psapi.GetModuleBaseNameA(h_process, None, byref(executable), 512)

    # now read it's title
    window_title = create_string_buffer("\x00" * 512)
    length = user32.GetWindowTextA(hwnd, byref(window_title), 512)

    # print out the header if we're in the right process
    print ("[ PID: %s - %s - %s ]" % (process_id, executable.value, window_title.value))


    # close handles
    kernel32.CloseHandle(hwnd)
    kernel32.CloseHandle(h_process)


get_current_process()

运行此错误信息:

追踪(最近一次通话):文件“ C:/ Users / **** / Desktop / KeyLogger / testkeylogger.py”,第75行,在get_current_process()文件“ C:/ Users / **** / Desktop / KeyLogger / testkeylogger.py”,第51行,在get_current_process可执行文件= create_string_buffer(“ \ x00” * 512)文件“ C:\ Users **** \ anaconda3 \ envs \ tf1 \ lib \ ctypes__init __。py”,第63行,在create_string_buffer引发TypeError(init)TypeError:

有人知道谁可以解决这个问题吗?

python ctypes pid keylogger
1个回答
0
投票
我认为这条线:

executable = create_string_buffer("\x00" * 512)

导致了您的问题。 create_string_buffer()接受一个int或bytes对象作为参数(source):

ctypes.create_string_buffer(init_or_size,size = None)

此功能创建一个可变字符缓冲区。返回的对象是ctypesc_char数组。

init_or_size必须是一个整数,它指定数组的大小,或将用于初始化数组项的字节对象。

您可以加上'b'以获得所需的功能,我相信:

executable = create_string_buffer(b"\x00" * 512)

之前,您正在创建一个大字符串:

>>> type('\x00' * 512) <class 'str'> >>> type(b'\x00' * 512) <class 'bytes'>

© www.soinside.com 2019 - 2024. All rights reserved.