Python中使用ctypes调用Windows API

# -*- coding: cp936 -*-

import ctypes
import ctypes.wintypes

#print(dir(ctypes))
#print(dir(ctypes.wintypes))

user32 = ctypes.WinDLL("user32.dll")
FindWindowA = user32.FindWindowA
GetWindowTextA = user32.GetWindowTextA
EnumWindows = user32.EnumWindows

#Windows回调函数
def EnumWindowProc(hWnd, lParam):
    text = ctypes.c_char_p()
    text.value = ""
    GetWindowTextA(hWnd, text, 200)
    print(text.value)
    return 1

#声明原型,注意需要首先要声明返回值类型
pFunc = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.wintypes.HWND, ctypes.wintypes.LPARAM)
pFuncHandle = pFunc(EnumWindowProc)

EnumWindows(pFuncHandle, None)

 

posted on 2014-11-27 00:31  牧之巴巴  阅读(1522)  评论(0编辑  收藏  举报

导航