question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem using IExplorerBrowserEvents and SafeHandles: SafeHandles cannot be marshaled from unmanaged to managed

See original GitHub issue

Hello 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:closed
  • Created 5 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
dahallcommented, Jan 14, 2019

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.

1reaction
dahallcommented, Jan 2, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Marshaling SafeHandles from unmanaged to managed
The error message is misleading. It's 100% possible to marshal safehandles from unmanaged to managed because that is how SafeHandles are ...
Read more >
WinEventProc first param needs to be changed to IntPtr I ...
Current model creates a runtime exception: 'Cannot marshal 'parameter #1': SafeHandles cannot be marshaled from unmanaged to managed.
Read more >
CA2006: Use SafeHandle to encapsulate native resources
Managed code uses to access native resources. ... Problems will occur if the IntPtr represents some native resource, such as memory, ...
Read more >
Using SafeHandles in Structs via Stateful Marshalling
SafeHandles provide reliable and secure means of managing the lifetime of unmanaged resources. It's commonly recommended to use SafeHandle ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found