如何从 c# 到 c++ 传递 bool IDL 中的 com 接口
我知道我错过了一些简单的东西,我对这些 com 东西几乎没有经验。
我想在 idl 的接口内执行此操作,
[id(5), helpstring("Returns true if the object is in a valid state.")]
HRESULT IsValid([out, retval] boolean bValid);
但这给出: [out] 参数不是指针。
好的,我明白了。
但是,在实现此功能的 C# 代码中,我无法从 IsValid() 方法返回 bool*,因为它不安全。
返回布尔值的正确方法是什么?
I know I'm missing something simple, I have next to no experience with these com things.
I would like to do this within an interface in an idl
[id(5), helpstring("Returns true if the object is in a valid state.")]
HRESULT IsValid([out, retval] boolean bValid);
However this gives : [out] paramter is not a pointer.
Ok, I understand that.
However, in the C# code implementing this, I can't return a bool* from the method IsValid() because it is unsafe.
What is the correct way for me to return the boolean value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
为了作为输出工作,它必须是指向值的指针; 这就是在 C++ 端的写入方式:
我不知道是否可以将类型写为
boolean
- 我只见过使用VARIANT_BOOL
。在 C# 方面,它将等同于:
换句话说,运行时可调用包装器 (RCW) 将实现方法签名的“更好”版本,并为您处理不安全的转换。 如果 C++ 实现返回
E_FAIL
(或E_WHATEVER
),则 RCW 的方法将抛出ComException
。您还可以考虑添加
[propget]
属性,这将使IsValid
可用作属性而不是方法。 与任何属性一样,只有在评估成本低廉并且没有副作用时才执行此操作(调试器将在您单步执行 C# 代码时对其进行评估)。Try:
In order to work as an output, it has to be a pointer to the value; this is how it will be written to on the C++ side:
I don't know if you can write the type as
boolean
- I've only ever seenVARIANT_BOOL
being used.On the C# side, it will become equivalent to:
In other words, the runtime callable wrapper (RCW) will implement a "nicer" version of the method signature and take care of the unsafe translation for you. If the C++ implementation returns
E_FAIL
(orE_WHATEVER
), then the RCW's method will throw aComException
.You might also consider adding the
[propget]
attribute, which will makeIsValid
available as a property instead of a method. As with any property, only do this if it is cheap to evaluate and has no side effects (the debugger will evaluate it as you step through C# code).