Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. FormatMessage for unicode

FormatMessage for unicode

Scheduled Pinned Locked Moved ATL / WTL / STL
csharpc++visual-studiocomdata-structures
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    Windows 10, Visual Studio 2012,C++ I need a version of FormatMessage() for Unicode, WCHAR. Google and MSDN deny all my searches for same. I want to call a function of my own that accepts a pointer to WCHAR and the error number, and puts the resultant string in the passed array. Lacking that, I need a version of swprintf() that has an argument for a max number of characters to move into the string and accepts the LPSTR that is returned by FormatMessage().

    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

    L 1 Reply Last reply
    0
    • B bkelly13

      Windows 10, Visual Studio 2012,C++ I need a version of FormatMessage() for Unicode, WCHAR. Google and MSDN deny all my searches for same. I want to call a function of my own that accepts a pointer to WCHAR and the error number, and puts the resultant string in the passed array. Lacking that, I need a version of swprintf() that has an argument for a max number of characters to move into the string and accepts the LPSTR that is returned by FormatMessage().

      Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I presume you mean something like:

      void ShowError(DWORD dwError,
      PWSTR pszMessage,
      int maxMessageSize
      )
      {
      if (dwError == 0)
      {
      dwError = GetLastError();
      }
      if (FormatMessage(
      FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_IGNORE_INSERTS |
      FORMAT_MESSAGE_MAX_WIDTH_MASK, // don't include line breaks
      NULL, // no source message
      dwError, // message identifier
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
      pszMessage, // address of message buffer
      maxMessageSize, // size of user supplied buffer
      NULL // optional parameter list
      ) == 0)
      {
      // unknown error code
      swprintf_s(pszMessage, maxMessageSize, L"Error code: %d (0x%X)", dwError, dwError);
      }
      }

      Which you call by:

      WCHAR        szMessage\[512\];
      ShowError(nErrorCode, szMessage, \_countof(szMessage));
      wcout << "Exception: " << szMessage << endl;
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups