@sm2GVFR ,
What actually did help:
1) In your C# code, declare the IDispatch interface explicitely:
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00020400-0000-0000-C000-000000000046")]
public interface IDispatch
{
[PreserveSig]
int GetTypeInfoCount(out int Count);
[PreserveSig]
int GetTypeInfo
(
[MarshalAs(UnmanagedType.U4)] int iTInfo,
[MarshalAs(UnmanagedType.U4)] int lcid,
out System.Runtime.InteropServices.ComTypes.ITypeInfo typeInfo
);
[PreserveSig]
int GetIDsOfNames
(
ref Guid riid,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)]
string[] rgsNames,
int cNames,
int lcid,
[MarshalAs(UnmanagedType.LPArray)] int[] rgDispId
);
[PreserveSig]
int Invoke
(
int dispIdMember,
ref Guid riid,
uint lcid,
ushort wFlags,
ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams,
out object pVarResult,
ref System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo,
out UInt32 pArgErr
);
}
2) Derive your class, which you try to get from outside application, from IDispatch:
[ComVisible(true)]
[Guid("08BBAD0D-51AD-450D-9CF9-50B26F656CBD")]
[ProgId("MY_PROG_ID")]
public class MyClass : IDispatch, IExtensionApplication
3) You don't need to implement IDispatch methods actually:
int IDispatch.GetTypeInfoCount(out int Count)
{
throw new NotImplementedException(); // never called actually, added just for compatibility of .NET Core assemblies with IDispatch calls from native code
}
int IDispatch.GetTypeInfo(int iTInfo, int lcid, out ITypeInfo typeInfo)
{
throw new NotImplementedException(); // never called actually, added just for compatibility of .NET Core assemblies with IDispatch calls from native code
}
int IDispatch.GetIDsOfNames(ref Guid riid, string[] rgsNames, int cNames, int lcid, int[] rgDispId)
{
throw new NotImplementedException(); // never called actually, added just for compatibility of .NET Core assemblies with IDispatch calls from native code
}
int IDispatch.Invoke(int dispIdMember, ref Guid riid, uint lcid, ushort wFlags, ref DISPPARAMS pDispParams, out object pVarResult, ref EXCEPINFO pExcepInfo, out uint pArgErr)
{
throw new NotImplementedException(); // never called actually, added just for compatibility of .NET Core assemblies with IDispatch calls from native code
}
4) That's it. I don't have 100% explanation why it works, I just found (during debugging) that on some stage your class is checked on the IDispatch interface presense in base interfaces, and if this interface is not found, the InvalidCastException or something is thrown. Hope this helps.