I am attempting to port the code in this article to VB6, but I'm experiencing crashing. I'm pretty sure my error is in my call to SHBindToParent
(MSDN entry) since SHParseDisplayName
is returning 0 (S_OK
) and ppidl
is being set. I admit my mechanism of setting the riid (I used an equivalent type, a UUID
) is pretty ugly, but I think it more likely I'm doing something wrong with psf
.
Private Declare Function SHParseDisplayName Lib "shell32" (ByVal pszName As Long, ByVal IBindCtx As Long, ByRef ppidl As ITEMIDLIST, sfgaoIn As Long, sfgaoOut As Long) As Long
Private Declare Function SHBindToParent Lib "shell32" (ByVal ppidl As Long, ByRef shellguid As UUID, ByVal psf As Long, ByVal ppidlLast As Long) As Long
Private Sub Main()
Dim hr As Long
Dim ppidl As ITEMIDLIST
Dim topo As String
Dim psf As IShellFolder
Dim pidlChild As ITEMIDLIST
topo = "c:\tmp\" '"//This VB comment is here to make SO's rendering look nicer.
Dim iid_shellfolder As UUID
iid_shellfolder.Data1 = 136422
iid_shellfolder.Data2 = 0
iid_shellfolder.Data3 = 0
iid_shellfolder.Data4(0) = 192
iid_shellfolder.Data4(7) = 70
hr = SHParseDisplayName(StrPtr(topo), 0, ppidl, 0, 0)
Debug.Print hr, Hex(hr)
hr = SHBindToParent(VarPtr(ppidl), iid_shellfolder, VarPtr(psf), VarPtr(pidlChild)) 'Crashes here
End Sub
I believe your call to SHBindToParent is crashing because you need to pass longs, then use the returned pointers to copy the memory into your types. I found several posts when I googled the SHBindToParent function that mentioned OS support, mostly 95 and 98. When I tried it on XP SP3 I got an error "No such interface supported."
Here is how I modified your code to get past the GPF:
Option Explicit
Private Declare Function SHParseDisplayName Lib "shell32" (ByVal pszName As Long, ByVal IBindCtx As Long, ByRef ppidl As Long, ByVal sfgaoIn As Long, ByRef sfgaoOut As Long) As Long
Private Declare Function SHBindToParent Lib "shell32" (ByVal ppidl As Any, ByRef shellguid As UUID, ByRef psf As Any, ByRef ppidlLast As Any) As Long
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Type UUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Sub Command1_Click()
Dim hr As Long
Dim ppidl As Long
Dim topo As String
Dim psf As IShellFolder
Dim pidlChild As Long
Dim iid_shellfolder As UUID
Dim lpIDList2 As Long
topo = "C:\Temp"
' create a uuid = {B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC}'
iid_shellfolder.Data1 = &HB7534046
iid_shellfolder.Data2 = &H3ECB
iid_shellfolder.Data3 = &H4C18
iid_shellfolder.Data4(0) = 190
iid_shellfolder.Data4(1) = 78
iid_shellfolder.Data4(2) = 100
iid_shellfolder.Data4(3) = 205
iid_shellfolder.Data4(4) = 76
iid_shellfolder.Data4(5) = 183
iid_shellfolder.Data4(6) = 214
iid_shellfolder.Data4(7) = 172
hr = SHParseDisplayName(StrPtr(topo), ByVal 0&, lpIDList2, ByVal 0&, ByVal 0&)
' Debug.Print hr, Hex(hr)'
hr = SHBindToParent(lpIDList2, iid_shellfolder, psf, pidlChild) 'retuns "No such interface supported" error
End Sub