diff --git a/SlnkDWFImpl/JglUtil.cpp b/SlnkDWFImpl/JglUtil.cpp index f27ef9b..06873ff 100644 --- a/SlnkDWFImpl/JglUtil.cpp +++ b/SlnkDWFImpl/JglUtil.cpp @@ -9,22 +9,27 @@ HRESULT myAtlReportError(const CLSID & clsid, LPCTSTR lpszFormat, ...) va_list args; va_start(args, lpszFormat); - int nBuf; - char szBuffer[1024]; // Maximum voor wvsprintf!! + CString s; + s.FormatV(lpszFormat, args); - nBuf = _vsnprintf(szBuffer, sizeof(szBuffer), lpszFormat, args); if (errno) - nBuf = _snprintf(szBuffer, sizeof(szBuffer), - "%s\n(errno %d)", szBuffer, errno); - if (GetLastError()) - nBuf = _snprintf(szBuffer, sizeof(szBuffer), - "%s\n(lasterror may not apply) %d: %s", szBuffer, GetLastError(), myGetLastErrorMsg()); - // ASSERT(nBuf < _countof(szBuffer)); - - va_end(args); + { + CString s2; + s2.Format("%s\n(errno %d)", s, errno); + s = s2; + } - myTRACE(szBuffer); - return AtlReportError(clsid, szBuffer); + if (GetLastError()) + { + CString s2; + s2.Format("%s\n(lasterror may not apply) %d: %s", s, GetLastError(), myGetLastErrorMsg()); + s = s2; + } + + va_end(args); + + myTRACE(s); + return AtlReportError(clsid, s); } const CString myCString(LPCTSTR lpszFormat, ...) @@ -32,14 +37,12 @@ const CString myCString(LPCTSTR lpszFormat, ...) va_list args; va_start(args, lpszFormat); - int nBuf; - char szBuffer[1024]; // Maximum voor wvsprintf!! + CString s; + s.FormatV(lpszFormat, args); - nBuf = _vsnprintf(szBuffer, sizeof(szBuffer), lpszFormat, args); - va_end(args); - return CString(szBuffer); + return s; } void myDoTRACE(LPCTSTR lpszFormat, ...) @@ -47,32 +50,36 @@ void myDoTRACE(LPCTSTR lpszFormat, ...) va_list args; va_start(args, lpszFormat); - int nBuf; - char szBuffer[512]; + CString s; + s.FormatV(lpszFormat, args); - nBuf = _vsnprintf(szBuffer, sizeof(szBuffer), lpszFormat, args); - // ASSERT(nBuf < _countof(szBuffer)); - - OutputDebugString(szBuffer); + OutputDebugString(s); va_end(args); } -const char* myGetLastErrorMsg() +const CString myGetLastErrorMsg() { - static char MsgBuf [_MAX_PATH]; - - ::FormatMessage( - FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR) &MsgBuf, - sizeof(MsgBuf), - NULL - ); + LPSTR lpBuffer; + DWORD dwError = GetLastError(); + int nResult = 0; + if (dwError >= 12000 && dwError <= 12174) + nResult = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE, + GetModuleHandle("wininet.dll"), dwError, 0, + (LPSTR)&lpBuffer, 0, NULL); + else + nResult = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, dwError, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), + (LPSTR)&lpBuffer, 0, NULL); + if (nResult) + { + CString s(lpBuffer); + LocalFree(lpBuffer); + return s; + } - return MsgBuf; + return "Could not format errormessage"; } long myRound(double const &x) { diff --git a/SlnkDWFImpl/Jglutil.h b/SlnkDWFImpl/Jglutil.h index 53fbcd9..865a945 100644 --- a/SlnkDWFImpl/Jglutil.h +++ b/SlnkDWFImpl/Jglutil.h @@ -8,7 +8,7 @@ void myDoTRACE(LPCTSTR lpszFormat, ...); long myRound(double const &x); -const char* myGetLastErrorMsg(); +const CString myGetLastErrorMsg(); HRESULT myAtlReportError(const CLSID & clsid, LPCTSTR lpszFormat, ...); const CString myCString(LPCTSTR lpszFormat, ...);