Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Launching Windows programs from a C++ program

Hi,
I asked a question a while ago that was answered, but I have found that I can only launch programs that can be launched from a MS-DOS prompt. My question is how do you launch Win programs from a C++ program? Like, for example, how would you launch Excel from your program.

Thanks.
Ian.
0
ianmatchett
Asked:
ianmatchett
  • 3
1 Solution
 
mnashadkaCommented:
To open files in, for example, you can use ShellExecute; to launch processes you can use CreateProcess.
0
 
mnashadkaCommented:
Sorry, I meant "in Excel, for example"
0
 
mnashadkaCommented:
ShellExecute(NULL, NULL, "C:\\Program Files\\Microsoft Office\\Office\\Excel.exe", "C:\\Program Files\\Microsoft Office\\Office\\Excel.exe c:\\test.xls", NULL, SW_SHOW); will open the file test.xls in Excel.exe.
0
 
RattleSnakeCommented:
The following function "Execute" uses the CreateProcess function to call a program by a command line string. Therefor, you will have to quote the path to the executable if it contains whitespaces, as sampled in the main() routine I copied as well. The boolean return value indicates the success of CreateProcess, but I didn't check it here because ... well because it works ...


BOOL Execute(LPCSTR strPath)
{
     PROCESS_INFORMATION pInfo;
     STARTUPINFO sInfo;
     
     GetStartupInfo(&sInfo);

     return CreateProcess(NULL,(char*) strPath,
          NULL,NULL,TRUE,NORMAL_PRIORITY_CLASS,
          NULL,NULL,&sInfo,&pInfo);
}


int main (int argc, char **argv)
{
     Execute("\"c:\\winnt\\system32\\ping.exe\" www.google.com"); // path to ping.exe quoted
     return 0;
}


0
 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: mnashadka

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now