Does .NET define common HRESULT values? [duplicate]

落花浮王杯 提交于 2019-12-31 04:22:34

问题


I'm creating a COM class in C#, which will be called from unmanaged C++. I want to use ThrowExceptionForHR but I'd rather not have to hard-code HRESULT numeric values.

I was expecting there would be some enum of common HRESULT values in .Net somewhere?

Put another way, where can I find named symbols which map to HRESULT values to pass into ThrowExceptionForHR?

Update:

MS talk about it in this page: HRESULT Information in Managed Code. They reference VSConstants class Microsoft.VisualStudio.VSConstants but when I try to use this, it claims that namespace doesn't exist.


回答1:


Yes, .NET predefines HRESULT values. You use them by not helping, throw a standard .NET exception just the way you would do it if a managed program uses your library. The exception's HResult property value sometimes matches a common HRESULT value if the match is obvious (like OutOfMemoryException == 0x800700E), in general a 0x8013xxxx value.

The xxxx values are widely documented and listed in the CorError.h SDK header file. IErrorInfo::GetDescription() gives you the exception's Message property value. Automagically localized, nice. You can't get the holy stack trace.



来源:https://stackoverflow.com/questions/33036757/does-net-define-common-hresult-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!