views:

207

answers:

2

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.

+9  A: 

ShellExecute

Igal Serban
+4  A: 

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.

paxdiablo