Problem using IExplorerBrowserEvents and SafeHandles: SafeHandles cannot be marshaled from unmanaged to managed
See original GitHub issueHello mates,
currently I’m converting my WindowsExplorer-Replacement to use Vanara 2.1.0 wherever possible. So I implemented a Wrapper for ExplorerBrowser as seen e.g. in the Windows-API-Code-Pack.
What code is involved To get the ExplorerBrowser to work, I had to implement a class defining the following interfaces:
- Vanara.PInvoke.Shell32.IServiceProvider
- Vanara.PInvoke.Shell32.IExplorerPaneVisibility
- Vanara.PInvoke.Shell32.IExplorerBrowserEvents
- Vanara.PInvoke.Shell32.ICommDlgBrowser3
Expected behavior Using IExplorerBrowserEvents, my code should get noticed before trying to browse to a folder.
Describe the bug However, when trying to do so I get the following runtime exception:
Managed Debugging Assistant 'InvalidMemberDeclaration' Message=Managed Debugging Assistant 'InvalidMemberDeclaration' : 'The following error occurred while determining how to marshal the parameters of member 'OnNavigationPending' of type 'IExplorerBrowserEvents': System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #1': SafeHandles cannot be marshaled from unmanaged to managed. This is most likely due to an incompatible MarshalAs attribute on one of the parameters. '
Final Thoughts
Since I’m new to Vanara, I really don’t get the point where to fix this. Sorry if I’m wrong, but my assumption is, that since public void OnNavigationPending(Shell32.PIDL pidlFolder) { System.Windows.Forms.MessageBox.Show("OnNavigationPending!"); }
is called by Window Explorer’s ExplorerBrowser-Implementation, the Marshaller can’t marshal the native PIDL-Pointer back to Vanara’s safe Implementation of a PIDL.
Any ideas on this would be very appreciated! Thanks in advance, tajbender
Issue Analytics
- State:
- Created 5 years ago
- Comments:16 (16 by maintainers)
Top GitHub Comments
Pushed a much better tested assembly today to NuGet (2.1.2) where I’ve duplicated the samples from Microsoft to ensure the interfaces and structures are working for those exposed from Shell32. Hope this helps your issues. If not, let me know and I’ll share the test code I’ve used.
Well, this is one of those “Ah, crap” moments. I incorrectly used a SafeHandle to simplify the passing/retrieval of parameters and forgot that .NET doesn’t marshal those for in/out COM method parameters. Sorry. Let me look at what I can do to fix it.