Looking for help with non-COM C++ interfaces implementation. I can’t
find any reference on how to implement / use non-COM C++ interfaces in C#.
I have an application that uses COM interfaces from one of the Microsoft
services and have no problems using those. However, there are few
interfaces in this service defined as (C++ (not COM) interface)
regarding Microsoft documentation on this service API, also these
interfaces inherit the methods of the standard COM interface IUnknown
(same source).
Making story short, I can successfully call method that returns IntPtr
to the non-COM interface with reasonable pointer value, even
Marshal.QueryInterface works fine and returns proper pointer. My problem
is how to define C# wrapper for this type of interface, looks like
standard way that COM likes, does not work for me.
[ComVisible(false),
ComImport,
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("E561901F-03A5-4afe-86D0-72BAEECE7004")]
public interface IProviderNotifications
{
[PreserveSig()]
uint OnLoad([MarshalAs(UnmanagedType.IUnknown)] object pCallback);
[PreserveSig()]
uint OnUnload(int bForceUnload);
}
I tried “Marshal.GetObjectForIUnknown” on returned IntPtr, it just
throws an exception (No such interface) even without me trying to cast
it to specific type. I also tried “Marshal.PtrToStructure” and define my
wrapper class not as interface but as abstract class (blowup with
exception “Cannot create an abstract class”, reasonable).
I hope someone know anything about this issue, any help, suggestion,
hint greatly appreciated. I really stuck now.