com 返回值检验及异常处理
2009-09-10 10:13
120 查看
该文算是我对com异常的一点总结。
ms定义的com 返回值hresult的结构
//
// Values are 32 bit values layed out as follows:
//
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +---+-+-+-----------------------+-------------------------------+
// |Sev|C|R| Facility | Code |
// +---+-+-+-----------------------+-------------------------------+
//
// where
//
// Sev - is the severity code
//
// 00 - Success
// 01 - Informational
// 10 - Warning
// 11 - Error
//
// C - is the Customer code flag
//
// R - is a reserved bit
//
// Facility - is the facility code
//
// Code - is the facility's status code
//
//
可以通过下述方式检验返回值
SUCEED();FAILD();
打印错误:
WCHAR * GetErrorMessage(WCHAR *szBuffer, DWORD dwSize)
{
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
szBuffer,
dwSize - 1,
NULL
);
return szBuffer;
}
或者
WCHAR * GetErrorMessage(WCHAR *szBuffer, DWORD dwSize,HRESULT hr)
{
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
szBuffer,
dwSize - 1,
NULL
);
return szBuffer;
}
对于异常的处理:
try{....}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format(:%s/r/n%s",e.ErrorMessage(),(char*)e.Description());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
ms定义的com 返回值hresult的结构
//
// Values are 32 bit values layed out as follows:
//
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +---+-+-+-----------------------+-------------------------------+
// |Sev|C|R| Facility | Code |
// +---+-+-+-----------------------+-------------------------------+
//
// where
//
// Sev - is the severity code
//
// 00 - Success
// 01 - Informational
// 10 - Warning
// 11 - Error
//
// C - is the Customer code flag
//
// R - is a reserved bit
//
// Facility - is the facility code
//
// Code - is the facility's status code
//
//
可以通过下述方式检验返回值
SUCEED();FAILD();
打印错误:
WCHAR * GetErrorMessage(WCHAR *szBuffer, DWORD dwSize)
{
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
szBuffer,
dwSize - 1,
NULL
);
return szBuffer;
}
或者
WCHAR * GetErrorMessage(WCHAR *szBuffer, DWORD dwSize,HRESULT hr)
{
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
szBuffer,
dwSize - 1,
NULL
);
return szBuffer;
}
对于异常的处理:
try{....}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format(:%s/r/n%s",e.ErrorMessage(),(char*)e.Description());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
相关文章推荐
- android studio异常处理之[Error: com.android.ide.common.process.ProcessException]
- 我的理解:在有返回值的函数内是否应该使用异常处理?欢迎大家斧正
- 异常处理com.sun.image.codec.jpeg.JPEGImageEncoder
- 未处理的“System.Runtime.InteropServices.COMException”类型的异常出现在 comlayout.exe 中。其他信息: 检索 COM 类工厂中 CLSID 为 {0EC8CCC8-EBED-495E-9A9F-313
- com.android.build.api.transform.TransformException: java.util.zip.ZipException:异常处理
- 使用@RestController,@ExceptionHandler和@Valid,把检验和异常处理从主要业务逻辑里面抽离出来
- 第八节:Task的各类Task<TResult>返回值以及通用线程的异常处理方案。
- Getting error "No subject alternative names" when doing secure URL connection(针对处理https连接的安全异常)(转:http://www.coderanch.com/t/134
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到性能,甚至可能导致应用程序不响应或者使用的内存随时间不断累积
- R语言︱异常值检验、离群点分析、异常值处理
- c# 多播委托的 异常处理 如果有返回值 只返回最后一个返回值
- R语言︱异常值检验、离群点分析、异常值处理
- java从0单排(3)-经验(java.lang.ClassNotFoundException: com.mysql.jdbc.Driver异常处理办法)
- android studio异常处理之[Error: com.android.ide.common.process.ProcessException]
- 错误处理的返回--异常还是返回值
- C++异常处理解析3: 错误处理(返回值, 错误标志变量, 异常)各有千秋
- 异常信息:CLR无法从COM 上下文0x645e18 转换为COM上下文0x645f88,这种状态已持续60秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作.这种情况通常会影响到
- com.mongodb.MongoException$CursorNotFound: cursor not found on server异常处理
- 异常处理 DirectoryServiceCOMexception
- C#读写Word文档异常处理解析System.Runtime.InteropServices.COMException