我需要使用 MFC 的CString将一些调试信息发布到日志中,但我似乎无法找到它是否保留了最后一个 WinAPI 设置的错误代码(并且可以使用GetLastError检索)?
编辑:这是我目前在现有项目中所做的简化版本的代码示例:
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?
}