拾取HRESULT从C ++ COM inC#.NET代码返回 [英] Picking up HRESULT returned from C++ COM inC# .NET code

查看:103
本文介绍了拾取HRESULT从C ++ COM inC#.NET代码返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写的.NET代码调用用C ++编写的COM。



COM接口方法返回HRESULT以指示调用是否是成功。

但是,当我查看C#中的界面方法时,它们声称是无效函数。



我如何获取COM的HRESULT然后呢?



I''m calling a COM written in C++ from a .NET code written in C#.

The COM interface methods return HRESULT to indicate whether the call was successful.
However, when I look at the interface methods in C#, they claim to be void functions.

How do I pick up the HRESULTs from the COM then?

STDMETHOIMP CMyCOMObject::XMyCOMInterface::MyMethod()
{
	METHOD_PROLOGUE(CMyCOMObject, MyCOMInterface);
	return S_OK;
}







void Foo()
{
	MyCOM.IMyCOMInterface m_myCOM = new MyCOM.IMyCOMInterface;
	int nOut = m_myCOM.MyMethod(); // ERROR! C# thinks MyMethod is a void function!
}

推荐答案

您好,



默认情况下COM在输出中包装为 void ,但如果你的方法返回失败的HRESULT它会引发异常。

你可以处理COMException [ ^ ]。 COMException.HResult 是错误代码。



另一种方法是创建自己的包装并指定 PreserveSig [ ^ ]方法的属性并返回 int 而不是 void



有关指定的HRESULTS和.NET异常的更多信息:

如何:映射HRESULT和例外 [ ^ ]



问候,

Maxim。
Hello,

By default COM wrapped as void on output, but it throws an exception if your method returns failed HRESULT.
You can handle COMException[^] in catch block. The COMException.HResult is the error code.

Another way is to create your own wrapper and specify PreserveSig[^] Attribute for the method and makes returns int instead of void.

More information about specified HRESULTS and .NET exceptions:
How to: Map HRESULTs and Exceptions[^]

Regards,
Maxim.


这篇关于拾取HRESULT从C ++ COM inC#.NET代码返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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