Private Declare Function SHGetIDListFromPath Lib "SHELL32.DLL" Alias "#162" (ByVal szPath As String) As LongPrivate Sub Command1_Click()
  RootDir = "C:\WINDOWS"
  IDListRoot = 1  If RootDir = "" Then
    IDListRoot = 0
  Else
    If Dir(RootDir, vbDirectory) <> "" Then
      IDListRoot = SHGetIDListFromPath(RootDir) '将路径转为标识符
    End If
  End If
  Debug.Print IDListRoot
End Sub为什么不管RootDir的路径赋予什么,IDListRoot返回的值总是0,而我所给的路径系统里也都有,用逐句运行的方法测试看着它走过“IDListRoot = SHGetIDListFromPath(RootDir)”后IDListRoot还是为0,是不是我哪里错了呢?
在网上搜过这函数,中文网站是寥寥无几的,只有在Google里一些外国网站上提起过,可看了他们的应用也跟我差不多啊,但我的就是不行,直接复制他们的也不行,不解啊?????

解决方案 »

  1.   

    Private Declare Function SHGetIDListFromPath Lib "SHELL32.DLL" Alias "#162" (ByVal szPath As String) As LongPrivate Sub Command1_Click()
    RootDir = "C:\WINDOWS"
    IDListRoot = 1If RootDir = "" Then
    IDListRoot = 0
    Else
    If Dir(RootDir, vbDirectory) <> "" Then
    IDListRoot = SHGetIDListFromPath(StrConv(RootDir, vbUnicode)) 
    End If
    End If
    Debug.Print IDListRoot
    End Sub
      

  2.   

    SHGetIDListFromPath(StrConv(RootDir, vbUnicode))
      

  3.   

    楼主什么系统啊,我在中文xp sp2 下测试没有问题可以考虑先提升一下权限
      

  4.   

    'In Form:>Option ExplicitDim m_sFolder           As String 'Path of Folder/File returned from BrowseForFolder dialog.
    Dim m_sDisplayName      As String 'Item text returned from BrowseForFolder dialog.Private Sub cmdBrowse_Click()
       Dim sFolder          As String   m_sFolder = "D:\"   sFolder = Browse(Me.hWnd, _
          BIF_BROWSEINCLUDEFILES Or BIF_STATUSTEXT Or BIF_USENEWUI, _
          m_sFolder, _
          m_sDisplayName, _
          "Title:", _
          "Dialog Title: ", _
          "")   Debug.Print sFolder & vbCrLf & _
          "DisplayName: " & m_sDisplayNameEnd Sub'----------------------------
    'In Module:Option ExplicitPrivate Const WM_USER   As Long = &H400&' message from browser
    Private Const BFFM_INITIALIZED As Long = 1
    Private Const BFFM_SELCHANGED As Long = 2
    Private Const BFFM_SETSTATUSTEXTA As Long = (WM_USER + 100)
    Private Const BFFM_SETSELECTIONA As Long = (WM_USER + 102)Private Const MAX_PATH  As Long = 260Private Type BROWSEINFOA
       hOwner               As Long
       pidlRoot             As Long
       pszDisplayName       As String
       lpszTitle            As String
       ulFlags              As Long
       lpfn                 As Long
       lParam               As Long
       iImage               As Long
    End TypePublic Enum bif
       BIF_RETURNONLYFSDIRS = &H1        ' For finding a folder to start document searching
       BIF_DONTGOBELOWDOMAIN = &H2       ' For starting the Find Computer
       BIF_STATUSTEXT = &H4              ' Top of the dialog has 2 lines of text for BROWSEINFO.lpszTitle and one line if
       ' this flag is set.  Passing the message BFFM_SETSTATUSTEXTA to the hwnd can set the
       ' rest of the text.  This is not used with BIF_USENEWUI and BROWSEINFO.lpszTitle gets
       ' all three lines of text.
       BIF_RETURNFSANCESTORS = &H8
       BIF_EDITBOX = &H10                ' Add an editbox to the dialog
       BIF_VALIDATE = &H20               ' insist on valid result (or CANCEL)   BIF_NEWDIALOGSTYLE = &H40         ' Use the new dialog layout with the ability to resize
       ' Caller needs to call OleInitialize() before using this API   BIF_USENEWUI = (BIF_NEWDIALOGSTYLE Or BIF_EDITBOX)
       BIF_BROWSEINCLUDEURLS = &H80      ' Allow URLs to be displayed or entered. (Requires BIF_USENEWUI)
       BIF_UAHINT = &H100                ' Add a UA hint to the dialog, in place of the edit box. May not be combined with BIF_EDITBOX
       BIF_NONEWFOLDERBUTTON = &H200     ' Do not add the "New Folder" button to the dialog.  Only applicable with BIF_NEWDIALOGSTYLE.
       BIF_NOTRANSLATETARGETS = &H400    ' don't traverse target as shortcut
       BIF_BROWSEFORCOMPUTER = &H1000    ' Browsing for Computers.
       BIF_BROWSEFORPRINTER = &H2000     ' Browsing for Printers
       BIF_BROWSEINCLUDEFILES = &H4000   ' Browsing for Everything
       BIF_SHAREABLE = &H8000            ' sharable resources displayed (remote shares, requires BIF_USENEWUI)
    End EnumPrivate Declare Function SHBrowseForFolderA Lib "shell32" (lpBrowseInfo As BROWSEINFOA) As Long
    Private Declare Function SHGetIDListFromPath Lib "shell32" Alias "#162" (ByVal szPath As String) As Long
    Private Declare Function SHGetPathFromIDListA Lib "shell32" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    Private Declare Function SetWindowTextA Lib "User32" (ByVal hWnd As Long, ByVal lpString As String) As Long
    Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pv As Long)Private Buffer          As String * MAX_PATH
    Private m_DialogTitle   As StringPrivate m_StartDir      As String
    Private m_bNewUI        As BooleanPublic Function Browse(ByVal hWnd As Long, Optional ulFlags As bif = BIF_RETURNONLYFSDIRS, Optional ByVal sStartDir As String, Optional ByRef sDisplayName As String, Optional ByVal sTitle As String, Optional ByVal sDialogTitle As String, Optional ByVal sRootDir As String) As String   Dim biA              As BROWSEINFOA
       Dim pidl             As Long   m_StartDir = sStartDir
       m_DialogTitle = sDialogTitle
       m_bNewUI = (ulFlags And BIF_NEWDIALOGSTYLE) = BIF_NEWDIALOGSTYLE   With biA 'Fill the BROWSEINFO structure.
          .hOwner = hWnd  'GetDesktopWindow() 'can be application or Desktop hwnd      If LenB(sRootDir) Then 'get pidl of root folder
             .pidlRoot = SHGetIDListFromPath(StrConv(sRootDir, vbUnicode))
          Else
             .pidlRoot = 0& 'desktop folder is used
          End If      .pszDisplayName = Buffer     'Display Name      If LenB(sTitle) Then
             .lpszTitle = sTitle   'Title text
          End If      .ulFlags = ulFlags 'dialog type.      .lpfn = GetAddressofFunction(AddressOf BrowseCallbackProc) 'Callback
       End With   pidl = SHBrowseForFolderA(biA)  'show the dialog
       sDisplayName = StripNull(biA.pszDisplayName)   Browse = SHGetPathFromIDList(pidl, Buffer)End FunctionPrivate Function BrowseCallbackProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lp As Long, ByVal pData As Long) As Long
       Dim sBuffer          As String   On Error Resume Next  'Suggested by MS to prevent an error from
       'propagating back into the calling process.   Select Case uMsg
          Case BFFM_INITIALIZED
             SendMessage hWnd, BFFM_SETSELECTIONA, 1, m_StartDir
             SetWindowTextA hWnd, m_DialogTitle      Case BFFM_SELCHANGED
             sBuffer = Space$(MAX_PATH)         If SHGetPathFromIDListA(ByVal lp, sBuffer) = 1 Then
                Call SendMessage(hWnd, BFFM_SETSTATUSTEXTA, 0, sBuffer)
             End If   End Select   BrowseCallbackProc = 0End Function' This function allows you to assign a function pointer to a vaiable.
    Private Function GetAddressofFunction(add As Long) As Long
       GetAddressofFunction = add
    End FunctionPrivate Function SHGetPathFromIDList(ByVal pidl As Long, ByVal pszPath As String) As String
       If pidl = 0 Then Exit Function   If SHGetPathFromIDListA(ByVal pidl, pszPath) Then
          SHGetPathFromIDList = StripNull(pszPath)
       End If   CoTaskMemFree pidlEnd FunctionPrivate Function StripNull(ByVal StrIn As String) As String
       Dim nul              As Long
       ' Truncate input string at first null.
       ' If no nulls, perform ordinary Trim.
       nul = InStr(1, StrIn, vbNullChar, vbBinaryCompare)
       Select Case nul
          Case Is > 1
             StripNull = Left$(StrIn, nul - 1)
          Case 1
             StripNull = ""
          Case 0
             StripNull = Trim$(StrIn)
       End Select
    End Function'----------------------
      

  5.   

    rainstormmaster(暴风雨 v2.0) 抄的Code好像没用到SHGetIDListFromPath,请仔细看看LZ的问题。
      

  6.   

    SHGetIDListFromPath API一般用在调用打开指定文件夹的对话框中,赋值于BROWSEINFOA结构的pidlRoot参数。
    If LenB(sRootDir) Then '得到指定文件夹的PIDL
        .pidlRoot = SHGetIDListFromPath(StrConv(sRootDir, vbUnicode))  ’Unicode String
    Else
        .pidlRoot = 0& '否则用桌面文件夹
    End If
      

  7.   

    //rainstormmaster(暴风雨 v2.0) 抄的Code好像没用到SHGetIDListFromPath,请仔细看看LZ的问题。在这里:With biA 'Fill the BROWSEINFO structure.
    .hOwner = hWnd 'GetDesktopWindow() 'can be application or Desktop hwndIf LenB(sRootDir) Then 'get pidl of root folder
    .pidlRoot = SHGetIDListFromPath(StrConv(sRootDir, vbUnicode))
    Else
    .pidlRoot = 0& 'desktop folder is used
    End If