Wednesday, December 24, 2008

[VC++] Using ShellExecute for starting applications

You might have thought of starting an application programatically, for example starting MS word or starting a browser with an intented page programmatically. Let me introduce you an API ShellExecute() and its usage through some examples. ShellExecute() Performs a specified operation on a specified file.This method allows you to execute any commands in a folder's shortcut menu or stored in the registry. Below examples will give you a better idea,

1. Start freshtechies.blogspot page in your default browser .
ShellExecute( 0, "open", "http://freshtechies.blogspot.com", 0, 0, SW_SHOWNORMAL);

2. Open the folder in C:\Windows
ShellExecute( 0, "open", "c:\\Windows", 0, 0, SW_SHOWNORMAL);

3. Launch the Shell's Find utility for C:\Windows
ShellExecute( 0, "find", "C:\\Windows", 0, 0, 0);

4. Start MS Word
CString csWordPath = "C:\\Program Files\\Microsoft Office\\Office12\\winword.exe";
ShellExecute( 0, "open", csWordPath, 0, 0, SW_SHOWNORMAL);

Now you are in a position to use ShellExecute(), let me call your attention to an extended version of this function ShellExecuteEx() is available with which you can set many options for controlling the operation to be performed.

No comments: