《ASCE1885的源码分析》の简单的进程封装类

一个简单的进程封装类,该类允许我们新建一个远程进程,并对其进行控制。

进程类CProcess的头文件如下:

class CProcess{

public:

PROCESS_INFORMATION ProcessInfo; //进程结构信息

STARTUPINFO StartupInfo; //启动信息

bool KillProcess(UINT); //强制杀死进程,不推荐

bool KillThread(UINT); //强制杀死线程,不推荐

bool LoadProcess(char *, char *, char *); //创建远程进程

bool Pause(void); //挂起主线程

bool Resume(void); //重启主线程

bool UnloadProcess(void); //释放进程和线程资源

bool WaitOnExit(LPDWORD); //等待远程进程的终止

};

进程类CProcess实现文件:

#include <Windows.h>

#include <stdio.h>

#include <stdlib.h>

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "CProcess.H"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool CProcess::LoadProcess(char *ExeName, char *CommandLine, char *CurrentDirectory)

{

ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));

ZeroMemory(&ProcessInfo, sizeof(PROCESS_INFORMATION));

StartupInfo.cb = sizeof(STARTUPINFO); //必须的

//Create a suspended Process

BOOL Result = CreateProcess(

ExeName, // pointer to name of executable module

CommandLine, // pointer to command line string

NULL, // pointer to process security attributes

NULL, // pointer to thread security attributes

TRUE, // handle inheritance flag

NORMAL_PRIORITY_CLASS | CREATE_SUSPENDED, // creation flags

NULL, // pointer to new environment block

CurrentDirectory, // pointer to current directory name

&StartupInfo, // pointer to STARTUPINFO

&ProcessInfo // pointer to PROCESS_INFORMATION

);

if(Result)

return true;

else return false;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool CProcess::UnloadProcess(void)

{

if(CloseHandle(ProcessInfo.hProcess) && CloseHandle(ProcessInfo.hThread))

return true;

else return false;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool CProcess::Pause(void)

{

if(SuspendThread(ProcessInfo.hThread))

return true;

else return false;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool CProcess::Resume(void)

{

if(ResumeThread(ProcessInfo.hThread))

return true;

else return false;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool CProcess::WaitOnExit(LPDWORD xReturnCode)

{

DWORD ReturnCode = 0;

do{

if(!GetExitCodeProcess(ProcessInfo.hProcess, &ReturnCode))

return false;

Sleep(1);

} while(ReturnCode == STILL_ACTIVE);

xReturnCode = &ReturnCode;

return true;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool CProcess::KillProcess(UINT ExitCode)

{

if(TerminateProcess(ProcessInfo.hProcess, ExitCode))

return true;

else return false;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool CProcess::KillThread(UINT ExitCode)

{

if(TerminateThread(ProcessInfo.hThread, ExitCode))

return true;

else return false;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

最后就是测试主函数了:

#include <Windows.h>

#include <stdio.h>

#include <stdlib.h>

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "CProcess/CProcess.H"

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main(void)

{

CProcess ASCEProcess;

DWORD ReturnCode = 0;

//调用另外一个进程,初始是挂起状态

if(!ASCEProcess.LoadProcess("D://Program Files//Foxmail//Foxmail.exe", NULL, NULL))

{

printf("Failed to execute the other application/n");

system("pause");

return 0;

}

//重新启动进程

ASCEProcess.Resume();

//等待远程进程的终止

printf("Waiting on remote process to terminate./n");

ASCEProcess.WaitOnExit(&ReturnCode);

//释放资源

ASCEProcess.UnloadProcess();

system("pause");

return 0;

}

你可能感兴趣的:(源码分析)