NWindowsAPIException.cpp
Upload User: dzyhzl
Upload Date: 2019-04-29
Package Size: 56270k
Code Size: 1k
Development Platform:

C/C++

  1. #include "nwindowsapiexception.h"
  2. #include<windows.h>
  3. //构造函数,通过GetLastError()取得系统模块错误,并初始化ErrorType,ErrorInfo
  4. NWindowsAPIException::NWindowsAPIException(const char * ThrowFileName, int ThrowLineNumber,const char * ModuleName)
  5. :NBaseException(ThrowFileName,ThrowLineNumber)
  6. {
  7. ErrorType = GetLastError();
  8. HMODULE hModule = NULL; // default to system source
  9. DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
  10. FORMAT_MESSAGE_IGNORE_INSERTS |
  11. FORMAT_MESSAGE_FROM_SYSTEM ;
  12. hModule = GetModuleHandle(ModuleName); 
  13. if(hModule != NULL)
  14. {
  15. dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;
  16. }
  17. LPVOID lpMsgBuf;
  18. FormatMessageA( 
  19. dwFormatFlags,
  20. hModule,
  21. ErrorType,
  22. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  23. (LPTSTR)& lpMsgBuf,
  24. 0,
  25. NULL 
  26. );
  27. ErrorInfo = string("系统模块") + string(ModuleName) + string("内部错误:") + string((char *)lpMsgBuf);  
  28. LocalFree( lpMsgBuf );
  29. }
  30. NWindowsAPIException::~NWindowsAPIException(void)
  31. {
  32. }
  33. void AssertWindowsAPI(int result,char * FileName,int Line,char * ModuleName)
  34. {
  35. if(result ==NULL)
  36. {
  37. throw NWindowsAPIException(FileName,Line,ModuleName);
  38. }
  39. }