void CWindowHandleDlg::OnBnClickedOk()
{
STARTUPINFO StartupInfo;
PROCESS_INFORMATION PrcInfo;
HANDLE hHandle;
DWORD dwExitCode=0;
memset(&StartupInfo,0,sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_SHOW;
::CreateProcess("C:\\Windows\\System3\\Notepad.exe",NULL,NULL,NULL,FALSE,0,NULL,NULL,&StartupInfo,&PrcInfo); /*Create Process
hHandle = ::OpenProcess(PROCESS_ALL_ACCESS,0,PrcInfo.dwProcessId); // handle of that process
::GetExitCodeProcess(hHandle,&dwExitCode); // Check for the status of that handle
if(dwExitCode==STILL_ACTIVE) // if its active terninate the process.
::TerminateProcess(hHandle,dwExitCode);
}
Initially a Thread is created by CreateProcess
Create A Process:
BOOL WINAPI CreateProcess(
_in_opt LPCTSTR lpApplicationName, __inout_opt LPTSTR lpCommandLine,
__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in_opt LPVOID lpEnvironment,
__in_opt LPCTSTR lpCurrentDirectory,
__in LPSTARTUPINFO lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation
);
- lpApplicationName - the Exe which has to be launched. The 9 th and the 10 th parameter must be initialized .
- You can leave all the other parameters as null and the bool variable bInheritHandles (which allows inheritance) can be set to FALSE .
- A structure STARTUPINFO has to be passed. Before passing it is memset to zero. The structure's variable cb has to be intialized to the structure size.
- lpProcessInformation has to be passed which stores all the information about the launched exe such as ProcessID ,ThreadID..etc.
- Before Terminating the process its better to check for the exsistence of the process.
- Handle of the process created is recieved from the variable lpProcessInformation.dwProcessId.
- If the process is still active then we can terinate the process.
Terminating the process:
::TerminateProcess(hHandle,dwExitCode);
- Just the window handle alone with the status of that handle.
No comments:
Post a Comment