Actually this is a piece of testing code.
But some of your points I have missed.
Thanks.
"Joseph M. Newcomer" <newcomer@flounder.com> ?????????
See below...
On Wed, 16 Sep 2009 20:50:06 +0800, "Kilo" <kilo@yahoo.com.hk> wrote:
Here is my code. It pops up a message box with text "Wait object". But I
can
still see the dialog box of that process.
DWORD createProcess(CString applicationPath, CString parameter) {
****
Why are these arguments not const CString & values?
****
STARTUPINFO startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
PROCESS_INFORMATION processInfo;
ZeroMemory(&processInfo, sizeof(processInfo));
DWORD returnCode = 0;
parameter = applicationPath + _T(' ') + parameter;
****
The correct code here would be
CString arglist = _T("\"") + applicationPath + _T("\" ) + parameter;
the use arglist.GetBuffer
****
if (!CreateProcess(
NULL,
parameter.LockBuffer(),
****
This is erroneous code. You want to use GetBuffer()
****
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&processInfo)) {
returnCode = GetLastError();
}
parameter.UnlockBuffer();
****
Use the corresponding ReleaseBuffer
I find it odd that you check to set the error code after CreateProcess but
you don't
actually DO anything with that information. Therefore, you have NO IDEA
if the
processinfo.hProcess handle is meaningful! Yet you blindly go ahead and
try to use it,
and then close it, naively assuming this code will work.
****
switch (WaitForSingleObject(processInfo.hProcess, INFINITE)) {
case WAIT_ABANDONED:
AfxMessageBox(_T("Wait abandoned"));
****
This will not happen. Abandoned state only applies to Mutexes
****
break;
case WAIT_TIMEOUT:
AfxMessageBox(_T("Wait timeout"));
*****
Because you are waiting infinitely, this will not happen. And I will say
that your choice
to do an infinite wait is a seriously poor design choice. This should not
be done here.
You must not block the main GUI thread while some other program runs, so
you will have to
do some redesign to fix this serious defect in the code. Essentially, it
is ALWAYS
inappropriate to launch a process in the main GUI thread and block the
main GUI thread
waiting for it to finish. You are stuck in some dead paradigm of
procedure call which is
totally inappropriate here.
*****
break;
case WAIT_OBJECT_0:
AfxMessageBox(_T("Wait object"));
break;
case WAIT_FAILED:
AfxMessageBox(_T("Wait failed"));
break;
}
****
I notice that you do not actually say which case occurred, so I gather we
are left to
guess wildly at what happened.
*****
CloseHandle(processInfo.hThread);
CloseHandle(processInfo.hProcess);
// zero if success
// non-zero if fail
return returnCode;
}
"Goran" <goran.pusic@gmail.com> ???
news:2c3cf2af-f739-4b5e-80c7-99176c19ed17@f10g2000vbf.googlegroups.com
???...
On Sep 16, 12:56 pm, "Kilo" <k...@yahoo.com.hk> wrote:
I have called CreateProcess to create a process. Then I called
WaitForSingleObject with INFINITE timeout. However, it does not wait.
It does. Chances are that your code is wrong.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm