Using Win32-specific APIs, is there an easy way to start an external application to open a file simply by passing in the path/name of the file?
For example, say I have a file called C:\tmp\image.jpg. Is there a single API that I can call to tell Windows to open the application associated with .jpg files? Without having to do a bunch of registry lookups?
I thought I remembered doing this many years ago, but I cannot find it.
ShellExecute
Performs an operation on a specified file.
Syntax
C++
HINSTANCE ShellExecute( _In_opt_ HWND hwnd, _In_opt_ LPCTSTR lpOperation, _In_ LPCTSTR lpFile, _In_opt_ LPCTSTR lpParameters, _In_opt_ LPCTSTR lpDirectory, _In_ INT nShowCmd );
Parameters
...
nShowCmd [in]
Type: INT
The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it. These values are defined in Winuser.h...
ShellExecute is the function you're looking for. It can handle both executable types and registered file types, and perform all sorts of actions (verbs) on the file, depending on what it supports.
The syntax is:
HINSTANCE ShellExecute(
HWND hwnd, // handle to owner window.
LPCTSTR lpOperation, // verb to do, e.g., edit, open, print.
LPCTSTR lpFile, // file to perform verb on.
LPCTSTR lpParameters, // parameters if lpFile is executable, else NULL.
LPCTSTR lpDirectory, // working directory or NULL for current directory.
INT nShowCmd // window mode e.g., SW_HIDE, SW_SHOWNORMAL.
);
Consult your friendly neighborhood MSDN documentation for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With