BOOL Start(CString exepath,WORD wShowWindow)
{
if(exepath.IsEmpty())
{
return false;
}
//STARTUPINFO結構 該結構用於指定新進程的主窗口特性
STARTUPINFO si;
ZeroMemory(&si,sizeof(STARTUPINFO));
si.cb=sizeof(STARTUPINFO);
si.dwFlags=STARTF_USESHOWWINDOW;
si.wShowWindow=wShowWindow;
if(wShowWindow!=SW_HIDE)
{
si.lpDesktop=(LPWSTR)"WinSta0\\Default";
}
PROCESS_INFORMATION processinfo;
memset(&processinfo,0,sizeof(processinfo));//=ZeroMemory(&newprocessinfo,sizeof(newprocess));
//以上爲必要參數設置,下面開始建立新進程
if(!CreateProcess(
exepath, //LPCTSTR lpApplicationName,在此指定可執行文件的文件名及路徑
NULL, //LPTSTR lpCommandline,命令行參數
NULL, //LPSECURITY_ATTRIBUTES lpProcessAttributes,
NULL, //LPSECURITY_ATTRIBUTES lpThreadAttributes,
FALSE, //BOOL blnheritHandles,
0, //DWORD dwCreationFlags,
NULL, //LPVOID lpEnvironment,
NULL, //LPCTSTR lpCurrentDirectory,
&si, //LPSTARTUPINFO lpStartuplnfo,
&processinfo) //LPPROCESS_INFORMATION lpProcesslnformation
)
{
return FALSE;
}
else
{
return TRUE;
}
//不使用的句柄最好關掉
CloseHandle(processinfo.hThread);
CloseHandle(processinfo.hProcess);
}
void main()
{
CString exefilepath;
exefilepath="E:\\test\\mytets.exe";
Start(exefilepath,0);
}命令行