Link to home
Start Free TrialLog in
Avatar of Ilango
Ilango

asked on

SHBrowseForFolder API declaration

I need to user SHBrowseForFolder API call defined in Shell32 for letting the user to select a folder. I couldn't find the declaration in API viewer and continue to define type of my own. I succeeded in getting the dialog. But when I close/Cancel, am getting Bad calling convention runtime error. I tried altering the TYPE and all. But no use. Can anybody help me with this declaration. I just want to display the selected folder in a Editbox. Thanks.

Avatar of Alon Hirsch
Alon Hirsch
Flag of Australia image

Private Type BrowseInfo
    hWndOwner As Long
    pIDLRoot As Long
    pszDisplayName As Long
    lpszTitle As Long
    ulFlags As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
End Type
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Sub Form_Load()
    Dim iNull As Integer, lpIDList As Long, lResult As Long
    Dim sPath As String, udtBI As BrowseInfo

    With udtBI
        'Set the owner window
        .hWndOwner = Me.hWnd
        'lstrcat appends the two strings and returns the memory address
        .lpszTitle = lstrcat("C:\", "")
        'Return only if the user selected a directory
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    'Show the 'Browse for folder' dialog
    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList Then
        sPath = String$(MAX_PATH, 0)
        'Get the path from the IDList
        SHGetPathFromIDList lpIDList, sPath
        'free the block of memory
        CoTaskMemFree lpIDList
        iNull = InStr(sPath, vbNullChar)
        If iNull Then
            sPath = Left$(sPath, iNull - 1)
        End If
    End If

    MsgBox sPath
End Sub

HTH,
Alon
ASKER CERTIFIED SOLUTION
Avatar of aeklund
aeklund

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ilango
Ilango

ASKER

Thanks for both your time. While both the code will solve my problem, I go with AEKLUND for making the integration part easier. Thanks AlonHirsch for your immediate helping hands. Now I'm engaged in making this in to a AX Control and to implement the dialog to explorer from previously select folder.
Hi,

You're welcome.

Have a look at http://vbaccelerator.com - there is already an ActiveX that encapsulates this functionality.
I'm not sure which project it is, but I think the description of the project mentions WinZip.
It allows you to serch for a folder like in WinZip.

HTH,
Alon
Thanks and Good Luck!