Re: CreateProcess not work on Vista
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 26 Apr 2007 11:30:37 -0400
Define "can't". Whenever you call an API function, and it fails, you MUST call
::GetLastError() to get the reason; otherwise, it is impossible to diagnose what is wrong.
What is MAX_LEN? Did you mean perhaps MAX_PATH? Note that if MAX_LEN is less than
MAX_PATH you can have problems.
On Thu, 26 Apr 2007 15:44:24 +0700, "Duy Trinh" <duy.trinh@xxxxxxxxxxx> wrote:
hi all****
Pls see code below, i have a problem that if my code is compiled on
None-Unicode mode, it run well, but i compiled on Unicode mode, it can't run
cmdline (ffmpeg.exe ...) on Vista. Any idea?
CString sCmd;
TCHAR szCmd[MAX_LEN];
TCHAR szDir[MAX_LEN];
GetCurrentDirectory(MAX_LEN, szDir);
USES_CONVERSION;
sCmd.Format(_T("\"%s\\ffmpeg.exe\" -y -i \"%s\" \"%s\""), szDir,
A2T(AVIFile), tFilePath);
_tcscpy(szCmd, sCmd);
Use either StringCchCopy or, if using VS2005, _tcscpy_s, to avoid buffer overruns. You
are not checking to see if sCmd.GetLength() > MAX_LEN-1, so this has potential for fatal
consequences.
Using GetBuffer is preferrable to making a copy into a fixed-size buffer, e.g.,
LPTSTR p = sCmd.GetBuffer();
and after the CreateProcess you must ReleaseBuffer()
****
****
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory (&si, sizeof ( si));
ZeroMemory (&pi, sizeof ( pi));
si.cb = sizeof ( STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
DWORD bRes = CreateProcess ( NULL, szCmd, NULL, NULL, TRUE,
NORMAL_PRIORITY_CLASS,
GetEnvironmentStrings (), NULL, &si, &pi);
PrintDebug(_T("%s res[%d]"), szCmd, bRes);
(a) why are you waiting if the process didn't start? Why are you not testing for failure?
(b) why are you not calling ::GetLastError()?
Without knowing the reason for the error, you have no idea why it failed. It could even
be some issue of Vista security. But without the precise reason being given, there is no
way to tell.
****
WaitForSingleObject ( pi.hProcess, INFINITE);****
If this is in your main GUI thread, waiting for the process kills the GUI until the
process finishes. See my essay on asynchronous process notification on my MVP Tips site
****
CloseHandle( pi.hProcess);****
CloseHandle( pi.hThread);
If the process did not start, these operations are meaningless and must not be done
(CloseHandle has the undocumented feature that it throws an exception when given an
illegal handle). Essentially, you MUST ALWAYS assume that operations like CreateProcess
will fail, and handle such situations.
*****
Joseph M. Newcomer [MVP]
return ( bRes);
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: CreateProcess not work on Vista
- From: Duy Trinh
- Re: CreateProcess not work on Vista
- References:
- CreateProcess not work on Vista
- From: Duy Trinh
- CreateProcess not work on Vista
- Prev by Date: Re: CString to double conversion
- Next by Date: Re: difference between winsock.h and afxsock.h?
- Previous by thread: Re: CreateProcess not work on Vista
- Next by thread: Re: CreateProcess not work on Vista
- Index(es):
Relevant Pages
|