Results 1 to 16 of 16

Thread: [VB6] API Open With Dialog with enhanced functionality

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,710

    [VB6] API Open With Dialog with enhanced functionality

    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.

    *UPDATE: Flags were declared incorrectly; forgot &H.


    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 = &H1 'Enable the "always use this program" checkbox. If not passed, it will be disabled.
        OAIF_REGISTER_EXT = &H2 'Do the registration after the user hits the OK button.
        OAIF_EXEC = &H4 'Execute file after registering.
        OAIF_FORCE_REGISTRATION = &H8 '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 = &H20 '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 = &H40 '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 = &H80 '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 Attached Files
    Last edited by fafalone; Oct 23rd, 2014 at 04:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width