我的意外处理
//
// 这是我处理意外的一个头文件,能独立使用。主要是处理Com只见调用的错误。
// 很朴实的,没有特别的修饰。本来是支持MFC的,后来改成了SDK方式的。
//
// Copyright (C) Sincere Corporation
#pragma once
#include <stdio.h>
#include <tchar.h>
#include <comdef.h>
#include <atltrace.h>
// 错误信息显示
__inline HRESULT WINAPI SITDisplayErrorToUser(_com_error& e) throw()
{
#if defined(DEBUG) | defined(_DEBUG)
TCHAR szErr[MAX_PATH];
_sntprintf(szErr, MAX_PATH-1,
_T("Error:\nCode = %x\nCode meaning = %s\nSource = %s\n"),
e.Error(), (TCHAR*)e.ErrorMessage(),(TCHAR*)e.Source());
szErr[MAX_PATH-1] = '\0';
#ifdef SIT_LOG_STATUS
TCHAR szLog[MAX_PATH];
_sntprintf(szLog, MAX_PATH-1, _T("%sError Description = %s"), szErr, (TCHAR*)e.Description());
szLog[MAX_PATH-1] = '\0';
SIT_LOG(strLog);
#endif //SIT_LOG_STATUS
_sntprintf(szErr, MAX_PATH-1,
_T("%sError Description = %s\n"), (TCHAR*)e.Description());
#else
TCHAR szErr[MAX_PATH];
_sntprintf(szErr, MAX_PATH-1, _T("调用模块出现%s "), e.ErrorMessage());
szErr[MAX_PATH-1] = '\0';
#endif //DEBUG
::MessageBox(NULL, szErr, _T("错误信息"), MB_OK|MB_ICONINFORMATION);
return E_FAIL;
}
__inline HRESULT WINAPI SITDisplayErrorToUser(TCHAR* strMsg) throw()
{
MessageBox(NULL, strMsg, _T("错误信息"), MB_OK|MB_ICONINFORMATION);
return E_FAIL;
}
// try...catch...块定义
// for com module operate
//#define SIT_SAFE_START try
//
//#define SIT_SAFE_END catch(_com_error &e) { return SITDisplayErrorToUser(e); } \
// catch(...) \
// { \
// return SITDisplayErrorToUser((TCHAR*)_T("读取数据时出现错误!")); \
// }
//
//#define SIT_SAFE_END_MSG(msg) catch(_com_error &e) { return SITDisplayErrorToUser(e); } \
// catch(...) \
// { \
// return SITDisplayErrorToUser((TCHAR*) msg); \
// }
//Define try...catch method.
#define Sit_Com_try try
#define Sit_Com_End(r) catch(_com_error& e) { SITDisplayErrorToUser(e); return r;} \
catch(...) \
{ \
SITDisplayErrorToUser((TCHAR*)_T("读取数据时出现错误!")); \
return r; \
}
#define Sit_Com_Out catch(_com_error& e) { SITDisplayErrorToUser(e); return; } \
catch(...) \
{ \
SITDisplayErrorToUser((TCHAR*)_T("读取数据时出现错误!")); \
return \
}
//Trace message
// 释放对象和变量
#define SIT_DELETE(p) { if(p) { delete (p); (p) = NULL; } }
#define SIT_DELETEB(p) { if(p) { delete[] (p); (p) = NULL; } }
#define SIT_DELETE_TYPE(p,type) { delete (type*)p; p=NULL;}
#define SIT_DELETE_ARRAY(p) { if(p) { delete[] (p); (p) = NULL; }}
#define SIT_RELEASE(p) { if(p) { (p)->Release(); (p) = NULL; }}
#define SIT_DESTROY(p) { if(p!=NULL) p->Destroy(); }
#define SIT_FREEBSTR(p) {::SysFreeString((p)); (p) = NULL; }
#define CheckNew(p) { if(p == NULL) return E_OUTOFMEMORY; }
// 角度操作
#define SIT_PI ((FLOAT) 3.141592654f)
#define SIT_1BYPI ((FLOAT) 0.318309886f)
#define SITToRadian( degree ) ((degree) * (SIT_PI / 180.0f))
#define SITToDegree( radian ) ((radian) * (180.0f / SIT_PI))
#define DELETE_POINTER(p) { if (NULL != p) { delete p; p = NULL; } }
#define RELEASE_INTERFACE(p) { IUnknown* pTmp = (IUnknown*)p; p = NULL; if (NULL != pTmp) pTmp->Release(); }
#define COUNTOF(x) ( sizeof(x) / sizeof(*x) )
// 直接显示调试信息的函数,非常酷,好好使用吧!
__inline HRESULT WINAPI SITTrace(const TCHAR* strFile, DWORD dwLine, HRESULT hr, const TCHAR* strMsg, BOOL bPopMsgBox)
{
_com_error e(hr);
TCHAR strErr[MAX_PATH-1];
_sntprintf(strErr, MAX_PATH-1, _T("%s(%d) : Message = %s\n"), strFile, dwLine, strMsg);
strErr[MAX_PATH-1] = '\0';
if(SUCCEEDED(hr))
{
ATLTRACE(strErr); return hr;
}
_sntprintf(strErr, MAX_PATH-1, _T("%sCode = %081x Code meaning = %s Source = %s"), strErr, (LONG)e.Error(), (TCHAR*)e.ErrorMessage(), (TCHAR*)e.Source());
strErr[MAX_PATH-1] = '\0';
#ifdef SIT_LOG_STATUS
TCHAR strLog[MAX_PATH];
_tcsncpy(strLog, strErr, MAX_PATH-1);
_tcsncat(strLog, _T("Error Description = "), MAX_PATH-1);
_tcsncat(strLog, e.Description(), MAX_PATH-1);
SIT_LOG(strLog);
#endif
_tcsncat(strErr, _T("Error Description = %s\n"), MAX_PATH-1);
_tcsncat(strErr, e.Description(), MAX_PATH-1);
ATLTRACE(strErr);
if(bPopMsgBox)
{
MessageBox(NULL, strErr, _T("Error"), MB_OK);
}
return hr;
}
#if defined(DEBUG) | defined(_DEBUG)
#define SITTRACE_MSG(str) SITTrace( __FILE__, (DWORD)__LINE__, 0, str, FALSE);
#define SITTRACE_ERR(str, hr) SITTrace( __FILE__, (DWORD)__LINE__, hr, str, FALSE);
#define SITTRACE_ERR_MSGBOX(str,hr) SITTrace( __FILE__, (DWORD)__LINE__, hr, str, TRUE);
#else
#define SITTRACE_MSG(str) (0L)
#define SITTRACE_ERR(str, hr) (hr)
#define SITTRACE_ERR_MSGBOX(str,hr) (hr)
#endif
#ifndef SITASSERT
#define SITASSERT ATLASSERT
#endif
#define SIT_DEBUGDLG_TRACE0 0x10120
#define WM_DEBUGDLG_MSG WM_USER+160
#if defined(DEBUG) | defined(_DEBUG)
#define TICK_START int nStartTickCount = GetTickCount();
#define TICK_END(parent, text) \
{ \
int nTickCount = GetTickCount() - nStartTickCount; \
TCHAR strTickMsg[MAX_PATH];\
_sntprintf(strTickMsg, MAX_PATH-1, _T("%s %d"), text, nTickCount);\
strTickMsg[MAX_PATH-1] = '\0';\
::SendMessage(parent, WM_DEBUGDLG_MSG, SIT_DEBUGDLG_TRACE0, (LPARAM)strTickMsg);\
}
#else
#define TICK_START 0
#define TICK_END(parent, text) 0
#endif
浙公网安备 33010602011771号