가령 메모장 같은거 실행시킨후 끝나기를 기다리는 겁니다.
원래 델파이 소스인데 빌더용으로 고쳤습니다.
void WaitFor(void *processHandle)
{
TMsg msg;
DWORD ret;
do
{
ret = MsgWaitForMultipleObjects(
1, // { 1 handle to wait on }
(void * const *)&processHandle, // { the handle }
false, // { wake on any event }
INFINITE, // { wait without timeout }
QS_PAINT | // { wake on paint messages }
QS_SENDMESSAGE // { or messages from other threads }
);
if (ret == WAIT_FAILED)
return; // { can do little here }
if (ret == (WAIT_OBJECT_0 + 1))
{
// Woke on a message, process paint messages only. Calling
// PeekMessage gets messages send from other threads processed.
while(PeekMessage(&msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE ))
{
DispatchMessage(&msg);
}
}
}
while(ret != WAIT_OBJECT_0);
}
DWORD winexecAndWait32V2(char *FileName, int Visibility)
{
char zAppName[512];
TStartupInfo StartupInfo;
TProcessInformation ProcessInfo;
ULONG ret;
strcpy(zAppName, FileName);
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = Visibility;
if (! CreateProcess(NULL,
zAppName, // { pointer to command line string }
NULL, // { pointer to process security attributes }
NULL, // { pointer to thread security attributes }
false, // { handle inheritance flag }
CREATE_NEW_CONSOLE |// { creation flags }
NORMAL_PRIORITY_CLASS,
NULL, // { pointer to new environment block }
NULL, // { pointer to current directory name }
&StartupInfo, // { pointer to STARTUPINFO }
&ProcessInfo)) // { pointer to PROCESS_INF }
{
return -1; // { failed, GetLastError has error code }
}
else
{
WaitFor(ProcessInfo.hProcess);
GetExitCodeProcess(ProcessInfo.hProcess, &ret);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
}
return ret;
}
부를때는
winexecAndWait32V2("C:\\WINNT\\notepad.exe", 1);
|
이 게시판에 몇년 전에 올렸던 겁니다. ^^
http://www.borlandforum.com/impboard/impboard.dll?action=read&db=bcb_tip&no=37