从 C# 调用 IDispatch COM 接口的成员

11

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14