Execute another program in C++

前端 未结 4 1345
失恋的感觉
失恋的感觉 2020-12-18 02:09

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 ho

相关标签:
4条回答
  • 2020-12-18 02:31

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

    0 讨论(0)
  • 2020-12-18 02:33

    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

    0 讨论(0)
  • 2020-12-18 02:34

    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.

    0 讨论(0)
  • 2020-12-18 02:49

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

    0 讨论(0)
提交回复
热议问题