views:

324

answers:

1

I'm currently in the process of moving some code from native C++ to managed C++ (pure). The code involves interacting with Windows Active Scripting. Currently our native code provides a class called 'ObjectDispatch' which implements 'IDispatch' (using ATL). This class implementation queries our own native class 'Object' at runtime to determine what methods and properties it supports and then forwards any 'IDispatch::Invoke' calls to this 'Object'.

I have seen examples using .NET COM interop which support 'IDispatch', however those implementations where either derived from IDL or where based on the specification of a .NET class, neither of which occurs at runtime.

It appears that .NET COM interop can generate a implementation of 'IDispatch' at compile time if you use the following attribute on a given class:

[ClassInterface(ClassInterfaceType::AutoDispatch)]

I'm assuming that I could dynamically generate a class at runtime that supports this attribute. However before I attempt that I'm wondering if anyone has any ideas how this might be achieved through similar means that were used with the native code.

Note that at this time the 'Object' class is remaining as a native class.

A: 

Got some official feedback from microsoft that "might" work. Have had no time to confirm but thought would be good idea to post in case anyone was interested.

    [Guid("00020400-0000-0000-c000-000000000046"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IDispatch
    {
        int GetTypeInfoCount();


    ITypeInfo GetTypeInfo(
        [MarshalAs(UnmanagedType.U4)] int iTInfo,
        [MarshalAs(UnmanagedType.U4)] int lcid);

    [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,
        [MarshalAs(UnmanagedType.U4)] int lcid,
        [MarshalAs(UnmanagedType.U4)] int dwFlags,
        ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams,
        [Out, MarshalAs(UnmanagedType.LPArray)] object[] pVarResult,
        ref System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo,
        [Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] pArgErr);
}

class IImplimentIDispatch : IDispatch
{
    public IImplimentIDispatch(object o)
        {
             _o = o;
…
karmasponge