2016-11-06 84 views
0

到現在爲止還沒有任何關於接口的知識,但我實際上正在努力實施this question的解決方案。從接口指針獲取接口/對象的正確方法?

我想我正確地獲取指向IShellFolder接口的指針,但我似乎無法實際使用該指針。如何獲得一個可用的接口,當有指針?

下面的代碼顯示了我在做什麼以及我懷疑存在哪些問題。

首先,我接口delarations

' I have wrapped the interfaces in their own namespace, "BinarusShell". 
' I have declared every interface's functions/subs in the same order as they are in the .idl files of the Windows Platform SDK 7.1. 
' Every function/sub has the <PreserveSig()> attribute so that I can see the native return values when debugging. 
' I'd like to keep the question as lean as possible, so I am showing only a few functions per interface. 
' Of course, I have implemented all of them (as they are in the respective .idl file). 

Namespace BinarusShell 

    <ComImport()> _ 
    <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _ 
    <Guid("000214FA-0000-0000-C000-000000000046")> _ 
    Public Interface IExtractIcon 

    ' The above GUID is the GUID for the UNICODE version of the interface, i.e. IExtractIconW. 
    ' Therefore, the UNICODE variant of strings is used in the parameters of the following functions. 
    ' See also the include files of the Windows SDK where this is done exactly that way as well. 

    <PreserveSig()> Function GetIconLocation(ByVal uFlags As UInteger, 
              <MarshalAs(UnmanagedType.LPWStr, SizeParamIndex:=2)> ByVal pszIconFile As String, 
              ByVal cchmax As UInteger, 
              ByRef piIndex As Integer, 
              ByRef pwFlags As UInteger) As Integer 

    ' ... 
    ' Here comes the second function (IExtractIcon has only two) ... 
    ' ...  

    End Interface 

    <ComImport()> _ 
    <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _ 
    <Guid("000214E6-0000-0000-C000-000000000046")> _ 
    Public Interface IShellFolder 

    <PreserveSig()> Function GetUIObjectOf(<[In]()> ByVal hwndOwner As IntPtr, 
              <[In]()> ByVal cidl As UInteger, 
              <[In]()> <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=1)> ByVal apidl() As IntPtr, 
              <[In]()> <MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid, 
              <[In](), Out()> ByRef rgfReserved As UInteger, 
              <Out()> ByRef ppv As IntPtr) As Integer 

    ' ... 
    ' Here come the other functions ... 
    ' ... 

    End Interface 

End Namespace 

其次,我的Windows API聲明,並 三,功能我想實現(這是我在哪裏卡住):

' The Windows API declarations are in class Win32Api. 
' This class is not wrapped in a certain namespace. 
' To keep this question as lean as possible, I am leaving out my declarations of constants or GUIDs. 
' I have taken all of them literally from the Windows Platform SDK 7.1. 

Public Class Win32Api 

    <DllImport("shell32.dll", CharSet:=CharSet.Auto)> _ 
    Private Shared Function SHBindToParent(<[In]()> ByVal pidl As IntPtr, 
             <[In]()> <MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid, 
             <Out()> ByRef ppv As IntPtr, 
             <Out()> ByRef ppidlLast As IntPtr) As Integer 
    End Function 

    <DllImport("shell32.dll", CharSet:=CharSet.Auto)> _ 
    Private Shared Function SHGetKnownFolderIDList(<[In]()> <MarshalAs(UnmanagedType.LPStruct)> ByVal rfid As Guid, 
               <[In]()> ByVal dwFlags As UInteger, 
               <[In]()> ByVal hToken As IntPtr, 
               <Out()> ByRef ppidl As IntPtr) As Integer 
    End Function 

' ----------------------------------------------------- 

' The function I would like to implement is in class Win32Api as well. 
' The function at this stage does nothing which is useful for the outer world; 
' It is just meant for code which I am stepping through with the debugger. 
' The comments below show the result of every step and my understanding about what has happened. 

    Public Shared Function IconTest() As Boolean 

    Dim ui_ReturnFlags As UInteger 
    Dim i_Result As Integer 
    Dim intptr_CurrentPidlAbsolute As IntPtr, 
     intptr_ParentIShellFolder As IntPtr, 
     intptr_CurrentIExtractIcon As IntPtr 
    Dim arintptr_RelPidList(0) As IntPtr 
    Dim ishellfolder_Parent As BinarusShell.IShellFolder 
    Dim iextracticon_Extractor As BinarusShell.IExtractIcon 

    i_Result = SHGetKnownFolderIDList(FOLDERID_System, 0, 0, intptr_CurrentPidlAbsolute) 
    ' The above seems to work. i_Result is 0 which means success, and 
    ' intptr_CurrentPidlAbsolute has a reasonable value now. 

    i_Result = SHBindToParent(intptr_CurrentPidlAbsolute, IID_IShellFolder, intptr_ParentIShellFolder, arintptr_RelPidList(0)) 
    ' The above seems to work. i_Result is 0 which means success, and 
    ' intptr_ParentIShellFolder and arintptr_RelPidList(0) have reasonable values now. 

    ' Now the PROBLEMS begin. I have no clue how to get an interface/object 
    ' from intptr_ParentIShellFolder which I can use from within VB.net. 
    ' Therefore, I just have tried the following (but believing that it is wrong): 
    ishellfolder_Parent = DirectCast(Marshal.GetObjectForIUnknown(intptr_ParentIShellFolder), BinarusShell.IShellFolder) 
    ' This does not throw an error, but I have no clue what actually is happening. 
    ' Marshal.GetObjectForIUnknown, as the name implies, should return an object which 
    ' encapsulates an IUnknown interface, but I am giving a pointer to an IShellFolder 
    ' interface, so I really wonder why it doesn't freak out. The original idea 
    ' to try it that way was that I believe that IShellFolder inherits IUnknown, 
    ' so it *eventually* could work that way. Could somebody comment about that? 

    i_Result = ishellfolder_Parent.GetUIObjectOf(0, 1, arintptr_RelPidList, IID_IExtractIcon, ui_ReturnFlags, intptr_CurrentIExtractIcon) 
    ' Now this is the point where I am completely puzzled. i_Result is still 0, 
    ' which means success, but intptr_CurrentIExtractIcon is 0 as well, meaning 
    ' that something has gone horribly wrong. According to my understanding of 
    ' the MSDN documentation, GetUIObjectOf MUST NOT return S_OK if something 
    ' has gone wrong; in other words, if it returns S_OK, then intptr_CurrentIExtractIcon 
    ' MUST be set to a reasonable value. Could somebody explain what's happening? 

    ' I have left out the rest of the function because it wouldn't make any sense 
    ' to use the NULL pointer for any further action. 

    End Function 

End Class 

任何想法我做錯了什麼?從文檔中,我得到每隔文件夾項目(虛擬或不)的印象和文件公開IExtractIcon。

+0

Marshal.GetTypedObjectForIUnknown()。我懷疑當你不發佈可運行代碼時,有人會爲你調試它。 –

+0

@HansPassant感謝您的回覆。我已經嘗試過這種方法(而不是我上面顯示的),但也無法獲得IExtractIcon接口。同時,我認爲我發現了我的錯誤(請參閱下面的答案)。你認爲這個問題和答案是有用的嗎?如果沒有,我會刪除整個事情... – Binarus

回答

0

回答我的問題:當在VB.net定義IShellFolder接口,我忘了包括BindToFolder()功能(你真的不應該試圖03:00點到落實的事情,特別是如果你沒有任何的線索那些事情直到1小時之前)。

當然,作爲記錄無處不在,這又取得了IShellFolder界面上產生錯誤的結果(不是真的知道我在說一下,因爲BindToFolder()GetUIObjectOf()之前在接口定義,反過來後者自帶GetDisplayNameOf()之前,我可以想象,實際上GetDisplayNameOf()已被調用時,我的代碼GetUIObjectOf(),這將解釋奇怪的結果)。

現在我已更正了我的IShellFolder定義(即我添加了缺少的函數),並且對我的其他接口定義(那些沒有錯誤)進行了雙重檢查,並且所有工作都按預期工作。