[fpc-pascal] If ShellExecute(0, 'open', PChar(ExecuteName), '', '', 0) <= 32 Then

Bart bartjunk64 at gmail.com
Wed Jan 28 11:30:17 CET 2009

> Wild is good, but same results:
> Unable to open file: "%COMSPEC%/c Del2Pas.bat" ErrorCode=0

Did you try invoking notepad.exe from your batchfile?
I never suggested to use %comspec%.
(And as others pointed out you need to evaluate this with something
like: ComspecStr := GetEnvironmentVariable('COMSPEC') (not tested))

My guess was that the process was executed but was not shown on the
desktop, which lead you to conclude it was never executed.

Ok, the last parameter in ShellExecute determines the way the
application is shown.
You specify 0, that is SW_HIDE, so you will not see the app!
If the batchfile requires interaction from the user you will not see
it and the batchtfile will never terminate.

A returnvalue of 0 from ShellExecute means: The operating system is
out of memory or resources.
For batchfiles (and any other executable) you need not specify 'open',
nil will suffice quite nicely in this case.

There is no need for %comspec% (I'm not even sure it exists in NT
based windows).

Here's my testcase:

procedure TForm1.Button1Click(Sender: TObject);
  Res: LongWord;
  Res := ShellExecute(0, nil, PChar('test.bat'),nil,nil,SW_SHOWNORMAL);
  DebugLn('Res = ',DbgS(Res));

Here's the output:

Res = 16806

and a new window appears which shows me the output of my test.bat

If I put 0 instead of SW_SHOWNORMAL then I never get to see the output
of my batchfile, but it does run (I can see that because I let it
write to soem file which I examine later).

Tested with Lazarus 0.9.27 rev. 18450 / fpc 2.2.2

Hope this helps.


More information about the fpc-pascal mailing list