PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/include/error.c

http://shutdownguard.googlecode.com/
C | 49 lines | 33 code | 3 blank | 13 comment | 7 complexity | ab12a04576517be8d84106ded62f1277 MD5 | raw file
  1. /*
  2. Error message handler.
  3. Copyright (C) 2010 Stefan Sundin (recover89@gmail.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. */
  9. int showerror = 1;
  10. LRESULT CALLBACK ErrorMsgProc(INT nCode, WPARAM wParam, LPARAM lParam) {
  11. if (nCode == HCBT_ACTIVATE) {
  12. //Edit the caption of the buttons
  13. SetDlgItemText((HWND)wParam, IDYES, L"Copy error");
  14. SetDlgItemText((HWND)wParam, IDNO, L"OK");
  15. }
  16. return 0;
  17. }
  18. void Error(wchar_t *func, wchar_t *info, int errorcode, wchar_t *file, int line) {
  19. if (!showerror) {
  20. return;
  21. }
  22. //Format message
  23. wchar_t msg[1000], *errormsg;
  24. int length = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, errorcode, 0, (wchar_t*)&errormsg, 0, NULL);
  25. if (length != 0) {
  26. errormsg[length-2] = '\0'; //Remove that damn newline at the end of the formatted error message
  27. }
  28. swprintf(msg, L"%s failed in file %s, line %d.\nError: %s (%d)\n\n%s", func, file, line, errormsg, errorcode, info);
  29. LocalFree(errormsg);
  30. //Display message
  31. HHOOK hhk = SetWindowsHookEx(WH_CBT, &ErrorMsgProc, 0, GetCurrentThreadId());
  32. int response = MessageBox(NULL, msg, APP_NAME" Error", MB_ICONERROR|MB_YESNO|MB_DEFBUTTON2);
  33. UnhookWindowsHookEx(hhk);
  34. if (response == IDYES) {
  35. //Copy message to clipboard
  36. int size = (wcslen(msg)+1)*sizeof(wchar_t);
  37. OpenClipboard(NULL);
  38. EmptyClipboard();
  39. wchar_t *data = LocalAlloc(LMEM_FIXED, size);
  40. memcpy(data, msg, size);
  41. SetClipboardData(CF_UNICODETEXT, data);
  42. CloseClipboard();
  43. LocalFree(data);
  44. }
  45. }