5

I have a Delphi application that uses ShellExecute to call a second Delphi Application on a button press.

The applications are stored on the same server, on the same network share. Their paths are in the format:

const
   JobManager = 'Z:\Apps\Application 1\Application1.exe';
   FeeManager = 'Z:\Apps\Application 2\Application2.exe';

The call to ShellExecute is made as follows:

rh := FindWindow(PChar('TMF'), PChar('Edit Job Details'));
if rh = 0 then
begin
   ShellExecute(Handle, 'open', JobManager, nil, nil, SW_SHOWNORMAL);
   ... 

As we have three office we have copies of the Apps folder on each office server. Each server has the Apps folder on a share mapped to "Z:"

In one of the offices we have discovered a problem where the Applications cannot be found if the paths contain spaces. As the applications are straight copies of each other, and work in the other offices the problem seems to be a machine setting.

Any ideas?

Dan Kelly
  • 2,634
  • 5
  • 41
  • 61
  • 1
    Are you tried using double quotes? `ShellExecute(0, nil, PChar('"Z:\Apps\Application 1\Application1.exe"'), nil, nil, 0);` – RRUZ Oct 16 '12 at 03:16
  • 4
    Show the code, and include the values of any variables passed to ShellExecute. – David Heffernan Oct 16 '12 at 06:29
  • @RRUZ- We've tried adding quotes around the constant – Dan Kelly Oct 16 '12 at 08:30
  • It is very likely that in your (good) reduction of the problem to fit the Q&A format you have edited out some essential details. How exactly did you conclude that on that particular machine "spaces in the path" are the difference between working / not working? I find that suspect, can you confirm this with a trivial test app? Also, what does filemon say? – Paul-Jan Oct 16 '12 at 09:16
  • 1
    @Paul-Jan It's been tested by a quick recompile to remove the spaces and a rename of the affected folder on the problem machine. This worked, but isn't a long term solution. We haven't ascertained it this is a single machine or branch office problem. – Dan Kelly Oct 16 '12 at 09:59
  • 4
    ShellExecute returns an error code when it fails, the code does not seem to check it. – Sertac Akyuz Oct 16 '12 at 11:05
  • The fault is probably with the target app rather than shell execute call. – David Heffernan Oct 16 '12 at 12:25
  • @SertacAkyuz Do you have any details about ShellExecute errors? – Dan Kelly Oct 16 '12 at 12:44
  • @Dan - See 'return value' section in the [documentation](http://msdn.microsoft.com/en-us/library/bb762153%28v=vs.85%29.aspx). – Sertac Akyuz Oct 16 '12 at 12:46
  • 3
    In your parameters. Try using 0 as Handle, `nil` instead of `open` (default) and `PChar(JobManager)`. double quotes around are not needed at all AFAIK. – kobik Oct 16 '12 at 14:07
  • 1
    @Kobik looks like the PChar around the JobManager was the issue - can you set that as an answer so I can accept it? On to the second issue... – Dan Kelly Oct 16 '12 at 15:31
  • @Dan - That doesn't make any sense. [Documentation](http://docwiki.embarcadero.com/RADStudio/XE3/en/String_Types#Using_Pointers.2C_Arrays.2C_and_String_Constants): *"You can also pass string constants to any function that takes value or const parameters of type PChar or PWideChar.."*. I believe you either posted fake code, or your problem lay elsewhere. – Sertac Akyuz Oct 16 '12 at 15:57
  • @Sertac For the majority of our PCs passing the JobManager constant without calling PChar works. For this one office (one user?) PChar seems to be the solution, whilst not breaking the other users. – Dan Kelly Oct 16 '12 at 16:05
  • @Dan - Looking at the CPU window, a PChar cast around a string literal looks like effectively a nop. In any case, glad you've resolved it. – Sertac Akyuz Oct 16 '12 at 16:09

2 Answers2

5

With your lpFile parameter you should cast JobManager as PChar:

ShellExecute(Handle, 'open', PChar(JobManager), nil, nil, SW_SHOWNORMAL);

Note that the open verb parameter is also not needed, and you could pass nil with the lpOperation parameter (default).

kobik
  • 21,001
  • 4
  • 61
  • 121
0

It works with double quotes:

WinExec(PAnsiChar(AnsiString(ExtractFilePath(application.ExeName) + '\winrar.exe A  "c:\BACKUP 2016\backup_"' .....
Brian
  • 14,610
  • 7
  • 35
  • 43
JeffPower
  • 11
  • 1