从 C# 调用 IDispatch COM 接口的成员 [英] Calling a member of IDispatch COM interface from C#

查看:27
本文介绍了从 C# 调用 IDispatch COM 接口的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从实现 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 接口的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆