Execute another program in C++

13,463

Solution 1

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

Solution 2

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).

Solution 3

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

Share:
13,463
Admin
Author by

Admin

Updated on July 23, 2022

Comments

  • Admin
    Admin almost 2 years

    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