/////////////////////////////////////////////////////////////////////////////////////////
// Project: SP Error Log 1.4
//
// File: spAtlLogError.cpp
// Implementation of the SP::EAtlLogError class members.
//
// Developer(s): Sergei Pavlovsky
// sergei_vp@hotmail.com
// Copyright (c) 2005
//
// Description: EAtlLogError keeps information about ATL exception.
//
// Platforms: Win32, ATL
//
// This code may be used in compiled form in any way you desire. This file may be
// redistributed unmodified by any means PROVIDING it is not sold for profit without
// the authors written consent, and providing that this notice and the authors name
// is included. If the source code in this file is used in any commercial application
// then acknowledgement must be made to the author of this file (in whatever form
// you wish).
//
// This file is provided "as is" with no expressed or implied warranty. The author
// accepts no liability for any damage/loss of business that this product may cause.
/////////////////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "spAtlLogError.h"
#include "spStringUtils.h"
namespace SP
{
/////////////////////////////////////////////////////////////////////////////////////////
// EAtlLogError class implementation
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
// Operations: Cloning
ELogError* EAtlLogError::Clone() const throw()
{
EAtlLogError* pClone;
try
{
pClone = new EAtlLogError(*this);
_ASSERTE( pClone );
pClone->AddRef();
}
#ifdef _DEBUG
catch ( std::bad_alloc& )
{
ATLASSERT( 0 );
return NULL;
}
#endif // _DEBUG
catch ( ... )
{
ATLASSERT( 0 );
return NULL;
}
return pClone;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Operations: Error information
LPCTSTR EAtlLogError::GetSource() const throw()
{
return _T("ATL");
}
UINT EAtlLogError::GetCode() const throw()
{
return static_cast<UINT>(m_hrSrc);
}
SIZE_T EAtlLogError::GetModuleName(LPTSTR lpszBuffer, SIZE_T cchBuffer) const throw()
{
ATLASSERT( !::IsBadWritePtr(lpszBuffer, cchBuffer * sizeof(TCHAR)) );
// Load string in temporal buffer
TCHAR szBuffer[MAX_RESSTRINGLEN + 1];
const SIZE_T cchLoaded = ::LoadString(_AtlBaseModule.GetResourceInstance(),
SPLER_IDS_ERS_ATL,
szBuffer, MAX_RESSTRINGLEN + 1);
ATLASSERT( cchLoaded > 0 && cchLoaded < sizeof(szBuffer)/sizeof(TCHAR) );
// Copy string to the output buffer
return ( lpszBuffer )
? SP::StrCopy(szBuffer, lpszBuffer, cchBuffer)
: cchLoaded;
}
SIZE_T EAtlLogError::GetDescription(LPTSTR lpszBuffer, SIZE_T cchBuffer) const throw()
{
ATLASSERT( !::IsBadWritePtr(lpszBuffer, sizeof(cchBuffer * sizeof(TCHAR))) );
LCID lcID = ::GetThreadLocale();
DWORD landID;
::GetLocaleInfo(lcID, LOCALE_RETURN_NUMBER|LOCALE_ILANGUAGE,
reinterpret_cast<LPTSTR>(&landID), sizeof(DWORD));
HLOCAL hlocal = NULL;
SIZE_T cchRes = ::FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|
FORMAT_MESSAGE_ALLOCATE_BUFFER|
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, static_cast<DWORD>(m_hrSrc), landID,
reinterpret_cast<LPTSTR>(&hlocal), 0, NULL);
if ( cchRes )
{
CHeapPtr<TCHAR, CLocalAllocator> mem(reinterpret_cast<TCHAR*>(hlocal));
if ( lpszBuffer )
{
LPCTSTR lpcszMsg = reinterpret_cast<LPCTSTR>(::LocalLock(mem.m_pData));
cchRes = SP::StrCopy(lpcszMsg, lpszBuffer, cchBuffer);
::LocalUnlock(mem.m_pData);
}
return cchRes;
}
else
{
return ELogError::GetDescription(lpszBuffer, cchBuffer);
}
}
DWORD EAtlLogError::GetHelpContext() const throw()
{
return ELogError::GetHelpContext();
}
SIZE_T EAtlLogError::GetHelpFile(LPTSTR lpszBuffer, SIZE_T cchBuffer) const throw()
{
ATLASSERT( !::IsBadWritePtr(lpszBuffer, sizeof(cchBuffer * sizeof(TCHAR))) );
return ELogError::GetHelpFile(lpszBuffer, cchBuffer);
}
}; // namespace SP