从 ShellBrowserWindow 对象到 ShellFolderView 对象的类型转换异常

Typecast exception from ShellBrowserWindow object to ShellFolderView object

正在寻求帮助以弄清楚为什么此类型转换无法在我的机器上运行。
此代码作为 提供给我遇到的另一个问题,但它对我不起作用。它适用于他们机器上的答案海报,但我在尝试从 ShellBrowserWindowShellFolderView.

的类型转换时遇到异常

我正在 Visual Studio Express 2013、运行 Windows 7 Pro X64 Sp1.该项目的目标框架是.Net Framework 4。我添加了对 Microsoft Internet ControlsMicrosoft Shell Controls and Automation 的引用,并为 Shell32SHDocVw 添加了 Imports 语句。 DLL版本如下: shell32.dll = 6.1.7601.18517 and shdocvw.dll = 6.1.7601.1822 我不确定我可能遗漏了什么。

代码如下所示。 (此代码在表单对象中)

     Imports EdmLib
     Imports Shell32
     Imports SHDocVw
     Public Class BlankForm   
         Private Sub BlankForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

             Dim bar As String() = GetExplorerSelectedFiles()
             Exit Sub        
             'The rest of my program is below this line - I'm just trying to test this one function right now...

         End Sub

         'Uses the windows shell to get the files selected in explorer
         Public Function GetExplorerSelectedFiles() As String()
             Dim ExplorerFiles As New List(Of String)
             Dim exShell As New Shell32.Shell
             Dim SFV As Shell32.ShellFolderView


             For Each window As SHDocVw.ShellBrowserWindow In DirectCast(exShell.Windows, SHDocVw.IShellWindows)
                 If (window.Document).GetType.Name <> "HTMLDocumentClass" Then
                     SFV = CType(window.Document, ShellFolderView) '<--This is where it fails!!
                     For Each fi As FolderItem In SFV.SelectedItems
                         ExplorerFiles.Add(fi.Path)
                     Next
                 End If
             Next

             Return ExplorerFiles.ToArray
         End Function
 End Class

SFV = CType(window.Document, ShellFolderView) 行导致以下消息:

An unhandled exception of type 'System.InvalidCastException' occurred in LaunchTemplateEPDM.exe

Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.ShellFolderView'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{29EC8E6C-46D3-411F-BAAA-611A6C9CAC66}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

我截取了 window 对象上的快速监视屏幕截图。 window.document 对象上的快速观察显示错误,指出它未定义或不可访问。

我 运行 查询 Microsoft.VisualBasic.Information.TypeName(window.document) 并且它 returns "IShellFolderViewDual3".

我修好了。 不确定为什么这会发生在我的系统上而不是你的系统上。 我发现 GetType.Name 总是只是 returns "System.__ComObject",无论对象是否为 ShellFolderViewHTMLDocumentClass 或其他。所以发生的事情是无论对象的实际类型是什么,它都通过了 <>"HTMLDocumentClass" 测试,因为它总是评估为 "System.__ComObject"。然后,当我们尝试在未实现 ShellFolderView 接口的对象上 运行 CType function 时,它会抛出该异常。

我最终偶然发现 this article which led me to experiment with the TypeName Function 似乎 return 实际类型,所以我得到了下面的工作代码:

Public Function GetExplorerSelectedFiles() As String()
    Dim ExplorerFiles As New List(Of String)
    Dim exShell As New Shell32.Shell

    For Each window As SHDocVw.ShellBrowserWindow In DirectCast(exShell.Windows, SHDocVw.IShellWindows)

        If TypeName(window.Document) Like "IShellFolderViewDual*" Then
            For Each fi As FolderItem In DirectCast(window.Document, ShellFolderView).SelectedItems
                ExplorerFiles.Add(fi.Path)
            Next
        End If
    Next

    Return ExplorerFiles.ToArray
End Function