问题描述
我想从实现 C# 中的 IDispatch 接口的 COM 对象调用 GetIdsOfNames 函数.我编写了以下代码,但由于 DISP_E_UNKNOWNNAME 失败.这是正确的方法吗?
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);
谢谢,
理查德
推荐答案
不可以,因为 InvokeMember 内部使用 GetIDsOfNames,而这个只检查实际方法,而不是 IDispatch 中的前 6 个.或者换句话说,不能使用 IDispatch 的方法 Invoke 调用 GetIDsOfNames.这就是 COM 的工作原理.
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.
这篇关于从 C# 调用 IDispatch COM 接口的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!