Meer CString gebruik ipv. _(v)snprintf varianten

svn path=/Slnkdwf/trunk/; revision=20043
This commit is contained in:
Jos Groot Lipman
2013-12-03 21:26:50 +00:00
parent 4b1f0a3dd5
commit 50d73d7787
2 changed files with 45 additions and 38 deletions

View File

@@ -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);
{
CString s2;
s2.Format("%s\n(errno %d)", s, errno);
s = s2;
}
if (GetLastError())
nBuf = _snprintf(szBuffer, sizeof(szBuffer),
"%s\n(lasterror may not apply) %d: %s", szBuffer, GetLastError(), myGetLastErrorMsg());
// ASSERT(nBuf < _countof(szBuffer));
{
CString s2;
s2.Format("%s\n(lasterror may not apply) %d: %s", s, GetLastError(), myGetLastErrorMsg());
s = s2;
}
va_end(args);
va_end(args);
myTRACE(szBuffer);
return AtlReportError(clsid, szBuffer);
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!!
nBuf = _vsnprintf(szBuffer, sizeof(szBuffer), lpszFormat, args);
CString s;
s.FormatV(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];
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;
}
::FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &MsgBuf,
sizeof(MsgBuf),
NULL
);
return MsgBuf;
return "Could not format errormessage";
}
long myRound(double const &x) {

View File

@@ -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, ...);