简体   繁体   中英

Execute another program in C++

I want to remotely execute another application from my C++ program. So far I played along with the CreateProcess(...) function and it works just fine.

The problem however is that I need the full path of the other program but I do not know the directory of it. So what I want is that I just have to enter the name of the other program, like when you type "cmd" or "winword" into Run... it opens the corresponding programs.

Thanks in advance, Russo

If you are using CreateProcess like this:

CreateProcessA( "winword.exe", .... );

then the PATH variable will not be used. You need to use the second parameter:

CreateProcessA( NULL, "winword.exe", .... );

See http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx for details.

You're looking for ShellExecute() . That will even work if you pass it a proper URL, just like the Run menu.

Launching programs and counting on PATH in any way is considered insecure coding. System PATHs may get polluted with locations that aren't secured properly such as a network drive. The best way to launch an application is to launch the executable from exactly where it stands and set the CWD to the location of the executable as installed. Otherwise you could be launching malicious code.

Most likely some combination of information from here will help get the location correctly: Detecting installed programs via registry

Greg

The directories of the programs you can run from start -> run are added to the PATH variable. You can add the folder your program is to the PATH and then use CreateProcess(). However, you say you don't know the directory, so you probably can't do this.

Do you know a partial path? For example, do you know that your exe will always be in C:\\something\\something\\ or a subfolder of this path? If so, look up FindFirst() and FindNext() to list all the files in that directory and search for your exe, then use CreateProcess() when you find your exe.

http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx shows how to list files in a directory. You will have to modify it to also search subdirectories (for example, make a recursive function).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM