首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SHBrowseForFolderA选择文件夹在windows 10平台上不起作用

使用SHBrowseForFolderA选择文件夹在windows 10平台上不起作用
EN

Stack Overflow用户
提问于 2019-07-31 21:46:49
回答 1查看 673关注 0票数 0

我有一个vba7宏,它使用基于windows api的文件夹选择框。此代码使用SHBrowseForFolderA、SendMessageA、SHGetPathFromIDListA API

到目前为止,这段代码可以在Windows7 x64平台上完美运行。当我在win 10 x64平台上运行它时,此代码崩溃。

代码语言:javascript
复制
    'API Declares
    Public Declare PtrSafe Function SendMessageA Lib "user32" (ByVal hWnd   As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    Private Declare PtrSafe Function SHBrowseForFolderA Lib "shell32.dll" (lpBrowseInfo As BrowseInfo) As Long
    Private Declare PtrSafe Function SHGetPathFromIDListA Lib "shell32.dll" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    Private Declare PtrSafe Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)



    Public Function FolderBrowse(ByVal sDialogTitle As String, ByVal sPath As String) As String
  Dim ReturnPath As String

  Dim b(MAX_PATH) As Byte
  Dim pItem       As Long
  Dim sFullPath   As String
  Dim bi          As BrowseInfo
  Dim ppidl       As Long

  sPath = CorrectPath(sPath)

  bi.hWndOwner = 0 'Screen.ActiveForm.hwnd

  'SHGetSpecialFolderLocation bi.hWndOwner, CSIDL_DRIVES, ppidl

  bi.pIDLRoot = 0 'ppidl

  bi.pszDisplayName = VarPtr(b(0))
  bi.lpszTitle = sDialogTitle
  bi.ulFlags = BF_Flags.BIF_RETURNONLYFSDIRS + BF_Flags.BIF_NEWDIALOGSTYLE + BF_Flags.BIF_STATUSTEXT              'BIF_RETURNONLYFSDIRS
  'bi.ulFlags = BF_Flags.BIF_RETURNONLYFSDIRS + BF_Flags.BIF_USENEWUI + BF_Flags.BIF_STATUSTEXT             'BIF_RETURNONLYFSDIRS

  If FolderExists(sPath) Then bi.lpfnCallback = PtrToFunction(AddressOf BFFCallback)
  bi.lParam = StrPtr(sPath)



  pItem = SHBrowseForFolderA(bi)

  If pItem Then ' Succeeded
    sFullPath = Space$(MAX_PATH)
    If SHGetPathFromIDListA(pItem, sFullPath) Then
      ReturnPath = Left$(sFullPath, InStr(sFullPath, vbNullChar) - 1) ' Strip nulls
      CoTaskMemFree pItem
    End If
  End If

'  If pItem <> 0 Then ' Succeeded
'    sFullPath = Space$(MAX_PATH_Unicode)
'    If SHGetPathFromIDListW(pItem, StrPtr(sFullPath)) Then
'      ReturnPath = Left$(sFullPath, InStr(sFullPath, vbNullChar) - 1) ' Strip nulls
'      CoTaskMemFree pItem 'nettoyage
'    End If
'  End If

  If Right$(ReturnPath, 1) <> "\" And ReturnPath <> "" Then  'Could be "C:"
    FolderBrowse = ReturnPath & "\"
  End If

'If Right$(ReturnPath, 1) <> "\" And ReturnPath <> "" Then  'Could be "C:"
'    FolderBrowse = ReturnPath & "\"
'  End If

End Function

我没有任何错误消息,只是Catia应用程序被冻结。

问候

EN

回答 1

Stack Overflow用户

发布于 2019-08-01 17:13:33

我终于找到解决这个问题的办法了。声明不正确

这是一份好的声明

代码语言:javascript
复制
'API Declares

    Public Declare PtrSafe Function SendMessageA Lib "user32" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
    Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As LongPtr
    Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidList As LongPtr, ByVal lpBuffer As String) As Boolean
    Private Declare PtrSafe Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As LongPtr)

'BrowseInfo Type
    Public Type BROWSEINFO
        hWndOwner As LongPtr
        pidlRoot As LongPtr
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfnCallback As LongPtr
        lParam As LongPtr
        iImage As Long
    End Type

问候

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57292321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档