C++ Builder: Run external application / Open documentCan you run an external program from within a C++ program? How to print documents from within your program, without explicitly starting the application that created the document, such as: print a Word-document without starting Word? How to open your browser with a local HTML document? How to surf to a site, i.e. open an external HTML page? All of this is possible by calling ShellExecute, a Windows API function. The syntax is: ShellExecute( hwnd, pOperation, pFile, pParameters, pDirectory, nShowCmd )
Important notes
ExamplesRun the application NotePad.exe: void __fastcall TForm1::Button1Click(TObject *Sender)
{
String Prog = "notepad.exe";
// ShellExecute does not accept Unicode strings, use a literal...
// ...such as L"open" or convert from Unicode with c_strc()
ShellExecute(NULL, L"open", Prog.c_str(), NULL, NULL, SW_SHOWNORMAL);
}
Run NotePad.exe and specify a text file: void __fastcall TForm1::Button2Click(TObject *Sender)
{
String Prog = "notepad.exe";
String TheFile = "C:\\test\\text.txt"; // double backslashes!
ShellExecute(NULL, L"open", Prog.c_str(), TheFile.c_str(), NULL, SW_SHOWNORMAL);
}
Open a text file with Windows' default text editor: void __fastcall TForm1::Button3Click(TObject *Sender) { String TheFile = "C:\\test\\text.txt"; ShellExecute(NULL, L"open", TheFile.c_str(), NULL, NULL, SW_SHOWNORMAL); } Explore a folder (directory): void __fastcall TForm1::Button4Click(TObject *Sender) { String TheFolder = "C:\\"; ShellExecute(NULL, L"explore", TheFolder.c_str(), NULL, NULL, SW_SHOWNORMAL); } Open a local HTML file in your default browser: void __fastcall TForm1::Button5Click(TObject *Sender) { String TheFile = "C:\\test\\test.htm"; ShellExecute(NULL, L"open", TheFile.c_str(), NULL, NULL, SW_SHOWNORMAL); } Open a web page in your default browser: void __fastcall TForm1::Button6Click(TObject *Sender) { String ThePage = "http://www.festra.com"; ShellExecute(NULL, L"open", ThePage.c_str(), NULL, NULL, SW_SHOWNORMAL); } |