问题
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