0

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?.

Revious
  • 7,816
  • 31
  • 98
  • 147
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Why are you attempting to use the function? It reads like the kind of thing you do if you're implementing a shell extension with some kind of background work going on. That doesn't sound like a good fit for a Winforms project. – Roger Lipscombe May 16 '16 at 17:33
  • @Roger Lipscombe It is actually for the purest curiosity, I not yet managed to find an applicable helpful case for a WinForms project, however, I tried to use that function for curiosity to discover whether a child process having that reference can avoid to be killed when parent process is killed with a signal to kill child processes too, but the function returns E_FAIL so I can't test my absurd things. – ElektroStudios May 16 '16 at 17:43
  • 1
    Which instance of explorer would you expect to be returned? It's not as if your WinForms process is hosted by Explorer. It would seem that `E_FAIL` is precisely what would be expected. – David Heffernan May 16 '16 at 18:37
  • 1
    To add to what @DavidHeffernan says - `SHGetInstanceExplorer` seems to be a utility function added so Shell extensions can access the browser instance as a kind of global static. Until and unless you instantiate a shell browser in your process there will not be an instance to return. – Chris Becke May 17 '16 at 06:28
  • 2
    The documentation you linked to says "Retrieves an interface that allows hosted Shell extensions and other components to prevent their host process from closing prematurely." It is for plug-ins to extend the lifetime of the process they are running in. It sounds like you are attributing magical powers to it that aren't written in the documentation. – Raymond Chen May 17 '16 at 13:09
  • @Raymond Chen Yes, you are right, my fault all this. – ElektroStudios May 17 '16 at 13:31

1 Answers1

1

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
Community
  • 1
  • 1
Ajay
  • 18,086
  • 12
  • 59
  • 105