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

[VB6, Vista+] Enumerate, explore, and change all file associations

$
0
0

File Associations

This program is similar to the Default Programs control panel applet. It scans the registry for all registered file extensions, then uses the AssocCreate API and IQueryAssociations interface to get further information. The icons are then loaded through SHGetFileInfo and added to the ListView. There's several methods to change the associations-- through the IAssocHandler interface, that lists the recommended programs you see in the Open With dialog; just click a program on the menu and it becomes the new default, updating Windows, next there's the Open With dialog, SHOpenWithDialog, which you can call passing only a file extension to change the default for all files of that type. However, this functionality is no longer available on Windows 10, because MS just loves taking away user control of the OS: you *have* to use the Control Panel to do it on Windows 10, so using the IOpenControlPanel interface we can at least send the user straight to the correct page. Note that this works on Vista/7/8 too; it's just that on Win10 it's the only way, but you can open that control panel page on earlier systems too.

AssocCreate

This function requires quite a bit of effort to be called through VB. It was declared in the original olelib, but that declare will not work (I'll fix it in a future version, but for now it has to be declared in a module). The CLSID parameter *must* be ByVal, but you can't send a user type ByVal. This even though the RIID parameter, which uses the same type (a GUID), *can* be passed ByRef. Passing VarPtr is not accepted, so the natural alternative is to pass each member of the struct ByVal. However, that still doesn't work, you get a "Bad DLL Calling Convention" error. The only way to do this is to rearrange a GUID, Long-Int-Int-Byte x8, into 4 Longs. Doing it this way works, and the interface is created:

Code:

Public Declare Function AssocCreate Lib "shlwapi" (ByVal CLSIDd1 As Long, ByVal CLSIDd23 As Long, _
                                                ByVal CLSIDd40123 As Long, ByVal CLSIDd44567 As Long, riid As UUID, ppv As Any) As Long


Dim pQA As IQueryAssociations
Dim lData1 As Long
Dim lData23 As Long
Dim lData40123 As Long
Dim lData44567 As Long
Dim ab() As Byte
ReDim ab(15)
Dim tCLSID As UUID
tCLSID = CLSID_QueryAssociations
CopyMemory ab(0), tCLSID, 16&
CopyMemory lData1, ab(0), 4& '0 1 2 3
CopyMemory lData23, ab(4), 4& '4 5 6 7
CopyMemory lData40123, ab(8), 4& '8 9 10 11
CopyMemory lData44567, ab(12), 4& '12 13 14 15
AssocCreate lData1, lData23, lData40123, lData44567, IID_IQueryAssociations, pQA

From there we can proceed to call whatever we need.
Code:

    pQA.Init 0&, sExt, 0&, Me.hwnd
    pQA.GetString 0&, ASSOCSTR_FRIENDLYDOCNAME, sVerb, sBuf, lp

and so on.

Some types appear in the registry but actually don't have handlers, so an error is thrown and it won't appear in the list. Another issue is with certain media files that have 'Play' as their default verb. Even Windows itself doesn't handle this properly, so we do the best we can. It will show the icon and the description, but like the control panel will say 'Unknown application' for opening it, even though the Play verb is being used and it works when you double click it.

The menu with the other default program choices you might recognize as mostly the code from my first project about file associations, [VB6] List/Execute File Handlers: IAssocHandler and IAssocHandlerInvoker (Vista+). Generating that menu adds quite a bit of code, but it's all just from that first project, and can be easily cut out by simply removing the GenerateOpenWithMenu submenu.

Finally, as a bonus, this project includes a new modular definitions BAS, for menus. I got tired of having to sift through which declares were needed, so like other controls menus now have a drop-in module that covers it all. The ListView/Header and ImageList modular definitions are also used.

Requirements
-Windows Vista or newer
-oleexp.tlb v4.0 or higher (only needed for the IDE, it gets compiled into your exe)
-oleexp addon mIID.bas (included in the oleexp download)
Attached Files

Viewing all articles
Browse latest Browse all 1449

Trending Articles



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