0

I'm trying:

cmdline := 'I:\test.exe';
Result := ShellExecute(0,nil, PChar(cmdline),PChar(''),nil,1)

Where I:\ is a network drive. This would give me the error: 2 - file not found

If I try:

cmdline := '\\10.10.10.10\data\test.exe';
Result := ShellExecute(0,nil, PChar(cmdline),PChar(''),nil,1)

Gives error: 5 - Access denied

So I guess ShellExecute does not get the mapped network drive, nor the credentials from the logged on user.

So how do I get ShellExecute to execute the command in the current user environment? Or more likely: What obvious thing am I missing?

Hoping @RBA gave me the obvious thing I have now tried:

FillChar( StartupInfo, SizeOf( TStartupInfo ), 0 );
StartupInfo.cb := SizeOf( TStartupInfo );
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := sw_Normal;
FillChar( ProcessInfo, SizeOf( TProcessInformation ), 0 );

if CreateProcess(pchar(cmdline), Nil, Nil, Nil,
                 False, CREATE_NEW_CONSOLE, Nil, Nil, 
                 StartupInfo, ProcessInfo) then
  result := true
else
  result := false; 

But still no exe launched :( Same error: 2 - file not found. Makes me believe that there is something other than the code that is playing me a trick...

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Ove Halseth
  • 431
  • 5
  • 13
  • 2
    You shouldn't be using ShellExecute() to launch an .exe file anyway. You should be using CreateProcess() instead – RBA Aug 11 '17 at 09:08
  • So I googled and tried CreateProcess and still unable to launch the exe on the mapped drive. (I'll update my question with the CreateProcess example) – Ove Halseth Aug 11 '17 at 09:25
  • @RBA whats wrong with using ShellExecute() to launch an exe if I may ask – Alec Aug 11 '17 at 09:55
  • @Fero - a simple google search provides a lot of answers to your question. Take a look at https://stackoverflow.com/questions/10747479/createprocess-and-shellexec-differences and – RBA Aug 11 '17 at 09:58
  • @RBA, I get that, but that does not explain why its wrong to use ShellExecute() in this context – Alec Aug 11 '17 at 10:02
  • @Fero - If you understood what that post says, than you should consider that ShellExecute shouldn't be used on any context, not in this particular one. It is obvious that nobody will stop you from using it. It's your choice – RBA Aug 11 '17 at 10:06
  • Frustrating that you tag as Delphi in spite of using Lazarus. Details matter. – David Heffernan Aug 11 '17 at 10:53
  • @DavidHeffernan Is it not relevant for delphi programmers also? I haven't tried it in delphi, but I thought the code would compile in delphi too... – Ove Halseth Aug 14 '17 at 06:14
  • Text encoding matters and Lazarus and Delphi handle that differently. – David Heffernan Aug 14 '17 at 06:16

1 Answers1

0

I finally found the obvious. The path given in the example above is just an example. The real path is:

I:\Prosjekt\Læringssenteret\VgFriskoleIndivid\Program\utvOve\WisKrypt\Install

And the problem is the norwegian character: æ But that does not explain why i got error 5 - access denied when trying UNC path...

Ove Halseth
  • 431
  • 5
  • 13