本文演示如何通過 CreateProcess 創建一個進程句柄和主線程句柄可以被繼承的子進程。
BOOL CreateProcess(
LPCTSTR lpApplicationName, // name of executable module
LPTSTR lpCommandLine, // command line string
LPSECURITY_ATTRIBUTES LPSECURITY_ATTRIBUTES , // SD
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
BOOL bInheritHandles, // handle inheritance option
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // new environment block
LPCTSTR lpCurrentDirectory, // current directory name
LPSTARTUPINFO lpStartupInfo, // startup information
LPPROCESS_INFORMATION lpProcessInformation // process information);
CreateProcess 的第三,第四參數分別表示新進程的進程屬性和主線程屬性,如果希望創建一個新進程,使得新進程的進程句柄和主線程句柄在當前進程的句柄表中可以被繼承,則需要對 LPSECURITY_ATTRIBUTES 進行設置。
下面通過一個例子來測試進程句柄繼承。
代碼中出現的路徑,請在您的機器上做相應的修改。
主進程 InheritProcessHandle.exe
// InheritProcessHandle.cpp : Defines the entry point for the console application.
// 測試創建可繼承的進程
#include "stdafx.h"
#include <WINDOWS.H>
int main(int argc, char* argv[])
{
// 創建一個進程句柄和主線程句柄可繼承的進程A
SECURITY_ATTRIBUTES saProcess, saThread;
saProcess.nLength = sizeof(saProcess);
saProcess.lpSecurityDescriptor = NULL;
saProcess.bInheritHandle = TRUE;
saThread.nLength = sizeof(saThread);
saThread.lpSecurityDescriptor = NULL;
saThread.bInheritHandle = TRUE;
STARTUPINFO siA = {0};
siA.cb = sizeof(siA);
PROCESS_INFORMATION piA;
char szCmd[256] = "c:\\notepad.exe";
CreateProcess(NULL, szCmd, &saProcess, &saThread, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &siA, &piA);
// 創建進程B,繼承當前進程句柄表中進程A的主線程句柄和A的進程句柄,句柄通過命令行參數傳遞給B進程
memset(szCmd, 0, 256);
sprintf(szCmd, "F:\\MyProjects\\InheritProcessHandle2\\Debug\\InheritProcessHandle2.exe %x %x", piA.hProcess, piA.hThread);
STARTUPINFO siB = {0};
siB.cb = sizeof(siB);
PROCESS_INFORMATION piB;
CreateProcess(NULL, szCmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &siB, &piB);
//printf("error: %d\n", GetLastError());
return 0;
}
子進程 InheritProcessHandle2.exe
// InheritProcessHandle2.cpp : Defines the entry point for the console application.
// 主進程通過 CreateProcess 創建該進程,兩個命令行參數分別表示notepad的進程句柄和主線程句柄
// 在該進程中繼承得到notepad的句柄,並對notepad進行控制操作
#include "stdafx.h"
#include <WINDOWS.H>
int main(int argc, char* argv[])
{
// 獲取繼承的notepad進程句柄和主線程句柄
DWORD dwProcessHandle = -1, dwThreadHandle = -1;
sscanf(argv[1], "%x", &dwProcessHandle);
sscanf(argv[2], "%x", &dwThreadHandle);
printf("進程句柄: %x 主線程句柄: %x\n", dwProcessHandle, dwThreadHandle);
// 掛起notepad
printf("掛起 notepad.exe 主線程5秒\n");
SuspendThread((HANDLE)dwThreadHandle);
Sleep(5000);
printf("恢復 notepad.exe 主線程.\n");
ResumeThread((HANDLE)dwThreadHandle);
printf("輸入0關閉 notepad.exe 進程\n");
while (getchar() != '0');
TerminateProcess((HANDLE)dwProcessHandle, 1);
WaitForSingleObject((HANDLE)dwProcessHandle, INFINITE);
printf("notepad.exe 進程已關閉\n");
return 0;
}
主進程運行結果
子進程啓動後,notepad進程被掛起5秒,可以看到notepad的界面已經阻塞
5秒後notepad恢復運行
子進程可以關閉notepad進程,通過繼承notepad的進程句柄。