现在的位置: 首页 > 综合 > 正文

创建进程并等待进程退出

2013年08月19日 ⁄ 综合 ⁄ 共 1125字 ⁄ 字号 评论关闭

// 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;
}

抱歉!评论已关闭.