Skip to main content
gamedev.net
🔒 Locked

TellingWin32 to execute a program (not system() )

Started by krikkit Aug 15, 2006 at 6:52 PM 3 replies 1.1k views
Original Post
krikkit
krikkit
Hey folks, I'm in that "just learning windows API" phase(with c++), where its becoming plain that I know enough to get me in trouble but not enough to really accomplish anything I want to do:P At the moment, Im simply trying to tell windows to execute a program (that is specified by path) along with another file as an argument. Using system(), I can call the program with no arguments, ie

system("\"c:\\Documents and Settings\\etc\\whatever.exe\"");
to feed the command line the windows dir in quotes, and it launches. But if I want to pass the program another filename as an arg(as in open this file), system seems to break, ie

system("\"c:\\Documents and Settings\\etc\\whatever.exe\ c:\test.wtf\"");
Its almost as if the system drops the "'s around the string, and pukes up a " 'c:\Documents' is not a real command" Apart from all that, I know system() causes the process to wait until the executed program completes, and therefore is a horrible choice, and leaves me wondering what the simplest(oh I hope its simple) true Win32 method for just telling the OS to launch a program is. I've tried working it out via google or msdn, but generic terms like "win32 api shell execute program" return a very wide berth of results and I havent picked up a clue yet.
SiCrane
SiCrane
ShellExecute() is probably the simplest method, though you may need CreateProcess() if you want to get fancy with the way you start the program.
krikkit
krikkit
Ah, thanks for the hint. I'll check em out.
krikkit
krikkit
For posterity, ShellExecute() worked like a charm with an interesting twist.

Heres a quicky explanation I found on a website:
|ShellExecuteExecute program attached to the file:Declare://shell executeFunction long ShellExecute(long hwnd, string lpOperation , string lpFile , string lpParameters , string lpDirectory , long nShowCmd) Library "shell32.dll" Alias for "ShellExecuteA"  Execution:ShellExecute(Handle(parent), "open" , "iexplore" , "" , "" , 3 ) 


Intrigingly, lpFile can just have the path in it, ie "C:\Documents and Settings\Blah", but (and it makes sense if you think about it)

lpParameters needed the double-quote treatment, in my case, to deal with windows directories (so my target program knows how to deal with things, apparantly), ie
"\"C:\My File.wtf\"".

For anyone whos curious:)
Anon Mike
Anon Mike
It looks like you are running into the standard confusion about what work the shell does and what you have to do yourself. system() is probably deferring to a relatively low-level API (e.g. CreateProcess). You told it to run a process named "c:etc - i.e. the intial quote is take literally. It wants a file that starts with a quote and you don't have one.

On the other hand, if you were to type it directly into the shell (i.e. command prompt) it would parse what you type, strip out the quotes around the command name, and pass it along. That's why it works there.

You could probably make it work by using system() to run the shell and telling it to run your program in turn. e.g. system("cmd.exe /c \"c:etc\""). Actually to be technically correct you need to read COMSPEC and use that instead of hardcoding cmd.exe. Or you can just punt and use ShellExecute like you did which is far easier and you don't need to mess with quoting the command name.
-Mike

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.