Jump to content
azrael_11

Shellexecute cmd.exe with spaces

Recommended Posts

Hello..

 

When i execute this command

var
	program_path: string;
begin
program_path:= c:/myprogram.exe

ShellExecute(0, nil, 'cmd.exe', PChar('/C ' + program_path+ ' -fg'), PChar(program_path), SW_HIDE);

 

but when the program_path have a space like a "New folder"

var
	program_path: string;
begin
program_path:= c:/New Folder/myprogram.exe

ShellExecute(0, nil, 'cmd.exe', PChar('/C ' + program_path+ ' -fg'), PChar(program_path), SW_HIDE);

Then nothing happens . I try this

ShellExecute(0, nil, 'cmd.exe', PChar(AnsiQuotedStr('/C ' + program_path+ ' -fg', Char(34))), PChar(program_path), SW_HIDE);

Nothing.

Share this post


Link to post
34 minutes ago, dummzeuch said:

You should only quote the program path rather than the whole command line.

I try this

ShellExecute(0, nil, 'cmd.exe', PChar(AnsiQuotedStr('/C ' + program_path, Char(34))+ ' -fg'), PChar(program_path), SW_HIDE);

Nothing, not working.

 

Share this post


Link to post
2 hours ago, azrael_11 said:

I try this


ShellExecute(0, nil, 'cmd.exe', PChar(AnsiQuotedStr('/C ' + program_path, Char(34))+ ' -fg'), PChar(program_path), SW_HIDE);

Nothing, not working.

 

You quoted the whole command line, not just the program path.

 

This is what I meant:

 

ShellExecute(0, nil, 'cmd.exe', PChar('/C ' + AnsiQuotedStr(program_path, Char(34))+ ' -fg'), PChar(program_path), SW_HIDE);
 
Edited by dummzeuch

Share this post


Link to post
41 minutes ago, dummzeuch said:

You quoted the whole command line, not just the program path.

 

This is what I meant:

 


ShellExecute(0, nil, 'cmd.exe', PChar('/C ' + AnsiQuotedStr(program_path, Char(34))+ ' -fg'), PChar(program_path), SW_HIDE);

 

 

Like
  •  

That work just fine.

 

Thank you.

Share this post


Link to post

Pointless to ask ShellExecute to create a cmd process to in turn create another process. Create the other process directly. This is the source of all your problems. 

  • Like 3

Share this post


Link to post
On 12/9/2018 at 5:57 PM, David Heffernan said:

Pointless to ask ShellExecute to create a cmd process to in turn create another process. Create the other process directly. This is the source of all your problems. 

I think you are right about this so i make a process to call that...

 

Thank you.

Share this post


Link to post
1 hour ago, azrael_11 said:

I think you are right about this so i make a process to call that...

 

Thank you.

Surely you just call CreateProcess directly

  • Like 1

Share this post


Link to post
On 12/9/2018 at 4:57 PM, David Heffernan said:

Pointless to ask ShellExecute to create a cmd process to in turn create another process. Create the other process directly. This is the source of all your problems. 

... unless you need to use pipes, environment variables or want to capture the output of a command line program into a file. Then cmd.exe is a real life saver for which there is no real alternative as far as I'm aware.

 

I had precisely that case last week.  In an installation routine (inno Setup) I needed to figure out if a set of Microsoft IIS components was installed correctly before allowing the user to continue the installation of my ISAPI webservice.  The following one-liner gives that information sorted in a text file. 

 

cmd.exe /s /c dism /online /Get-Features /Format:table /English | sort >%tmp%\dismfeatures.txt

 

 

 

(Note that on 64-bit systems,  this particular example requires calling the 64-bit version of cmd.exe;  The 32-bit version of cmd.exe will call the 32-bit version of dism.exe which is totally non-functional on 64-bit operating systems. Google "Wow64DisableWow64FsRedirection" for more info). 

Share this post


Link to post

But none of that complexity is present here. And even if it was, then you'd still use CreateProcess. ShellExecuteEx would have value with the runas verb only. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×