Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

[VB6] API Open With Dialog with enhanced functionality

$
0
0
All the methods I've seen for bringing up the Open With dialog use rundll32. But Windows Vista and above has a better option: the SHOpenWithDialog API call. This allows a number of different options in addition to modality. After searching, it seems no one has posted a VB6 implementation yet, so I thought others might like the idea of using this as much as I did.


Requirements: The API call is only available on Vista or higher.

Code:

Option Explicit

'Module: mOpenWith
'Version: 0.1
'Author: fafalone
'Purpose: Vista and above provides an API call for the Open With dialog, which offers more options
'        than the previous typical method of using rundll

Public Declare Function SHOpenWithDialog Lib "shell32" (ByVal hWnd As Long, poainfo As OPENASINFO) As Long

Public Enum OPEN_AS_INFO_FLAGS
    OAIF_ALLOW_REGISTRATION = 1 'Enable the "always use this program" checkbox. If not passed, it will be disabled.
    OAIF_REGISTER_EXT = 2 'Do the registration after the user hits the OK button.
    OAIF_EXEC = 4 'Execute file after registering.
    OAIF_FORCE_REGISTRATION = 8 'Force the Always use this program checkbox to be checked. Typically, you won't use the OAIF_ALLOW_REGISTRATION flag when you pass this value.
    OAIF_HIDE_REGISTRATION = 20 'Introduced in Windows Vista. Hide the Always use this program checkbox. If this flag is specified, the OAIF_ALLOW_REGISTRATION and OAIF_FORCE_REGISTRATION flags will be ignored.
    OAIF_URL_PROTOCOL = 40 'Introduced in Windows Vista. The value for the extension that is passed is actually a protocol, so the Open With dialog box should show applications that are registered as capable of handling that protocol.
    OAIF_FILE_IS_URI = 80 'Introduced in Windows 8. The location pointed to by the pcszFile parameter is given as a URI.
End Enum

Public Type OPENASINFO
    pcszFile As Long
    pcszClass As Long 'file type description for registering the type with 'always open', if not set uses extension, as in 'XYZ File'
    oafInFlags As OPEN_AS_INFO_FLAGS
End Type



Public Function OpenWith(sFile As String, lFlags As OPEN_AS_INFO_FLAGS, Optional hWndParent As Long, Optional sClass As String) As Long
Dim oai As OPENASINFO
oai.pcszFile = StrPtr(sFile)
oai.oafInFlags = lFlags
If sClass <> "" Then oai.pcszClass = StrPtr(sClass)
OpenWith = SHOpenWithDialog(hWndParent, oai)
End Function

The sample project attached contains a form that calls the OpenWith function.

OpenWith(sFile As String, lFlags As OPEN_AS_INFO_FLAGS, Optional hWndParent As Long, Optional sClass As String)

sFile - The file to be opened
lFlags - See the descriptions in the BAS; you'll usually want to include OAIF_EXEC to open the file afterwards, and OAIF_ALLOW_REGISTRATION to enable the 'always use this program' box.
hWndParent - You can specify an owner window (e.g. Form1.hWnd) and the dialog will be modal to that form (you can't click on anything on the form until the dialog closes).
sClass - You can optionally specify a file type description for registering the type for always open. If not specified, the file extension would be used (e.g. XYZ File).

Note: Since this is a Unicode function (it takes lpcwstr's, hence the need for strptr()), it should handle unicode file names and unicode path lengths without issue.
Attached Files

Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>