Run another C ++ program

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

However, the problem is that I need the full path to another program, but I do not know its directory. Therefore, I just want to enter the name of another program, for example, when you enter "cmd" or "winword" in Run ..., it opens the corresponding programs.

Thanks in advance, Russo

+7
c ++ windows visual-c ++ winapi
source share
4 answers

If you use CreateProcess as follows:

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 more details.

+8
source share

You are looking for ShellExecute() . This will even work if you give it the correct URL, just like on the Run menu.

+7
source share

Directories of programs that you can run from start → run are added to the PATH variable. You can add the folder where your program is located in PATH, and then use CreateProcess (). However, you say you do not know the directory, so you probably cannot do this.

Do you know a partial path? For example, did you know that your exe will always be in C: \ something \ something \ or a subfolder of this path? If so, find FindFirst () and FindNext () to list all the files in this directory and search for 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 change it to also look for subdirectories (e.g. make a recursive function).

+1
source share

Running programs and accounting for PATH is in any way considered unsafe coding. System PATHs may be contaminated with locations that are not properly protected, such as a network drive. The best way to run the application is to run the executable from where it stands, and install CWD in the place of the executable as installed. Otherwise, you may run malicious code.

Most likely, some combination of information here will help to determine the location correctly: Detection of installed programs through the registry

Greg

+1
source share

All Articles