Click to See Complete Forum and Search --> : SHBrowseForFolder
Geespot
Apr 6th, 2002, 01:18 PM
Hi, i would like to use the SHBrowseForFolder API in vb.net
this site (http://www.mvps.org/vbnet/index.html?code/browse/browsefolders.htm)
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...
Dim bi As New BROWSEINFO()
Dim pidl As Integer
bi.hOwner = Me.Handle.ToInt32
bi.pidlRoot = 0&
bi.lpszTitle = "Select Directory"
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl = SHBrowseForFolder(bi)
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 :)
Jop
Apr 6th, 2002, 01:31 PM
Show us the Structure please,
anyway, the problem should be solved by changing
pidl = SHBrowseForFolder(bi)
to
pidl = new SHBrowseForFolder(bi)
WideAwake
Apr 6th, 2002, 10:16 PM
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)
Jop
Apr 7th, 2002, 07:04 AM
Please, Please always put your code between vbcode tags!
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)
WideAwake
Apr 8th, 2002, 06:50 AM
Yep, your right, sorry about that, thanks for fixing :-)
Scott Penner
Apr 9th, 2002, 05:21 PM
c# assembly for SHBrowseForFolder (http://www.vbforums.com/showthread.php?s=&threadid=160003)
You should be able to drop this into your VB program without a problem...
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.