Recent

Author Topic: Shell Execute  (Read 17527 times)

Zath

  • Sr. Member
  • ****
  • Posts: 391
Shell Execute
« on: August 04, 2014, 01:14:03 pm »
How do I allow the exe to see the ritem.dat file in the shell execute statement ?

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  if ShellExecute(0,nil, PChar('"D:\SomeDir\abc.exe"'),PChar('"D:\SomeDir\Data\ritem.dat"'),nil,1) =0 then;
end;

Doubleclicking the abc.exe on its own and it runs fine, calling it with this method and it shows Data\ritem.dat can't be found.

I must say the error is called by abc.exe and not Lazarus.

Thanks

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Shell Execute
« Reply #1 on: August 04, 2014, 01:43:58 pm »
No idea, but you might need to escape the \ with another \\
Code: [Select]
"D:\\SomeDir\\Data\\ritem.dat"
Just a wild guess.
Or you don't need the " marks

It's presumably documented in the shellexecute reference on MSDN...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12269
  • FPC developer.
Re: Shell Execute
« Reply #2 on: August 04, 2014, 02:06:08 pm »
No, certainly not. \\ is a Unix disease, not windows.

Maybe give up a working directory, but I think it is best to try to characterize abc.exe's behaviour first, by running it in various circumstances.

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Shell Execute
« Reply #3 on: August 04, 2014, 02:21:19 pm »
No idea, but you might need to escape the \ with another \\
Code: [Select]
"D:\\SomeDir\\Data\\ritem.dat"
Just a wild guess.
Or you don't need the " marks

It's presumably documented in the shellexecute reference on MSDN...

Thanks but no joy. Extra \ and single quotes etc. all to no avail.
I'm using the example off the Wiki.
The MSDN site didn't help me much but I'll look again.

« Last Edit: August 04, 2014, 02:42:33 pm by Zath »

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Shell Execute
« Reply #4 on: August 04, 2014, 03:50:50 pm »
try to use without double quotes.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Shell Execute
« Reply #5 on: August 04, 2014, 04:37:04 pm »
try to use without double quotes.

I've tried all that thanks, no joy.

Basile B.

  • Guest
Re: Shell Execute
« Reply #6 on: August 04, 2014, 05:23:55 pm »
Hi, you can try with a TProcess, it'll work fine:
Code: [Select]
MyProcess.Executable := 'D:\SomeDir\abc.exe';
MyProcess.Parameters.Add('D:\SomeDir\Data\ritem.dat');
MyProcess.Execute;
while MyProcess.Running do (*nothing*);
if MyProcess.ExitCode <> 0 then
begin
 // bla bla error this error that...
end;

You don't need to double quote the filename, TProcess internally do the job if there is some spaces in a parameter.

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: Shell Execute
« Reply #7 on: August 04, 2014, 07:19:52 pm »
Fiercely stroking my magic orb... waving my magic wand and keeping my tongue at the right angle....

The problem is with abc.exe obviously.
It tries to search for a folder Data in the current directory; it ignores the full path of the parameter you are giving.
You should include the run-directory in your call to shellexecute to circumvent the error in abc.exe:
Code: [Select]
if ShellExecute(0,nil, PChar('D:\SomeDir\abc.exe'),
                       PChar('D:\SomeDir\Data\ritem.dat'),
                       PChar('D:\SomeDir'),
                       1) = 0 then     
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Shell Execute
« Reply #8 on: August 05, 2014, 12:11:43 am »
Thanks Basile B but I didn't try yours as I was a little unsure how to proceed with TProcess and eny's was close to what I had established.

Running my project in the same directory worked without any problem so I was thinking back in DOS days of CHDIR / CD.

Eny's solution was spot on.

Thanks everyone for all the input.

 

TinyPortal © 2005-2018