w32err.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <windows.h>
  2. #include "w32err.h"
  3. /*
  4. * Description: the windows32 version of perror()
  5. *
  6. * Returns: a pointer to a static error
  7. *
  8. * Notes/Dependencies: I got this from
  9. * comp.os.ms-windows.programmer.win32
  10. */
  11. char *
  12. map_windows32_error_to_string (DWORD ercode) {
  13. /* __declspec (thread) necessary if you will use multiple threads */
  14. __declspec (thread) static char szMessageBuffer[128];
  15. /* Fill message buffer with a default message in
  16. * case FormatMessage fails
  17. */
  18. wsprintf (szMessageBuffer, "Error %ld", ercode);
  19. /*
  20. * Special code for winsock error handling.
  21. */
  22. if (ercode > WSABASEERR) {
  23. HMODULE hModule = GetModuleHandle("wsock32");
  24. if (hModule != NULL) {
  25. FormatMessage(FORMAT_MESSAGE_FROM_HMODULE,
  26. hModule,
  27. ercode,
  28. LANG_NEUTRAL,
  29. szMessageBuffer,
  30. sizeof(szMessageBuffer),
  31. NULL);
  32. FreeLibrary(hModule);
  33. }
  34. } else {
  35. /*
  36. * Default system message handling
  37. */
  38. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  39. NULL,
  40. ercode,
  41. LANG_NEUTRAL,
  42. szMessageBuffer,
  43. sizeof(szMessageBuffer),
  44. NULL);
  45. }
  46. return szMessageBuffer;
  47. }