How do I write DllGetClassObject as a C# delegate?

2019-08-08 09:12发布

问题:

There is a dll method exported written in delphi: DllGetClassObject: function(const CLSID, IID: TGUID; var Obj): HResult; stdcall;

I need to write equivalent method in c# as a delegate. How should it looks like?

回答1:

Like this:

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate uint DllGetClassObjectDelegate(
    [MarshalAs(UnmanagedType.LPStruct)]
    Guid rclsid,
    [MarshalAs(UnmanagedType.LPStruct)]
    Guid riid,
    [MarshalAs(UnmanagedType.IUnknown, IidParameterIndex=1)]
    out object ppv
);

Source: http://blog.kutulu.org/2012/01/com-interop-part-9-custom-activations.html?m=1