I know this is probably painfully simple, but I need to know how to launch another app from within my program.
p.s. Is there a way to launch an EXE and grab the handle at the same time?
I know this is probably painfully simple, but I need to know how to launch another app from within my program.
p.s. Is there a way to launch an EXE and grab the handle at the same time?
Well, depending on how much control you want, you could either
or, since you are on WindowsCode:system("application-path");
Code:CreateProcess(...);
I hate real numbers.
Gah. I'm not getting this. CreateProcess has too many bloody arguments, and system() isn't working as desired. When I tried it by itself, it launched the program but the CMD window stayed and locked up the app that called it. I tried adding a /C to the system() call, but it's not working. Bottom line: I'm trying to start another program (just as if I went and double clicked on it, nothing special) and grab a handle to it for later (with PROCESS_ALL_ACCESS), preferably without having to enumerate all processes and look for it.
Code:// sprintf(tmpFileName, "/C %s", lFileName); sprintf(tmpFileName, "/C \"%s\"", lFileName); system(tmpFileName);
I don't think CreateProcess() gives you a handle to the process though, so you'd also need to call OpenProcess().
If you're having problems with system() & CreateProcess(), then maybe you should try one of the _spawn functions.
Before you pass it to system, try printing it out, like say
printf( "The command is -->%s<--\n", tmpFileName );
And then post your results here.
Yes, the %s is in the middle, so we can easily see if there's say an embedded newline or anything.
We are of course assuming at this point that tmpFileName is an array with enough space.
> CreateProcess has too many bloody arguments
Better get used to it, many functions do.
There's an example in the FAQ.
Many of the args can be NULL for default behaviour.
> p.s. Is there a way to launch an EXE and grab the handle at the same time?
Only if you use the Win32 API functions for creating a process.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Shows perfectly normal. I did sprintf(tmpFileName, "cmd /C \"%s\"", lFileName); and it still kept the damn CMD window open and froze my app as long as that window was open. I thought /C was supposed to execute the command and close the damn CMD window.
What FAQ? Is there one here or just that MSDN example? Searching the FAQ forum didn't turn up anything useful.
Try start.
Code:system("start notepad");
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
/C means that the command window closes when the command you passed in is finished. Since you don't tell us what that is, we can't really tell you how long that can be expected to take.
system() is synchronous, so it waits for the child process to finish before it returns.
As dwks suggests, a command to system like "start xxx" will start a new process running the "xxx" process, and almost immediately return to your current code.
Or you can use CreateProcess and have full control over the newly created process - yes it adds a bit more code (more arguments, but as stated above, most of theose are "NULL" or 0 for normal use).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
CreateProcess does give you a handle to the newly created process. The last argument of the function is a pointer to a PROCESS_INFORMATION structure.
I hate real numbers.
CreateProcess() is handy if you are trying to manipulate the child process. It is certainly safer than calling something like system("NOTEPAD.EXE"), which introduces potential security holes.
Ah. I think I have CreateProcess working now. I'll test it more once I get some other stuff sorted out.
It has a nasty number of parameters, however most of them can be zeroed. ShellExecute() isn't much better, but I have needed to use it a time or two before.