从 c# .net 代码返回 S_OK 或 E_FAIL 的值是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/362861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:35:41  来源:igfitidea点击:

What values to return for S_OK or E_FAIL from c# .net code?

c#.netcomreturn-value

提问by Rory

I'm implementing a COM interface that should return int values either S_OKor E_FAIL. I'm ok returning S_OKas I get that back from another call (Marshal.QueryInterface), but if I want to return a failure value what actual value do I use for E_FAIL?

我正在实现一个 COM 接口,它应该返回 int 值S_OKE_FAIL. S_OK当我从另一个调用 (Marshal.QueryInterface) 中返回时,我可以返回,但是如果我想返回一个失败值,我使用的实际值是什么E_FAIL

(It's such a basic fundamental question that it's hard to find an answer to)

(这是一个基本的基本问题,很难找到答案)

Assuming it's a specific number defined in the Win32 API, is there way to use it within .net code without declaring my own constant?

假设它是 Win32 API 中定义的特定数字,有没有办法在 .net 代码中使用它而不声明我自己的常量?

thanks!

谢谢!

Update (answered below):

更新(在下面回答):

Maybe I'm being a complete plonker, but I'm having problems with this. According to my Platform SDK, HRESULT is a LONG, which is a 32-bit signed integer, right? So possible values –2,147,483,648 to 2,147,483,647. But 0x80004005 = 2,147,500,037 which is > 2,147,483,647. What gives!?

也许我是一个彻头彻尾的笨蛋,但我遇到了这个问题。根据我的平台 SDK,HRESULT 是一个 LONG,它是一个 32 位有符号整数,对吗?所以可能的值是 –2,147,483,648 到 2,147,483,647。但是 0x80004005 = 2,147,500,037,即 > 2,147,483,647。是什么赋予了!?

This means when I try to put this in my code:

这意味着当我尝试将其放入我的代码时:

const int E_FAIL = 0x80004005;

I get a compiler error Cannot implicitly convert type 'uint' to 'int'.

我收到编译器错误无法将类型“uint”隐式转换为“int”。

Update 2:

更新 2:

I'm going to declare it like this:

我要这样声明:

const int E_FAIL = -2147467259;

because if I try to do something like this:

因为如果我尝试做这样的事情:

const UInt32 E_FAIL = 0x80004005;
return (Int32)E_FAIL;

I get a compiler error Constant value '2147500037' cannot be converted to a 'int' (use 'unchecked' syntax to override)

我收到编译器错误常量值“2147500037”无法转换为“int”(使用“unchecked”语法覆盖)

Phew! Who knew how tricky it would be to declare a standard return value.... Somewhere there must be a class lurking that I should have used like return Win32ReturnCodes.E_FAIL;... sigh

呼!谁知道声明一个标准的返回值会是多么棘手......某处必须有一个潜伏的类,我应该使用像return Win32ReturnCodes.E _FAIL; ……叹息

ULTIMATE SOLUTION:

最终解决方案:

I now do this by getting the (massive but very useful) HRESULT enum from pinvoke.netand adding it to my solution. Then use it something like this:

我现在通过从pinvoke.net获取(大量但非常有用的)HRESULT 枚举并将其添加到我的解决方案中来做到这一点。然后像这样使用它:

return HRESULT.S_OK;

采纳答案by Binary Worrier

E_FAIL is Hex 80004005 in WinError.h

E_FAIL 是 WinError.h 中的十六进制 80004005

You can see the full WinError.h file here. You don't have to install C++ just to see the values.

您可以在此处查看完整的WinError.h 文件。您不必安装 C++ 来查看值。

UPDATE:

更新:

The signed and unsigned versions of 0x80004005 are just two representations of the same bit mask. If you're getting a casting error then use the negative signed value. When casted to an UN signed long it will be the "correct" value. Test this yourself in C#, it'll work e.g.

0x80004005 的有符号和无符号版本只是同一位掩码的两种表示形式。如果您遇到转换错误,请使用负符号值。当转换为联合国签名的 long 时,它将是“正确”的值。在 C# 中自己测试一下,它会工作,例如

This code

这段代码

    static void Main(string[] args)
    {
        UInt32 us = 0x80004005;
        Int32 s = (Int32)us;

        Console.WriteLine("Unsigned {0}", us);
        Console.WriteLine("Signed {0}", s);
        Console.WriteLine("Signed as unsigned {0}", (UInt32)s);

        Console.ReadKey();
    }

will produce this output

将产生这个输出

  • Unsigned 2147500037
  • Signed -2147467259
  • Signed as unsigned 2147500037
  • 未签名 2147500037
  • 签名 -2147467259
  • 签名为未签名 2147500037

So it's safe to use -2147467259 for the value of E_FAIL

所以使用 -2147467259 作为 E_FAIL 的值是安全的

回答by Rob Prouse

From WinError.h for Win32

来自 Win32 的 WinError.h

#define E_FAIL _HRESULT_TYPEDEF_(0x80004005L)

To find answers like this, use visual studio's file search to search the header files in the VC Include directory of your visual studio install directory.

要找到这样的答案,请使用 Visual Studio 的文件搜索来搜索 Visual Studio 安装目录的 VC Include 目录中的头文件。

C:\Program Files\Microsoft Visual Studio 9.0\VC\include

回答by Cédric Guillemette

You should download the platform SDK (link)

您应该下载平台 SDK(链接

I searched through the code and found the following in winerror.h

我搜索了代码,在 winerror.h 中找到了以下内容

typedef long HRESULT;

#ifdef RC_INVOKED
#define _HRESULT_TYPEDEF_(_sc) _sc
#else // RC_INVOKED
#define _HRESULT_TYPEDEF_(_sc) ((HRESULT)_sc)
#endif // RC_INVOKED

#define E_FAIL _HRESULT_TYPEDEF_(0x80004005L)
#define S_OK  ((HRESULT)0x00000000L)

You should return E_FAIL instead of the value. Simply include winerror.h.

您应该返回 E_FAIL 而不是值。只需包含 winerror.h。

回答by cliffwi

I've used this:

我用过这个:

const int EFail = int.MinValue + 0x00004005;

to compromise between readability (if you're used to the hex codes) and C#'s restriction.

在可读性(如果您习惯于十六进制代码)和 C# 的限制之间做出妥协。

回答by Simon Giles

Use the "unchecked" keyword to do this.

使用“unchecked”关键字来做到这一点。

e.g.

例如

const int E_FAIL = unchecked((int)0x80004005);

回答by basiphobe

VSConstantsmight do you want you want:

VSConstants可能是你想要的:

VSConstants.S_OK Field

VSConstants.S_OK 字段

Generic HRESULT for success.

成功的通用 HRESULT。

Namespace: Microsoft.VisualStudio

命名空间:Microsoft.VisualStudio

Assembly: Microsoft.VisualStudio.Shell.10.0 (in Microsoft.VisualStudio.Shell.10.0.dll)

程序集:Microsoft.VisualStudio.Shell.10.0(在 Microsoft.VisualStudio.Shell.10.0.dll 中)