Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SHBrowseForFolder Function

Status
Not open for further replies.

GKProgrammer

Programmer
Sep 26, 2001
285
CA
Hi All,

I am using the SHBrowseForFolder function in a program that I am currently writing. I have created a type shellBrowseInfo:

Type shellBrowseInfo
hwndOwner As Long
pIDlRoot As Long
pszDisplayName As Long
IpszTitle As String
uIFlags As Long
IpfnCallBack As Long
IParam As Long
iImage As Long
End Type

When the browse for folder dialog box appears I want it to only display one certain folder with all subfolders within that folder. This folder will be different for whichever user is logged in, so the folder looks as follows:

H:\Home\USERNAME\PCAM <--(fill in actual user for USERNAME)

I can get the string version of this path very easily and I have no porblem doing that. My problem is that the parameter that sets which folder to begin with is the pIDlRoot displayed above. This is a pointer to an Item ID (PIDL), does anybody know how to get this PIDL from possibly a string version of the path so that I can pass it into this parameter before opening the browse for folder dialog box. Any other comments or suggestions will be welcomed also.

Thank you very much,
gkprogrammer
 
from some code I got some time ago:
am including the copyright
can e-mail you the rest of the code if you need it (might be too long to post)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2001 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' You are free to use this code within your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this source code without prior written consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Code:
Function BrowseForFolderByPath(Optional sSelPath As String = vbNullString, _
    Optional theTitle As String = vbNullString) As String

  Dim BI As BROWSEINFO
  Dim pidl As Long
  Dim lpSelPath As Long
  Dim spath As String * MAX_PATH
  
  With BI
    .hOwner = Me.hWnd
    .pidlRoot = 0
    .lpszTitle = theTitle
    .lpfn = FARPROC(AddressOf BrowseCallbackProcStr)
    
    lpSelPath = LocalAlloc(LPTR, Len(sSelPath) + 1)
    CopyMemory ByVal lpSelPath, ByVal sSelPath, Len(sSelPath) + 1
    .lParam = lpSelPath
    
    End With
    
   pidl = SHBrowseForFolder(BI)
   
   If pidl Then
     
      If SHGetPathFromIDList(pidl, spath) Then
         BrowseForFolderByPath = Left$(spath, InStr(spath, vbNullChar) - 1)
      End If
      
      Call CoTaskMemFree(pidl)
   
   End If
   
  Call LocalFree(lpSelPath)

End Function


Function BrowseForFolderByPIDL(sSelPath As String) As String

   Dim BI As BROWSEINFO
   Dim pidl As Long
   Dim spath As String * MAX_PATH
  
   With BI
      .hOwner = Me.hWnd
      .pidlRoot = 0
      .lpszTitle = &quot;Pre-selecting a folder using the folder's pidl.&quot;
      .lpfn = FARPROC(AddressOf BrowseCallbackProc)
      .lParam = GetPIDLFromPath(sSelPath)
   End With
  
   pidl = SHBrowseForFolder(BI)
  
   If pidl Then
      If SHGetPathFromIDList(pidl, spath) Then
         BrowseForFolderByPIDL = Left$(spath, InStr(spath, vbNullChar) - 1)
      End If
     
     'free the pidl returned by call to SHBrowseForFolder
      Call CoTaskMemFree(pidl)
  End If
  
 'free the pidl set in call to GetPIDLFromPath
  Call CoTaskMemFree(BI.lParam)
  
End Function


Function GetPIDLFromPath(spath As String) As Long

  'return the pidl to the path supplied by calling the
  'undocumented API #162 (our name SHSimpleIDListFromPath).
  'This function is necessary as, unlike documented APIs,
  'the API is not implemented in 'A' or 'W' versions.

  If IsWinNT Then
    GetPIDLFromPath = SHSimpleIDListFromPath(StrConv(spath, vbUnicode))
  Else
    GetPIDLFromPath = SHSimpleIDListFromPath(spath)
  End If

End Function
 
Hi Justin,

Thanks for the reply, I would be interested in seeing the whole code, can you please email it to the following address:

gregkittler@vtech.ca

Thanks Alot,
gkprogrammer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top