Calling a member of IDispatch COM interface from C#(从 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 接口的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 C# 调用 IDispatch COM 接口的成员


- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 使用 rss + c# 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01