Results 1 to 6 of 6

Thread: SHBrowseForFolder

  1. #1

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577

    SHBrowseForFolder

    Hi, i would like to use the SHBrowseForFolder API in vb.net
    this site

    i copied it to my vb.net program, i changed all the long's to integers because intergs now are 32bit, Replaced Type with Structure

    and used this code to run it...
    VB Code:
    1. Dim bi As New BROWSEINFO()
    2.  
    3.         Dim pidl As Integer
    4.  
    5.         bi.hOwner = Me.Handle.ToInt32
    6.         bi.pidlRoot = 0&
    7.         bi.lpszTitle = "Select Directory"
    8.         bi.ulFlags = BIF_RETURNONLYFSDIRS
    9.  
    10.         pidl = SHBrowseForFolder(bi)
    11.  
    12.         Call CoTaskMemFree(pidl)


    now if i run that, i get a error on line
    pidl = SHBrowseForFolder(bi) with this error..

    Additional information: Object reference not set to an instance of an object.

    can someone please explain what is wrong?

    thanks

    P.S nearly 100 Posts

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Show us the Structure please,
    anyway, the problem should be solved by changing
    VB Code:
    1. pidl = SHBrowseForFolder(bi)
    to
    VB Code:
    1. pidl = new SHBrowseForFolder(bi)
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    I got it without the 'NEW' addition. This is what I use in VB.NET.

    Private Structure BROWSEINFO
    Dim hOwner As Integer
    Dim pidlRoot As Integer
    Dim pszDisplayName As String
    Dim lpszTitle As String
    Dim ulFlags As Integer
    Dim lpfn As Integer
    Dim lParam As Integer
    Dim iImage As Integer
    End Structure

    Private Const BIF_RETURNONLYFSDIRS As Short = &H1S
    Private Const BIF_DONTGOBELOWDOMAIN As Short = &H2S
    Private Const BIF_STATUSTEXT As Short = &H4S
    Private Const BIF_RETURNFSANCESTORS As Short = &H8S
    Private Const BIF_BROWSEFORCOMPUTER As Short = &H1000S
    Private Const BIF_BROWSEFORPRINTER As Short = &H2000S
    Private Const MAX_PATH As Short = 256

    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal nPidl As Integer, ByVal pszPath As String) As Integer
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (ByRef lpBrowseInfo As BROWSEINFO) As Integer
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal PV_Renamed As Integer)

    Public Function BrowseFolder(ByRef sTitle As String, ByRef frm As System.Windows.Forms.Form) As String
    On Error Resume Next

    Dim browse As BROWSEINFO
    Dim nPidl As Integer
    Dim sPath As String
    Dim nPos As Short
    Dim sReturn As String

    sReturn = ""
    browse.hOwner = frm.Handle.ToInt32
    browse.pidlRoot = 0
    browse.lpszTitle = sTitle
    browse.ulFlags = BIF_RETURNONLYFSDIRS
    nPidl = SHBrowseForFolder(browse)
    sPath = Space(MAX_PATH)
    If SHGetPathFromIDList(nPidl, sPath) Then
    nPos = InStr(sPath, Chr(0))
    sReturn = Left(sPath, nPos - 1)
    End If
    Call CoTaskMemFree(nPidl)
    BrowseFolder = sReturn
    End Function


    EXAMPLE:
    Dim ChosenFolder As String = BrowseFolder("Where Do You Want To Place Your Files", Me)

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Please, Please always put your code between vbcode tags!

    VB Code:
    1. Private Structure BROWSEINFO
    2. Dim hOwner As Integer
    3. Dim pidlRoot As Integer
    4. Dim pszDisplayName As String
    5. Dim lpszTitle As String
    6. Dim ulFlags As Integer
    7. Dim lpfn As Integer
    8. Dim lParam As Integer
    9. Dim iImage As Integer
    10. End Structure
    11.  
    12. Private Const BIF_RETURNONLYFSDIRS As Short = &H1S
    13. Private Const BIF_DONTGOBELOWDOMAIN As Short = &H2S
    14. Private Const BIF_STATUSTEXT As Short = &H4S
    15. Private Const BIF_RETURNFSANCESTORS As Short = &H8S
    16. Private Const BIF_BROWSEFORCOMPUTER As Short = &H1000S
    17. Private Const BIF_BROWSEFORPRINTER As Short = &H2000S
    18. Private Const MAX_PATH As Short = 256
    19.  
    20. Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal nPidl As Integer, ByVal pszPath As String) As Integer
    21. Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (ByRef lpBrowseInfo As BROWSEINFO) As Integer
    22. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal PV_Renamed As Integer)
    23.  
    24. Public Function BrowseFolder(ByRef sTitle As String, ByRef frm As System.Windows.Forms.Form) As String
    25. On Error Resume Next
    26.  
    27. Dim browse As BROWSEINFO
    28. Dim nPidl As Integer
    29. Dim sPath As String
    30. Dim nPos As Short
    31. Dim sReturn As String
    32.  
    33. sReturn = ""
    34. browse.hOwner = frm.Handle.ToInt32
    35. browse.pidlRoot = 0
    36. browse.lpszTitle = sTitle
    37. browse.ulFlags = BIF_RETURNONLYFSDIRS
    38. nPidl = SHBrowseForFolder(browse)
    39. sPath = Space(MAX_PATH)
    40. If SHGetPathFromIDList(nPidl, sPath) Then
    41.    nPos = InStr(sPath, Chr(0))
    42.    sReturn = Left(sPath, nPos - 1)
    43. End If
    44. Call CoTaskMemFree(nPidl)
    45. BrowseFolder = sReturn
    46. End Function
    47.  
    48.  
    49. EXAMPLE:
    50. Dim ChosenFolder As String = BrowseFolder("Where Do You Want To Place Your Files", Me)
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    Yep, your right, sorry about that, thanks for fixing :-)

  6. #6
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    c# assembly for SHBrowseForFolder

    You should be able to drop this into your VB program without a problem...
    -scott
    he he he

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