建立進程並等待進程退出

來源:互聯網
上載者:User

// cereatepross.cpp : 定義控制台應用程式的進入點。
//

#include "stdafx.h"
#include<iostream>
//#include<WinBase.h>
#include<Windows.h>
#include<TlHelp32.h>
#include<atlstr.h>
using namespace std;
BOOL FindAndKillProcessByName(LPCTSTR strProcessName);
int _tmain(int argc, _TCHAR* argv[])
{
 // 臨時變數
 CStringA sCommandLine;
 wchar_t cWindowsDirectory[MAX_PATH];
 char cCommandLine[MAX_PATH];
 DWORD dwExitCode;
 PROCESS_INFORMATION pi;
 STARTUPINFO si = {sizeof(si)};
 // 得到Windows檔案夾目錄
 GetWindowsDirectory(cWindowsDirectory, MAX_PATH);
 // 啟動"記事本"程式的命令列
 sCommandLine = CStringA(cWindowsDirectory) + L"\\NotePad.exe";
 ::strcpy(cCommandLine, sCommandLine);
 // 啟動"記事本"作為子進程
 
         wchar_t pszMultiByteString[MAX_PATH];
            MultiByteToWideChar( CP_UTF8, 0, cCommandLine, /*sizeof(pszTemp)*/ -1, pszMultiByteString, MAX_PATH );

 BOOL ret = CreateProcess(NULL,pszMultiByteString, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
 //建立的子進程控制代碼存放在pi中
 if (ret) {
  // 關閉子進程的主線程控制代碼
  CloseHandle(pi.hThread);
  // 等待子進程的退出
  WaitForSingleObject(pi.hProcess,INFINITE);
  // 擷取子進程的退出碼
  GetExitCodeProcess(pi.hProcess,&dwExitCode);
  // 關閉子進程控制代碼
  CloseHandle(pi.hProcess);
 }
 BOOL FindAndKillProcessByName(LPCTSTR strProcessName);

 return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.