Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-01

If ShellExecute(0, 'open', PChar(ExecuteName), '', '', 0) <= 32 Then

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

vmars

Thanks in advance!

[Any ideas why this works:

  ExecuteName := (AppPathExe + 'Del2Pas.bat.lnk');
  ExecuteShell;   ]
[But this doesn't work:
  ExecuteName := (AppPathExe + 'Del2Pas.bat');
  ExecuteShell;
And doesn't give an error msg.]
procedure TForm1.ExecuteShell;
begin
//    ShowMessage('ExecuteShell = ' + ExecuteName);
    If ShellExecute(0, 'open', PChar(ExecuteName), '', '', 0)  <= 32 Then
         MessageDlg('Unable to open file: ' + ExecuteName + ' ErrorCode= ' +
IntToStr(0), mtError,
             [mbOk], 0);
end; // procedure TForm1.ExecuteShell

ô¿ô
V e r n

WinXp sp2 , Delphi5, WebDwarf, Trellian WebPage,
Lazarus-0.9.26-fpc-2.2.2-win32.exe,
wxPython2.8-win32-unicode-2.8.9.1-py26.exe , Boa 0.6.1 http://www.flickr.com/photos/vmars956/

Wanted: Dwarf Hibiscus (Orange/Rasberry)

Vinzent Höfler
to vmars

-------- Original-Nachricht --------

Show quoted text 16 lines
Datum: Mon, 26 Jan 2009 17:42:23 -0600 Von: "vmars" An: Betreff: [fpc-pascal] If ShellExecute(0, \'open\', PChar(ExecuteName), \'\', \'\', 0) <= 32 Then Thanks in advance! [Any ideas why this works: ExecuteName := (AppPathExe + 'Del2Pas.bat.lnk'); ExecuteShell; ] [But this doesn't work: ExecuteName := (AppPathExe + 'Del2Pas.bat'); ExecuteShell; And doesn't give an error msg.]

Just trying an educated guess here, but I suppose .bat-files can only be executed via command.com (cmd.exe). Something like "%COMSPEC% /c Del2Pas.bat" could do the trick.

Vinzent.

Psssst! Schon vom neuen GMX MultiMessenger gehört? Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger
Bart
to Vinzent Höfler

Is the problem that you don't see output from del2pas.bat? or is it not executed at all (and how do you know this)?

If the problem is the first, that may be so because in a .lnk you specify that the file needs a console, and just running the .bat ma prevent it frome getting a console?
Put in your del2pas.bat a line like starts a GUI program like notepad.exe and see if it is launched, that would tell you definitely if the problem is having assigned a console or not.

(Just a wild guess)

Bart

vmars
to Vinzent Höfler: -------- Original-Nachricht -------- Just trying an educated guess here, but I suppose…
I tried many combinations of   "%COMSPEC% /c Del2Pas.bat"
            ExecuteName := ('C:\\WINDOWS\\system32\\cmd.exe ' + AppPathExe
+ 'Del2Pas.bat');
            ExecuteName := ('"%COMSPEC%/c Del2Pas.bat"');
            and many more!
but still:

Unable to open file: "%COMSPEC%/c Del2Pas.bat" ErrorCode=0

Help...

ô¿ô
V e r n

WinXp sp2 , Delphi5, WebDwarf, Trellian WebPage,
Lazarus-0.9.26-fpc-2.2.2-win32.exe,
wxPython2.8-win32-unicode-2.8.9.1-py26.exe , Boa 0.6.1 http://www.flickr.com/photos/vmars956/

Wanted: Dwarf Hibiscus (Orange/Rasberry)

Earlier message 42 lines
----- Original Message ----- From: ""Vinzent Höfler"" To: "FPC-Pascal users discussions" Sent: Monday, January 26, 2009 11:53 PM Subject: Re: [fpc-pascal] If ShellExecute(0, 'open', PChar(ExecuteName), '','', 0) <= 32 Then > > -------- Original-Nachricht -------- >> Datum: Mon, 26 Jan 2009 17:42:23 -0600 >> Von: "vmars" >> An: >> Betreff: [fpc-pascal] If ShellExecute(0, \'open\', PChar(ExecuteName), >> \'\', \'\', 0) <= 32 Then > >> Thanks in advance! >> >> [Any ideas why this works: >> ExecuteName := (AppPathExe + 'Del2Pas.bat.lnk'); >> ExecuteShell; ] >> >> >> [But this doesn't work: >> ExecuteName := (AppPathExe + 'Del2Pas.bat'); >> ExecuteShell; >> And doesn't give an error msg.] > > Just trying an educated guess here, but I suppose .bat-files can only be > executed via command.com (cmd.exe). Something like "%COMSPEC% /c > Del2Pas.bat" could do the trick. > > > Vinzent. > > -- > Psssst! Schon vom neuen GMX MultiMessenger gehört? Der kann`s mit allen: > http://www.gmx.net/de/go/multimessenger > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal >
vmars
to Bart: Is the problem that you don't see output from del2pas.bat? or is it not executed at all…

Wild is good, but same results:

Unable to open file: "%COMSPEC%/c Del2Pas.bat" ErrorCode=0

ô¿ô
V e r n

WinXp sp2 , Delphi5, WebDwarf, Trellian WebPage,
Lazarus-0.9.26-fpc-2.2.2-win32.exe,
wxPython2.8-win32-unicode-2.8.9.1-py26.exe , Boa 0.6.1 http://www.flickr.com/photos/vmars956/

Wanted: Dwarf Hibiscus (Orange/Rasberry)

Earlier message 26 lines
----- Original Message ----- From: "Bart" To: "FPC-Pascal users discussions" Sent: Tuesday, January 27, 2009 8:00 AM Subject: Re: [fpc-pascal] If ShellExecute(0, 'open', PChar(ExecuteName), '', '', 0) <= 32 Then > Is the problem that you don't see output from del2pas.bat? or is it > not executed at all (and how do you know this)? > > > If the problem is the first, that may be so because in a .lnk you > specify that the file needs a console, and just running the .bat ma > prevent it frome getting a console? > Put in your del2pas.bat a line like starts a GUI program like > notepad.exe and see if it is launched, that would tell you definitely > if the problem is having assigned a console or not. > > (Just a wild guess) > > Bart > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal >
Jonas Maebe
to vmars
vmars wrote
Wild is good, but same results: Unable to open file: "%COMSPEC%/c Del2Pas.bat" ErrorCode=0

You have to resolve %COMSPEC% first (it's an environment variable).

Jonas

Henry Vermaak
to vmars: I tried many combinations of "%COMSPEC% /c Del2Pas.bat" ExecuteName :=…

2009/1/28 vmars :

Show quoted text 8 lines
I tried many combinations of "%COMSPEC% /c Del2Pas.bat" ExecuteName := ('C:\\WINDOWS\\system32\\cmd.exe ' + AppPathExe + 'Del2Pas.bat'); ExecuteName := ('"%COMSPEC%/c Del2Pas.bat"'); and many more! but still: Unable to open file: "%COMSPEC%/c Del2Pas.bat" ErrorCode=0

the shell is responsible for resolving environment variables, so you need to retrieve comspec yourself with "GetEnvironmentVariable" in sysutils.

henry

Bart
to Jonas Maebe: You have to resolve %COMSPEC% first (it's an environment variable). Jonas
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);
var
  Res: LongWord;
begin
  Res := ShellExecute(0, nil, PChar('test.bat'),nil,nil,SW_SHOWNORMAL);
  DebugLn('Res = ',DbgS(Res));
end;

Here's the output:

F:\LazarusProjecten>test
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.

Bart

vmars
to Bart

OK. I finally got there, via much trial and error.

*
ExecuteName := GetEnvironmentVariable('COMSPEC');
{ExecuteName  =   'C:\WINDOWS\system32\cmd.exe'}
ShellExParms := (' /c ' + ExecuteName) ;

If ShellExecute(0, nil, PChar(ExecuteName), PChar(ShellExParms), nil, SW_SHOWNORMAL) <= 32 Then
MessageDlg('Unable to open file: ' + ExecuteName + ' ErrorCode= ' + IntToStr(0), mtError,

             [mbOk], 0);

*

Thanks all !

ô¿ô
V e r n

WinXp sp2 , Delphi5, WebDwarf, Trellian WebPage,
Lazarus-0.9.26-fpc-2.2.2-win32.exe,
wxPython2.8-win32-unicode-2.8.9.1-py26.exe , Boa 0.6.1 http://www.flickr.com/photos/vmars956/

Wanted: Dwarf Hibiscus (Orange/Rasberry)

Earlier message 63 lines
----- Original Message ----- From: "Bart" To: "FPC-Pascal users discussions" Sent: Wednesday, January 28, 2009 4:30 AM Subject: Re: [fpc-pascal] If ShellExecute(0, 'open', PChar(ExecuteName), '', '', 0) <= 32 Then >> 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); > var > Res: LongWord; > begin > Res := ShellExecute(0, nil, PChar('test.bat'),nil,nil,SW_SHOWNORMAL); > DebugLn('Res = ',DbgS(Res)); > end; > > Here's the output: > > F:\LazarusProjecten>test > 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. > > Bart > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal >
Bart
to vmars

And now try this withou the use of COMSPEC, and it should work. The problem is the implicit SW_HIDE (0) you were using.

Bart