gpt4 book ai didi

c# - 编码 C# 类型以调用 C++ IDispatch 接口(interface)导致类型不匹配

转载 作者:行者123 更新时间:2023-11-30 01:27:22 27 4
gpt4 key购买 nike

我正在尝试使用以下方法定义调用 c++ IDispatch 接口(interface):

ATL HRESULT TestFunc(long Command, [in, out] long* pData, [in, out] BSTR* pString, [out, retval] long* pRC);

// ...
Type t = Type.GetTypeFromProgID( "IMyTestInterfce" );
Object so = Activator.CreateInstance(t);
Object[] args = new Object[3];
args[0] = -8017;
args[1] = 0;
args[2] = "";
Object result = so.GetType().InvokeMember("TestFunc", BindingFlags.InvokeMethod, null, so, args);

调用的结果是类型不匹配,但我不确定原因。

InnerException = {"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"}`

谢谢

最佳答案

您的问题是第二个和第三个参数(pDatapString)在它们的定义中被标记为[in, out],这意味着它们在 C# 中转换为 ref 参数。您需要使用 the overload of InvokeMember它接受 ParameterModifier[] 的参数以指定这些参数应该通过引用传递,而不是通过值传递。 ParameterModifier 数组应包含一个元素,该元素将第二个和第三个索引指定为 true,以表明它们是通过引用传递的。

ParameterModifier modifier = new ParameterModifier(3);
modifier[1] = true;
modifier[2] = true;

Object result = so.GetType().InvokeMember(
"TestFunc", // name
BindingFlags.InvokeMethod, // invokeAttr
null, // binder
so, // target
args, // args
new ParameterModifier[] { modifier }, // modifiers
null, // culture
null // namedParameters
);

关于c# - 编码 C# 类型以调用 C++ IDispatch 接口(interface)导致类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9066224/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com