1234567891011121314151617181920212223242526272829303132333435363738394041 |
- #pragma once
- #include <string>
- //#include "TmplBlockBuffer.tlh"
- //#include "TmplBlockBuffer.tli"
- // 通过调用 FormtMessage, 把 Win32 错误码转换成字符串
- inline std::string ErrorCodeToString (DWORD errorCode)
- {
- std::string rc;
- if (errorCode == 0) errorCode = GetLastError ();
- std::string fs = std::to_string (errorCode);
- rc += "<Error Code=";
- rc += fs;
- rc += "> : ";
- const int nPreAlloc = 2048;
- //ECOM::Utility::TmplUniqueBuffer <char> errmsg;
- // auto pszBuffer = errmsg.GetBufferSetCount (nPreAlloc);
- std::string errmsg;
- errmsg.reserve (nPreAlloc);
- auto pszBuffer = errmsg.data ();
- auto len = ::FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- errorCode, // GetLastError (),
- MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) pszBuffer,
- nPreAlloc,
- NULL);
- rc.append (pszBuffer, len - 2); // -2 : 目的是删除尾部的回车/换行
- return std::move (rc);
- }
|