最近心血来潮准备写一个键盘记录器,作为一个键盘记录器当然要能记录窗口标题了!
这里用的是ctypes,ctypes是python的一个的一个外部库,它赋予了程序员类似C语言一样的底层操作能力。ctypes模块使的程序员可以轻而易举的调用动态链接库中的导出函数,并且可以构建C数据类型。
这里,我们就用windll.user32 windll.kernel32 windll.psapi 这三个模块!
代码:
# -*- coding: utf-8 -*-
from ctypes import *
user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
current_window = None
def get_current_process():
# 获取最上层的窗口句柄
hwnd = user32.GetForegroundWindow()
# 获取进程ID
pid = c_ulong(0)
user32.GetWindowThreadProcessId(hwnd,byref(pid))
# 将进程ID存入变量中
process_id = "%d" % pid.value
# 申请内存
executable = create_string_buffer("\x00"*512)
h_process = kernel32.OpenProcess(0x400 | 0x10,False,pid)
psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)
# 读取窗口标题
windows_title = create_string_buffer("\x00"*512)
length = user32.GetWindowTextA(hwnd,byref(windows_title),512)
print "[ PID:%s-%s-%s]" % (process_id,executable.value,windows_title.value)
# 关闭handles
kernel32.CloseHandle(hwnd)
kernel32.CloseHandle(h_process)
get_current_process()
各句代码的作用我已经注释了!
enjoy yourself
本人博客:www.bluexiang.com 欢迎光临