?
Solved

Launching Windows programs from a C++ program

Posted on 2003-03-26
6
Medium Priority
?
208 Views
Last Modified: 2010-04-01
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
Comment
Question by:ianmatchett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 8210802
To open files in, for example, you can use ShellExecute; to launch processes you can use CreateProcess.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8210807
Sorry, I meant "in Excel, for example"
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 400 total points
ID: 8210823
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
 

Expert Comment

by:RattleSnake
ID: 8211851
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
 
LVL 9

Expert Comment

by:tinchos
ID: 9550815
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question