如何在C#中使用HRESULT [英] How to use HRESULT in C#

查看:300
本文介绍了如何在C#中使用HRESULT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

有人可以告诉我是否有一种简单的方法可以在C#中使用 HRESULT 吗?
我有一个 DLL ,它使用HRESULT来表示错误/成功代码,我正在尝试在我的C#代码中使用此 DLL .我无法添加对 DLL 文件的引用. VS2010 .NET 4.0无法将 dll 文件识别为程序集组件.因此,我使用 interopservice 使用导入的 dll 文件,并指向函数.
函数应定义为HRESULT someFunction(),返回的代码我需要使用 SUCCEEDED(HRESULT hr) FAILED(HRESULT hr)进行评估.
感谢您的帮助.

Hello everyone

Can some one tell me if there is an easy way to use HRESULT in C#?
I have a DLL that uses HRESULT for error/success codes and I am trying to use this DLL in my c# code. I can not add a reference to the DLL file. VS2010 .NET 4.0 does not recognize the dll file as an assembly component. Therefore I use import dll file using interopservice and entry point to the functions.
Functions should be defined as HRESULT someFunction() and the return code I need to evaluate it using SUCCEEDED(HRESULT hr) and FAILED(HRESULT hr).
Thanks for the help.

推荐答案

问题与HRESULT无关.您将需要使用与COM中相同的方式来使用它.唯一的问题是在.NET程序集中使用非托管DLL.

有两种方法可以实现.

第一个是P/Invoke.如果您需要学习P/Invoke,请参阅:
http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/en-us/library/Aa712982 [ ^ ].

此CodeProject文章也可能有用:
基本P/调用 [
关于C ++/CLI,请参阅:
http://en.wikipedia.org/wiki/C%2B%2B/CLI [ ^ ],
http://www.ecma-international.org/publications/standards/Ecma-372.htm [^ ],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ].

—SA
The question has nothing to do with HRESULT. You would need to use it the same ways as in COM. The only problem is using unmanaged DLL in your .NET assembly.

There are two ways to do it.

First one is P/Invoke. If you need learn P/Invoke, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

This CodeProject article can also be useful:
Essential P/Invoke[^].

Another approach is using C++/CLI. You can create a mixed-mode (managed+unmanaged) C++/CLI DLL. You can mix C++ with C++/CLI and wrap all native calls to your native DLL into .NET C++/CLI "ref" classes. The obtained executable can be used like a regular .NET assembly and referenced by your C# assembly.

About C++/CLI, please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

—SA


HRESULT是Int32值-因此在DLLImport声明中使用该值,并对照有效值范围检查返回的实际int.这里有一个常见的列表: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378137(v=vs.85).aspx [ http://msdn.microsoft.com/zh-cn/library/cc704587(v = prot.10).aspx [
HRESULT is a Int32 value - so use that in your DLLImport declaration, and check the actual int returned against the range of valid values. There is a list of common ones here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378137(v=vs.85).aspx[^]
and a more extensive list here: http://msdn.microsoft.com/en-us/library/cc704587(v=prot.10).aspx[^]


这篇关于如何在C#中使用HRESULT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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