Calling a member of IDispatch COM interface from C

2019-01-11 21:23发布

问题:

I wanted to call the GetIdsOfNames function from a COM object that implements the IDispatch interface in c#. I've written the following code but it fails with the DISP_E_UNKNOWNNAME. Is this the correct approach to do this?

 Object so = Activator.CreateInstance(Type.GetTypeFromProgID("ProgID"));            
 Object[] args = new Object[5];
 string[] rgsNames = new string[1];
 rgsNames[0] = "PrintNormal";
 uint LOCALE_SYSTEM_DEFAULT = 0x0800;
 uint lcid = LOCALE_SYSTEM_DEFAULT;
 int cNames = 1;
 int[] rgDispId = new int[1];
 args[0] = IntPtr.Zero;
 args[1] = rgsNames;
 args[2] = cNames;
 args[3] = lcid;
 args[4] = rgDispId;             
 Object result = so.GetType().InvokeMember("GetIDsOfNames", BindingFlags.InvokeMethod, null, so, args);

Thanks,

Richard

回答1:

No you cannot, because InvokeMember internally uses GetIDsOfNames, and this one only checks actual methods, not the first 6 in IDispatch. Or in other words, GetIDsOfNames cannot be invoked using IDispatch's method Invoke. That is how COM works.



回答2:

Yes, doesn't work, the RCW doesn't expose the IDispatch methods. You have to obtain the IDispatch interface reference explicitly. This worked well:

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

class Program {
    static void Main() {
        Object so = Activator.CreateInstance(Type.GetTypeFromProgID("SAPI.SpVoice"));
        string[] rgsNames = new string[1];
        int[] rgDispId = new int[1];
        rgsNames[0] = "Speak";
        IDispatch disp = (IDispatch)so;
        Guid dummy = Guid.Empty;
        disp.GetIDsOfNames(ref dummy, rgsNames, 1, 0x800, rgDispId);
        Console.WriteLine(rgDispId[0]);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00020400-0000-0000-C000-000000000046")]
    private interface IDispatch {
        int GetTypeInfoCount();
        [return: MarshalAs(UnmanagedType.Interface)]
        ITypeInfo GetTypeInfo([In, MarshalAs(UnmanagedType.U4)] int iTInfo, [In, MarshalAs(UnmanagedType.U4)] int lcid);
        void GetIDsOfNames([In] ref Guid riid, [In, MarshalAs(UnmanagedType.LPArray)] string[] rgszNames, [In, MarshalAs(UnmanagedType.U4)] int cNames, [In, MarshalAs(UnmanagedType.U4)] int lcid, [Out, MarshalAs(UnmanagedType.LPArray)] int[] rgDispId);
    }
}


标签: c# com