Article ID: 125689
Article Last Modified on 9/27/2004
PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo = {0};
StartupInfo.cb = sizeof(STARTUPINFO);
if (CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE,
0, NULL, NULL, &StartupInfo, &ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
/* Process has terminated */
...
}
else
{
/* Process could not be started */
...
}
If necessary, you can put this code into a separate thread to allow the
initial thread to continue to execute.
105676 Starting and Terminating Windows-based Application
FARPROC lpfnCallback;
lpfnCallback = MakeProcInstance(NotifyRegisterCallback, ghInst);
if (!NotifyRegister(NULL, (LPFNNOTIFYCALLBACK)lpfnCallback,
NF_NORMAL))
{
MessageBox(NULL, "NotifyRegister Failed", "Error", MB_OK);
FreeProcInstance(lpfnCallback);
}
The next section of code demonstrates the implementation of the callback
function:
BOOL FAR PASCAL __export NotifyRegisterCallback (WORD wID,
DWORD dwData)
{
HTASK hTask; // task that called the notification callback
TASKENTRY te;
// Check for task exiting
switch (wID)
{
case NFY_EXITTASK:
// Obtain info about the task that is terminating
hTask = GetCurrentTask();
te.dwSize = sizeof(TASKENTRY);
TaskFindHandle(&te, hTask);
// Check if the task that is terminating is our child task.
// Also check if the hInstance of the task that is
// terminating is the same as the hInstance of the task
// that was WinExec'd by us earlier in the program.
if (te.hTaskParent == ghtaskParent &&
te.hInst == ghInstChild)
PostMessage(ghwnd, WM_USER+509, (WORD)te.hInst, dwData);
break;
default:
break;
}
// Pass notification to other callback functions
return FALSE;
}
The NotifyRegisterCallback() API is called by the 16-bit TOOLHELP DLL in
the context of the process that is causing the event. Problems arising
because of reentrancy and notification chaining makes the callback function
subject to certain restrictions. For example, operations that cause
TOOLHELP events cannot be done in the callback function. (See the TOOLHELP
NotifyRegister function documentation in your Software Development Kit for
events that cause TOOLHELP callbacks.)
BOOL StillExecuting(HINSTANCE hAppInstance)
{
TASKENTRY te = {0};
te.dwSize = sizeof(te);
if (TaskFirst(&te))
do
{
if (te.hInstance == hAppInstance)
return TRUE; // process found
} while (TaskNext(&te));
// process not found
return FALSE;
}
Refer to the TermWait sample for complete details on how to use
NotifyRegister and implement a callback function. For additional
information, please search in the Microsoft Knowledge Base using this word:
TERMWAIT
Additional query words: end exit notification notify spawn terminate termination
Keywords: kbhowto kbthread kbprogramming kbkernbase kbcode KB125689