Search code examples
c#vb.netwinapicomwindows-shell-extension-menu

SHGetInstanceExplorer function returns E_FAIL


When I try to use the SHGetInstanceExplorer function, it returns an E_FAIL HRESULT

I've defined these two functions with differents parameters (to test them), both definitions returns the same error:

<DllImport("shell32.dll", SetLastError:=False)>
Private Shared Function SHGetInstanceExplorer(
    <MarshalAs(UnmanagedType.IUnknown)> ByRef ppunk As Object
) As Integer
End Function

<DllImport("shell32.dll", SetLastError:=False)>
Private Shared Function SHGetInstanceExplorer(
    ByRef ppunk As stdole.IUnknown
) As Integer
End Function

I'm not really sure which kind of components more than a shell ext. are allowed to use this feature, I tried it from a WinForms application, maybe the problem is that?.


Solution

  • You probably want to use <Out> attribute along with ByRef since VB.Net doesn't have out keyword as in C#.

    <DllImport("shell32.dll", SetLastError:=False)>
    Private Shared Function SHGetInstanceExplorer(
        <Out()> ByRef ppunk As stdole.IUnknown
    ) As Integer
    End Function