Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch does not do what it should when called from ShellExecute

Status
Not open for further replies.

digitalgerrit

Technical User
Feb 7, 2007
14
NL
Hi there,

I call a batch using ShellExecute using the next lines of code:

if ShellExecute(0, nil, PChar('d:\RNH\zd.bat'), nil, nil, SW_NORMAL) <= 32 then
begin
label4.caption := 'error';
end;

When the batch is tested from the command prompt or from windows it does what it should. When I test it from Delphi, the returnvalue is good, when I test for a value of >32 and fill the label4 with OK it does. So it seems to function like this. But:

The batch transforms an .xml file using the msxsl.exe processor and gives back the result in an output file. But using the code above does not generate the outputfile. I think that maybe it is some kind of delay problem: when called from the command prompt or the windows explorer you see the DOS interface flashing for a brief moment, and 2 or 3 seconds later the file appears. From the Delphi code you only see the DOS interface flashing, you don't see no file appearing, so I thought that maybe it's some kind of time-out problem, maybe Delphi takes over control again before the batch process had a chance to generate the output file.

Does anyone have an idea how to resolve this one?

Any help will be greatly appreciated.

Thanks.
 
I'm not fully sure how Windows NT/XP/etc is with such a call, but I remember in the DOS days that you could only run (shell to program) an actual program with an exec call and had to pass a call to the command-line interpreter in order to have it run the batch file.

Something like:

COMMAND.COM ZD.BAT

I don't know if that's what it is, but you can most certainly try it.
 
I have run Batch file like this from Delphi. If the problem is timing related, use Execute and wait. see faq102-6048 for an example

Steve [The sane]: Delphi a feersum engin indeed.
 
Hi Glenn9999,
I tried, but unfortunately my attempt failed. Same result. Thanks anyway!
Sggaunt, looks good, I'm certainly going to try it out!

 
okay, I just had a look at the problem a little more deeply. I forgot to mention the command-parm you need if you call it through the interpreter (you don't have to, I found out).

COMMAND.COM /C ZD.BAT

I'm not sure the time-sensitive part of things matters (it might), but depending on what system you run it on, either way should be fine.

If sggaunt's idea doesn't work out, I can go ahead and post the execution code I've been using under Delphi.
 
Code:
    rc:= ShellExecute(0, 'open', pchar('cmd.exe'), pchar('d:\RNH\zd.bat'), NIL, SW_SHOWNORMAL);
    if  rc <= 32 then begin
      //handle error...
    end;
 
You may need to add the "/C":
Code:
  rc:= ShellExecute(0, 'open', pchar('cmd.exe'), pchar('/C d:\RNH\zd.bat'), NIL, SW_SHOWNORMAL);
    if  rc <= 32 then begin
      //handle error...
    end;
 
Hi There,

I tried every suggestion, unfortunately I did not succeed. Now I understand why, I seem to have missed a quite simple point. In the batch file I did not include any reference to the path of the msxls executable or the output file. I did not think this was necessary because the batchfile did what it had to do when activated from Dos or Windows. Apparently Delphi needed the exact filepaths of the executable and the output file (remarkably enough not of the .XML input file or the .XSLT stylesheet). So now it works.

Thanks anyway for all your suggestions, I really learnt a lot about some advanced Delphi programming constructs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top