.NET 中 COMException HRESULT 错误代码的符号名称
我希望能够在 .NET 代码中使用 COM 组件返回的 HRESULT 的标准符号名称。例如,我希望能够编写如下代码:
try {
someComObject->DoSomething();
}
catch (COMException ex) {
if (ex.ErrorCode == E_FAIL) {
HandleFail();
}
else if (ex.ErrorCode == E_OUTOFMEMORY) {
HandleOutOfMemory();
}
else {
HandleComError(ex.ErrorCode);
}
}
但是,我在 .NET 框架中找不到定义 E_FAIL、E_OUTOFMEMORY、E_UNEXPECTED 等符号的任何位置。对于本机 Win32 应用程序,可以从
我当然可以自己定义它们,无论我在哪里需要它们,就像这样:
int E_FAIL = unchecked((int)0x80004005L);
但我更愿意从标准位置获取定义(如果有的话)。我想我曾经在某个地方发现过它们的定义,但我的 MSDN 搜索现在没有找到它们。
.NET 库中是否存在一组此类定义?
I would like to be able to use the standard symbolic names for HRESULTs returned by COM components in my .NET code. For example, I'd like to be able to write code like this:
try {
someComObject->DoSomething();
}
catch (COMException ex) {
if (ex.ErrorCode == E_FAIL) {
HandleFail();
}
else if (ex.ErrorCode == E_OUTOFMEMORY) {
HandleOutOfMemory();
}
else {
HandleComError(ex.ErrorCode);
}
}
However, I can't find anyplace in the .NET frameworks where symbols like E_FAIL, E_OUTOFMEMORY, E_UNEXPECTED, and so on are defined. For native Win32 apps, one can get the definitions from <winerror.h> (see http://msdn.microsoft.com/en-us/library/aa378137(VS.85).aspx).
I can certainly define them myself, wherever I need them, like this:
int E_FAIL = unchecked((int)0x80004005L);
But I would prefer to get the definitions from a standard place, if available. I think I found them defined somewhere once, but my MSDN searches aren't finding them now.
Are there a set of such definitions somewhere in the .NET libraries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为框架本身有这样的框架。也许您在 pinvoke.net 上找到了此列表。不用担心值的变化。 C 头文件中的值被编译到应用程序中,因此在不破坏现有的每个 COM 应用程序的情况下无法更改它们。
此外,您也不会收到
E_OUTOFMEMORY
的COMException
。这将生成一个OutOfMemoryExcetpion
。运行时会将其所知的HRESULTS
映射到有意义的异常。请参阅 MSDN 上的映射定义。I don't think there is one in the framework itself. Maybe you found this list at pinvoke.net. Don't worry about the values changes. The values in a C header file get compiled into the applications so they can't be changed without breaking every COM application in existence.
Also you won't receive a
COMException
forE_OUTOFMEMORY
. That will generate anOutOfMemoryExcetpion
. The run time will mapHRESULTS
it knows about to meaningful exceptions. See the mapping definition at MSDN.