Re: CreateProcess not work on Vista

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.
*****

return ( bRes);

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: CreateProcess not work on Vista
    ... Error msg said that "System cannot find the file specified.", ... and after the CreateProcess you must ReleaseBuffer ... ZeroMemory (&si, sizeof (si)); ... Without knowing the reason for the error, you have no idea why it failed. ...
    (microsoft.public.vc.mfc)
  • Re: Coding inside the debugger
    ... but why isn't the test written so that the reason it fails is ... If something fails in tests like these, ... public void testCreateWithDefaultthrows Exception { ... Ghostworld world = worldWithClass; ...
    (comp.object)
  • Re: I need help please!
    ... It is documented on some systems I've use a lot as a standard way of exiting at any point. ... Any software written for general consumption should deal with unexpected keystrokes for the simple reason that, in the real world, they are very common. ... So you "REASONABLE man's method" fails on a very common system, ... I would say that a "solution" that does not work on a very common platform is NOT a reasonable solution. ...
    (comp.lang.c)
  • Re: more Ada.Directories questions
    ... it fails is not certain to work, so having a single routine to do the entire ... reasonably clear that Name_Error is tested first - for the whole string. ... and it is described as a test on the whole string. ... don't see any great reason to insist on it. ...
    (comp.lang.ada)
  • Re: what is wrong?
    ... The only reason you "should" get no output is if the int 21h/47h fails. ... After the "xlatb", ...
    (alt.lang.asm)