![]() |
Desktop Development »
Shell and IE programming »
General
Intermediate
License: The Code Project Open License (CPOL)
ExecApp, ExecRegisteredApp, and LookupRegisteredApp - non-MFC functions to execute an applicationBy Hans DietrichExecApp is a replacement for WinExec(). ExecRegisteredApp executes the app that is registered for the specified file extension. LookupRegisteredApp retrieves the application file path that is registered for the specified file extension. |
C++ (VC6, VC8.0), Windows, Visual Studio (VS2005), Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
The functions described below have been tested on Windows 98, 2000, XP, and Vista.
lpCommandLine parameter. It turns out that the simplest, most
reliable way to use
CreateProcess
is to use NULL for the first parameter, and pass the application
file path and arguments (each enclosed in quotes, separated by a space)
in the second parameter.
Here is function header of ExecApp():
/////////////////////////////////////////////////////////////////////////////// // // ExecApp() // // Purpose: Runs the specified application (replacement for WinExec) // // Parameters: lpszCommandLine - [in] command line (including exe file path) // that is passed to CreateProcess() // wShowCmd - [in] Specifies how app window is to be shown. // See ShowWindow() in MSDN for possible values. // // Returns: BOOL - TRUE = CreateProcess() succeeded // BOOL ExecApp(LPCTSTR lpszCommandLine, WORD wShowCmd /*= SW_SHOWNORMAL*/)
LookupRegisteredApp() retrieves the application file path
that is registered for the specified file extension.
LookupRegisteredApp() does its work by calling the shell function
AssocQueryString.
This function is exported via the import lib shlwapi.lib, which
is automatically linked to in ExecApp.cpp. (Note: requires
Internet Explorer 5 or later).
Here is function header of LookupRegisteredApp():
/////////////////////////////////////////////////////////////////////////////// // // LookupRegisteredApp() // // Purpose: Retrieves the application registered for the specified file // extension // // Parameters: lpszExt - [in] file extension (e.g., ".txt") used to // look up application file path. Preceding '.' // is necessary. // lpszApplication - [out] application path buffer // nSize - [in/out] size of path buffer in TCHARs // // Returns: BOOL - TRUE = found registered app // // Notes: AssocQueryString() is broken in Vista. If no application is // associated with the file extension, in Vista the function returns // the "Unknown" application, rather than an error code (as in XP). // Adding ASSOCF_IGNOREUNKNOWN to the flags parameter will make the // function behave as in XP. ASSOCF_IGNOREUNKNOWN is defined in the // latest Platform SDK. // BOOL LookupRegisteredApp(LPCTSTR lpszExt, LPTSTR lpszApplication, DWORD *nSize)
Aside from the small glitch in using
AssocQueryString
on Vista (see Notes in preceding header), there is another,
more serious problem with
AssocQueryString:
the ANSI version of this function (AssocQueryStringA)
doesn't work.
I don't know what the exact problem is, but the workaround is to convert the
parameters to Unicode, call AssocQueryStringW, and then convert
the result to ANSI. This is why ExecApp.cpp contains the
private (static) function _AssocQueryString.
ExecRegisteredApp() does.
Here is function header of ExecRegisteredApp():
/////////////////////////////////////////////////////////////////////////////// // // ExecRegisteredApp() // // Purpose: Runs the application registered for the specified file extension // // Parameters: lpszArgs - [in] command line arguments that are passed to app // via CreateProcess(); if not already in quotes ("), // they will be enclosed in quotes before CreateProcess() // is called. May be NULL. // lpszExt - [in] file extension (e.g., ".txt") used to look up // application file path. Preceding '.' is necessary. // wShowCmd - [in] Specifies how the app window is to be shown. // See ShowWindow() in MSDN for possible values. // // Returns: BOOL - TRUE = found registered app; CreateProcess() succeeded // BOOL ExecRegisteredApp(LPCTSTR lpszArgs, // may be NULL LPCTSTR lpszExt, WORD wShowCmd /*= SW_SHOWNORMAL*/)
Is ExecRegisteredApp() a replacement for
ShellExecute?
No, absolutely not. If you're 100% sure that a file type
(for example, .html) is registered on the target system, then
you should certainly use
ShellExecute.
But there are times when you can't be sure, and it's not possible
to register a custom file type. This is where ExecRegisteredApp()
is useful.
To integrate ExecApp into your app, you first need to add following files to your project:
The .cpp file should be set to Not using precompiled header in Visual Studio. Otherwise, you will get error
fatal error C1010: unexpected end of file while looking for precompiled header directive
AssocQueryString API. If you have the latest Platform SDK,
you can use that instead of the embedded definitions by un-commenting the line
#include <shlwapi.h> in ExecApp.cpp.
This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 8 Jun 2008 Editor: |
Copyright 2008 by Hans Dietrich Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |