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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to run other program from application

Status
Not open for further replies.

mironto

Programmer
May 12, 2001
26
SK
hi everybody,
I'd like to know, how to make something like command line in c++ builder that would work same way as command line in windows: when you type programXY.exe I want it to launch that program and when typing documentXY.doc or indexXY.htm to launch word or explorer.
thanks for tips
 
You will want to look at ShellExecute and ShellExecuteEx.
ShellExecute works like this:
Code:
HINSTANCE ShellExecute(
    HWND hwnd,	// handle to parent window
    LPCTSTR lpOperation,	// pointer to string that specifies operation to perform
    LPCTSTR lpFile,	// pointer to filename or folder name string
    LPCTSTR lpParameters,	// pointer to string that specifies executable-file parameters 
    LPCTSTR lpDirectory,	// pointer to string that specifies default directory
    INT nShowCmd 	// whether file is shown when opened
   );
lpOperation can be "open", "explore", "print", or null. lpFile is the name of the file. lpParameters can be parameters being passed to lpFile if it is an executable or it can be null. lpDirectory is a character string (null terminated) of the default directory. nShowCmd is a command that tells how the file is to be displayed. I usually use SW_SHOWNORMAL.

For example, to look at a file with my favorite editor, I would use:
Code:
ShellExecute(NULL, NULL, "E:\\MEW\\MEW32.EXE", FileName.c_str(), "H:\\TMP", SW_SHOWNORMAL);

I would have to make sure that "E:\MEW\MEW32.EXE" existed before I executed this command. To open the file with the default program, I would use:
Code:
ShellExecute(NULL, NULL, FileName.c_str(), NULL, "H:\\TMP", SW_SHOWNORMAL);


For more information you should look at MS's Knowledge Base or MSDN.

James P. Cottingham
 
You can also use WinExec("command line here",int x);
the second parameter can be 0. John Fill
1c.bmp


ivfmd@mail.md
 
Best function to use is the Windows API

BOOL CreateProcess(

LPCTSTR lpApplicationName, // pointer to name of executable module
LPTSTR lpCommandLine, // pointer to command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // pointer to process security attributes
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes
BOOL bInheritHandles, // handle inheritance flag
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // pointer to new environment block
LPCTSTR lpCurrentDirectory, // pointer to current directory name
LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION
);
This gives you control of te application that you want to execute esp if they are 16-bit applications. The only problem is that you will have to sweat it out in the beginning to get the darn function to work.
All the above mentioned methods wrap CreateProcess API

Welcome to the Pythian Games!
Long live Godess Athena!
dArktEmplAr of Delphi Oracle
 
thank you all for the solutions, it helped me a lot, now I can program that stuff! thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top