Hi,
i'am new to IDE Macro porgramming (and new to VB and .net as well) and stuck
with a problem with FolderBrowserDialog and SHGetPathFromIDList .
I Like to open a "Browse for Folder Dialog" from within a .NET Macro. At
first i tried FolderBrowserDialog but i get an empty dialog, only showing
the OK and Cancel buttons but no treeview for choosing the folder. This is
the code (WinWrapper is copied from the Macro examples):
------------------------------------
Public Class WinWrapper
Implements System.Windows.Forms.IWin32Window
Overridable ReadOnly Property Handle() As System.IntPtr Implements
System.Windows.Forms.IWin32Window.Handle
Get
Dim iptr As New System.IntPtr(DTE.MainWindow.HWnd)
Return iptr
End Get
End Property
End Class
Sub testFolderBrowser()
Dim winptr As New WinWrapper
Dim folderDialog As New FolderBrowserDialog
folderDialog.RootFolder = Environment.SpecialFolder.Desktop
folderDialog.ShowNewFolderButton = False
If folderDialog.ShowDialog(winptr) = DialogResult.OK Then
MsgBox(folderDialog.SelectedPath())
End If
End Sub
------------------------------
Then i tried the SHBrowseForFolder und SHGetPathFromIDList functions
directly:
------------------------------------
Private Structure BrowseInfo
Public hWndOwner As Long
Public pIDLRoot As Long
Public pszDisplayName As Long
Public lpszTitle As Long
Public ulFlags As Long
Public lpfnCallback As Long
Public lParam As Long
Public iImage As Long
End Structure
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260
Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function SHBrowseForFolder Lib "shell32" (ByVal lpbi As
BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As
Long, ByVal lpBuffer As String) As Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal
lpString1 As String, ByVal lpString2 As String) As Long
Public Function BrowseForFolder(ByVal hwndOwner As Long, ByVal sPrompt As
String) As String
Dim lpIDList As Long ' Declare Varibles
Dim sBuffer As String
Dim tBrowseInfo As BrowseInfo
With tBrowseInfo
.hWndOwner = hwndOwner ' Owner Form
.lpszTitle = lstrcat(sPrompt, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
End With
lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList(lpIDList, sBuffer)
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
MsgBox(sBuffer)
End If
End Function
------------------------------------
but this didn't work either. The dialog opens and i can choose a folder but
when i try to get the name with SHGetPathFromIDList() an exception is
thrown. It is something like "an object refernce is not tied to an object
instance". This sounds like a dereferenced NULL pointer to me (i'am a c++
programmer).
Do you have any ideas for solving the problem?
TIA, Henning
amadouN@gmail.com - 14 Mar 2005 19:20 GMT
your are using a function without return any string
> Hi,
>
[quoted text clipped - 84 lines]
>
> TIA, Henning