The title’s kind of a misnomer. This post is really to help me remember how to get a human-readable string from a Windows error code… I’m finally tired of always having to look it up :) . However, my current situation revolves around determining why a DLL (or *.so on Linux) failed to load, so that’s why this post it titled the way it is.

I like to disable the annoying default dialog that pops up in Windows when a library fails to load.

SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOOPENFILEERRORBOX);

Now, here’s the code to get a user friendly text string:

#ifdef WIN32
   LPVOID pStr = 0;
   DWORD_PTR args[1] = { (DWORD_PTR)pFilename };
   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
      FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_ARGUMENT_ARRAY,
      NULL,
      GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      (LPTSTR)&pStr,
      0,
      (va_list*)args);

   // TODO: Do something with the string pStr here.

   LocalFree(pStr);
#else
   // TODO: Call dlerror() and do something with the string.
#endif

Tagged with:  

Leave a Reply