前言:
在一個進程中創建並啟動一個新進程,無論是對於病毒木馬程序還是普通的應用程序而言。這都是一個常見的技術,最簡單的方法無非是直接通過調用WIN32 API函數創建新進程。用戶層上,微軟提供了WinExec、ShellExecute和CreateProcess等函數來實現進程創建
實現代碼:
//************************************ // 函數名: CStartDlg::WinExec_Start // 返回類型: BOOL // 功能: 以WinExec的方式創建進程 // 參數1: char * pszExePath exe文件路徑 // 參數2: UINT uiCmdShow 顯示方式 //************************************ BOOL CStartDlg::WinExec_Start(char *pszExePath, UINT uiCmdShow) { UINT uiRet = 0; //函數成功,返回值大於31 uiRet = WinExec(pszExePath, uiCmdShow); if (31 < uiRet) { return TRUE; } return FALSE; } //************************************ // 函數名: CStartDlg::ShellExecute_Test // 返回類型: BOOL // 功能: 以ShellExecute的方式創建進程 // 參數1: CString pszExePath exe文件路徑 // 參數2: UINT uiCmdShow 顯示方式 //************************************ BOOL CStartDlg::ShellExecute_Start(CString pszExePath, UINT uiCmdShow) { HINSTANCE hInstance = 0; //ShellExecute函數不僅可以運行exe文件,也可以運行已經關聯的文件。 //例如,可以打開網頁、發送郵件、以默認程序打開文件、打開目錄、打 //印文件等。若返回值大於32,則表示執行成功,否則執行失敗 hInstance = ShellExecute(NULL, NULL, pszExePath, NULL, NULL, uiCmdShow); if (32 < (DWORD)hInstance) { return TRUE; } return FALSE; } //************************************ // 函數名: CStartDlg::CreateProcess_Start // 返回類型: BOOL // 功能: 以CreateProcess方式創建進程 // 參數1: char * pszExePath exe文件路徑 // 參數2: UINT uiCmdShow 顯示方式 //************************************ BOOL CStartDlg::CreateProcess_Start(char* pszExePath, UINT uiCmdShow) { STARTUPINFO si = { 0 }; PROCESS_INFORMATION pi; BOOL bRet = FALSE; si.cb = sizeof(si); si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成員有效 si.wShowWindow = uiCmdShow; bRet = CreateProcess(NULL, (LPWSTR)pszExePath, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi); if (bRet) { //不使用的句柄最好關掉 CloseHandle(pi.hThread); CloseHandle(pi.hProcess); return TRUE; } return FALSE; }