I need to post some debugging information into a log using MFC's CString, but I can't seem to find if it preserves error code set by the last WinAPI (and retrievable with GetLastError)?
EDIT: Here's a code example of a simplified version of what I'm currently doing in my existing project:
HANDLE hFile = CreateFile(strFilePath, ...);
if(hFile == INVALID_HANDLE_VALUE)
{
logError(collectDebuggerInfo(strFilePath));
}
void logError(LPCTSTR pStrDesc)
{
int nLastError = ::GetLastError();
CString str;
str.Format(L"LastError=%d, Description: %s", nLastError, pStrDesc);
//Add 'str' to the logging file...
}
CString collectDebuggerInfo(LPCTSTR pFilePath)
{
int nLastError = ::GetLastError();
CString str;
str.Format(L"Debugging info for file: "%s"", pFilePath);
::SetLastError(nLastError);
return str; //RETURNING CString -- will it overwrite the last error?
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…