Results 1 to 6 of 6

Thread: [RESOLVED] How to pass a byte array to SHGetPathFromIDList

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    159

    Resolved [RESOLVED] How to pass a byte array to SHGetPathFromIDList

    Hi folks,

    I need to change a call to SHGetPathFromIDList so that I get folders with Unicode names – That is using a byte array instead of a string.

    The old call using a string for the folder was..

    PathID = SHBrowseForFolder(bInf)
    Fldr = Space$(512)
    RetVal = SHGetPathFromIDList(ByVal PathID, ByVal Fldr)
    CoTaskMemFree PathID

    And Never had a problem - ie there’s no problem with my declarations or SHbrowseforfolder call

    My new call uses a byte array (FldrB) instead of string Fldr…..

    PathID = SHBrowseForFolder(bInf)
    ReDim FldrB(512)
    RetVal = SHGetPathFromIDList(ByVal PathID, ByVal FldrB(0))
    CoTaskMemFree PathID

    The FldrB array does not receive the path.
    I’ve tried several variations like By Ref FldrB(0) and varptr (FldrB(0)) but I still do not get the path in the byte array

    What’s the correct way to do this?

    Thanks in advance.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How to pass a byte array to SHGetPathFromIDList

    Unless you are using a callback, you might find it easier to call the BrowseForFolder method of the Shell object instead (it is Unicode-aware on NT systems). Here's an example:

    Code:
    Private Sub Form_Click()
        Const BIF_RETURNONLYFSDIRS = &H1&, BIF_NONEWFOLDERBUTTON = &H200&
    
        On Error Resume Next
        CreateObject("WScript.Shell").Popup """" & CreateObject("Shell.Application").BrowseForFolder(Me.hWnd, "Please select a folder:", BIF_RETURNONLYFSDIRS Or BIF_NONEWFOLDERBUTTON, CurDir$).Self.Path & """", , App.Title, vbInformation
    End Sub
    Last edited by Bonnie West; Nov 25th, 2013 at 08:56 AM. Reason: Replaced ANSI MsgBox with Unicode CreateObject("WScript.Shell").Popup
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    159

    Re: How to pass a byte array to SHGetPathFromIDList

    Thanks for the reply but it doesn't solve the problem. Any folder with a unicode name is still returned as ?????????.

  4. #4
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: How to pass a byte array to SHGetPathFromIDList

    No need to use a Byte Array.
    StrPtr works just fine.
    Make sure you use a Unicode aware MsgBox (shown below) or a Unicode aware control to display results, otherwise you will see "???.
    Sample code:

    Code:
    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
    
    Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListW" (ByVal pidList As Long, ByVal lpBuffer As Long) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32" Alias "SHBrowseForFolderW" (ByRef lpbi As BROWSEINFO) As Long
    
    Public Function ShowBrowseDir(ByVal Prompt As String, Optional ByVal InitDir As String) As String
       Dim Pos As Long, IDList As Long
       Dim Path As String
       Dim BI As BROWSEINFO
       BrowseInitDir = InitDir
       With BI
          If Not Screen.ActiveForm Is Nothing Then
             .hWndOwner = Screen.ActiveForm.hWnd
          Else
             .hWndOwner = GetActiveWindow()
          End If
          .lpszTitle = StrPtr(Prompt)
          .ulFlags = BIF_RETURNONLYFSDIRS
          If Not InitDir = vbNullString Then .lpfnCallback = ProcPtr(AddressOf BrowseCallback)
       End With
       IDList = SHBrowseForFolder(BI)
       If IDList <> 0 Then
          Path = String(MAX_PATH_UNICODE, vbNullChar)
          SHGetPathFromIDList IDList, StrPtr(Path)
          CoTaskMemFree IDList
          Pos = InStr(Path, vbNullChar)
          If Pos > 0 Then Path = Left(Path, Pos - 1)
       End If
       If Not Path = vbNullString Then Path = Path & IIf(Right(Path, 1) = "\", "", "\")
       ShowBrowseDir = Path
    End Function
    
    'Purpose: Override Vb6 MsgBox with Unicode aware MsgBox. HelpFile/Context not supported.
    Function MsgBox(Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String) As VbMsgBoxResult
        MsgBox = CreateObject("WScript.Shell").Popup(Prompt, 0&, Title, Buttons)
    End Function
    Last edited by DrUnicode; Nov 25th, 2013 at 08:46 AM. Reason: Unicode aware MsgBox

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,734

    Re: How to pass a byte array to SHGetPathFromIDList

    Maybe the problem is not in the call to the function, but in the way the returned filename/path is displayed?
    The default VB6 controls are not Unicode aware.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    159

    Re: How to pass a byte array to SHGetPathFromIDList

    Thank you all and especially DrUnicode it was the StrPtr that fixed it!

Tags for this Thread

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