直接上代码:
1、
my_debugger_defines.py
定义相关结构体(在后面创建进程及返回信息时,传参可用到)
1fromctypesimport*2#Let's map the Microsoft types to ctypes for clarity3Word=c_ushort4DWORD=c_ulong5LPBYTE=POINTER(c_ubyte)6LPTSTR=POINTER(c_char)7HANDLE=c_void_p8#constants9DEBUG_PROCESS=0x0000000110CREATE_NEW_CONSOLE = 0x0000001011#Structures for CreateProcessA() function12classSTARTUPINFO(Structure):13_fields_=[14("cb",DWORD),15("lpReserved",LPTSTR),16("lpDesktop",LPTSTR),17("lpTitle",LPTSTR),18("dwX",DWORD),19("dwY",DWORD),20("dwXSize",DWORD),21("dwYSize",DWORD),22("dwXCountChars",DWORD),23("dwYCountChars",DWORD),24("dwFlags",DWORD),25("wShowWindow",WORD),26("bcReserved2",WORD),27("lpReserved2",LPBYTE),28("hStdInput", HANDLE),29("hStdOutput", HANDLE),30("hStdError", HANDLE),31]3233classPROCESS_INFORMATION(Structure):34_fields_=[35("hProcess",HANDLE),36("hThread", HANDLE),37("dwProcessId", DWORD),38("dwThreadId", DWORD),39]
View Code
2、my_debugger.py
定义创建并跟踪进程的函数:
1fromctypesimport*2frommy_debugger_definesimport*34kernel32=windll.kernel3256classdebugger():7def_init_(self):8pass9defload(self,path_to_exe):10#dwCreation flag determines how to create the process11#set creation_flags = CREATE_NEW_CONSOLE if you want12#to see the calculator GUI13creation_flags =DEBUG_PROCESS1415startupinfo=STARTUPINFO()16process_information=PROCESS_INFORMATION()1718startupinfo.dwFlags=0x119startupinfo.wShowWindow20startupinfo.cb=sizeof(startupinfo)2122#win32api函数CreatProcess用来创建一个新的进程和他的主线程,23#这个新进程运行指定的可执行文件,由第一个参数指定24ifkernel32.CreateProcessW(path_to_exe,#should be CreateProcessW ,not CreateProcessA ,it is UNICODE API25None,26None,27None,28None,29#指定附加的、用来控制优先类和进程的创建的标志。30creation_flags,31None,32None,33#该参数指向一个用于决定新进程的主窗体如何显示的STARTUPINFO结构体。34byref(startupinfo),#byref() 按地址传递35#该参数指向一个用来接收新进程的识别信息的PROCESS_INFORMATION结构体。36byref(process_information)#这里有个问题:37#结构体之间的赋值是如何进行的?38#因为这里定义的process_information跟39#creatprocess中process_information的参数数量一致40#而startupinfo是不一致的41):42print("We have sucessfully lunched the process")43print("PID:%d"%process_information.dwProcessId)4445else:46print("Error:0x%08x."%kernel32.GetLastError())47
View Code
尝试,调用函数:
1importmy_debugger23debugger=my_debugger.debugger()45debugger.load("C:\Windows\System32\calc.exe")
View Code
问题:
#结构体之间的赋值是如何进行的?按顺序?
#这里自己定义的process_information结构体跟
#win32函数中creatprocess中process_information的成员数量、位置是必须一致的吗?
#好像也不是这样,因为我process_information是一致的,成功传参了,而我startupinfo不一致,也成功了。
关于win32函数中creatprocess中process_information、startupinfo见http://baike.baidu.com/view/2421585.htm