This code tip applies to the new Common Item Dialog interfaces, IFileOpenDialog and IFileSaveDialog, that replace the old Common Dialog control and GetOpenFileName/GetSaveFileName API calls in Windows Vista and newer. See this project for an introduction to using these interfaces.
Users definitely appreciate the common dialogs opening to the last path, and programmers that this is automatically handled by Windows. Previously, this was limited however to a single state memory for the entire app; but among the many new features of the new IFileDialog-based Common Dialogs is the ability to have Windows manage it automatically for individual dialogs, as well as clear the settings without mucking about in the registry.
The key is IFileDialog's .SetClientGuid method. You can specify a unique GUID for your dialog, and the settings like last path are stored under the GUID, instead of under your app's name.
This example code is based on accessing the new Common Item Dialogs through my oleexp type library, with the IID module loaded as well.
First, establish GUIDs for each dialog you want to have its own settings. Two are shown here, but there's no limit to how many an app can have. These must new, unique GUIDs. Visual Studio 6 came with a tool called GUIDGEN, but there's plenty of other GUID generators out there.
Then all you have to do it associate the Common Dialog with the GUID. Per MSDN, this should be the very first call after creating the dialog:
And that's all there is to it. If you want to clear the saved state, simply call .ClearClientData after setting the associated GUID (you can also clear it for the app-level state by calling it without having set a GUID).
Attached is a small project showing this technique in action. It requires oleexp.tlb (version 4.0 or newer is referenced) and addon mIID.bas from the oleexp zip.
![]()
Users definitely appreciate the common dialogs opening to the last path, and programmers that this is automatically handled by Windows. Previously, this was limited however to a single state memory for the entire app; but among the many new features of the new IFileDialog-based Common Dialogs is the ability to have Windows manage it automatically for individual dialogs, as well as clear the settings without mucking about in the registry.
The key is IFileDialog's .SetClientGuid method. You can specify a unique GUID for your dialog, and the settings like last path are stored under the GUID, instead of under your app's name.
This example code is based on accessing the new Common Item Dialogs through my oleexp type library, with the IID module loaded as well.
First, establish GUIDs for each dialog you want to have its own settings. Two are shown here, but there's no limit to how many an app can have. These must new, unique GUIDs. Visual Studio 6 came with a tool called GUIDGEN, but there's plenty of other GUID generators out there.
Code:
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpszGuid As Long, pGuid As Any) As Long
Private Const sGUID_Dialog1 = "{A4BF774D-0029-4c8f-9174-B397211B92F5}"
Private Const sGUID_Dialog2 = "{83E461A9-5341-46f5-8825-EAC176603E94}"
Private Function GUID_Dialog1() As UUID
Static iid As UUID
If (iid.Data1 = 0) Then Call CLSIDFromString(StrPtr(sGUID_Dialog1), iid)
GUID_Dialog1 = iid
End Function
Private Function GUID_Dialog2() As UUID
Static iid As UUID
If (iid.Data1 = 0) Then Call CLSIDFromString(StrPtr(sGUID_Dialog2), iid)
GUID_Dialog2 = iid
End Function
Code:
Dim pFOD As New FileOpenDialog
pFOD.SetClientGuid GUID_Dialog1 'whichever GUID you want here
'[...now set any other options
Attached is a small project showing this technique in action. It requires oleexp.tlb (version 4.0 or newer is referenced) and addon mIID.bas from the oleexp zip.
