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();
}
catc开发者_高级运维h (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?
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
for E_OUTOFMEMORY
. That will generate an OutOfMemoryExcetpion
. The run time will map HRESULTS
it knows about to meaningful exceptions. See the mapping definition at MSDN.
精彩评论