Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

?system ("command"); -There is a

Status
Not open for further replies.

Polkadot

Technical User
Jan 23, 2002
8
GB
?system ("command");

-There is a C function that allows C to call another executeable as if from a command line prompt eg.

system("c:\type c:\autoexec.bat");

In C++ this will open a Dos window and type the autoexec. Note however that this doesn't work for windows .exe files.

Question? how can I get C++ to call another windows application in the same way as if I click on an icon or go to the Start menu click on run and type in Excel. (which in this instance will start up Excel) - You can contact me at Andrew.Carmichael@polkadotuk.demon.co.uk
 
I copied the text from somewhere else for you. Hope it helps.
***************************************************
There are several functions that run other programs. The simplest is WinExec():

WinExec ( "C:\\path\\to\\program.exe", SW_SHOWNORMAL );

There is also ShellExecute(), which can run executables as well as files that are associated with a program. For example, you can "run" a text file, as shown here:

ShellExecute ( hwndYourWindow, "open", "C:\\path\\to\\readme.txt", NULL, NULL, SW_SHOWNORMAL );
In this example, ShellExecute() looks up the program associated with .TXT files and runs that program. ShellExecute() also lets you set the program's starting directory and additional command line parameters. See the MSDN docs on ShellExecute() for more info.

If you want complete control over every aspect of the program launching process, use CreateProcess(). CreateProcess() has a ton of options, so see MSDN for all the details. Here is a simple example:

STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi = {0};
BOOL bSuccess;

bSuccess = CreateProcess ( NULL, "\"C:\\Program Files\\dir\\program.exe\"",
NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
NULL, NULL, &si, &pi );
Note that the program name should be enclosed in quotes, as shown above, if the path contains spaces.

If CreateProcess() succeeds, be sure to close the handles in the PROCESS_INFORMATION structure once you don't need them anymore.

CloseHandle ( pi.hThread );
CloseHandle ( pi.hProcess );
Of course, if all you need to do is just run a program, CreateProcess() is probably overkill, and ShellExecute() would be sufficient.


 
Thanks for your help which I have found very helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top