在VC程序中創(chuàng)建進(jìn)程,結(jié)束進(jìn)程,,按如下步驟進(jìn)行即可:
1.取得進(jìn)程的句柄(利用FindWindow函數(shù)得到);
2.獲取進(jìn)程ID號(hào)(用GetWindowThreadProcessId函數(shù)獲?。?;
3.打開進(jìn)程,OpenProcess函數(shù)中的第一個(gè)參數(shù)設(shè)為PROCESS_TERMINATE,,就可以獲取處理該進(jìn)程的句柄,;
4.利用TerminateProcess函數(shù)結(jié)束進(jìn)程,將該函數(shù)的第二個(gè)參數(shù)設(shè)為4,。
代碼如下:
//結(jié)束進(jìn)程
int CStaticFunc::KillProcess(LPCSTR pszClassName, LPCSTR
pszWindowTitle)
{
HANDLE hProcessHandle;
ULONG nProcessID;
HWND TheWindow;
TheWindow = ::FindWindow( NULL, pszWindowTitle );
///方法一:
::GetWindowThreadProcessId( TheWindow, &nProcessID );
hProcessHandle = ::OpenProcess( PROCESS_TERMINATE, FALSE,
nProcessID );
return ::TerminateProcess( hProcessHandle, 4 );
///方法二:
return ::PostMessage(TheWindow, WM_CLOSE, NULL, NULL);
}
而啟動(dòng)進(jìn)程則只需要CreateProcess函數(shù)就可完成,,需要注意的是這個(gè)函數(shù)的幾個(gè)輸入?yún)?shù),第一個(gè)參數(shù)是
//創(chuàng)建新進(jìn)程
///方法一:
int CStaticFunc::CreateNewProcess(LPCSTR pszExeName)
{
PROCESS_INFORMATION piProcInfoGPS;
STARTUPINFO siStartupInfo;
SECURITY_ATTRIBUTES saProcess, saThread;
ZeroMemory( &siStartupInfo, sizeof(siStartupInfo) );
siStartupInfo.cb = sizeof(siStartupInfo);
saProcess.nLength = sizeof(saProcess);
saProcess.lpSecurityDescriptor = NULL;
saProcess.bInheritHandle = true;
saThread.nLength = sizeof(saThread);
saThread.lpSecurityDescriptor = NULL;
saThread.bInheritHandle = true;
return ::CreateProcess( NULL, (LPTSTR)pszExeName, &saProcess,
&saThread, false,
CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
&siStartupInfo, &piProcInfoGPS );
}
///方法二:
WinExec(lpCmdLine, uCmdShow);