/////////////////////////////////////////////////////////////////////////////////////////
// Project: SP Error Log 1.4
//
// File: spWin32LogError.cpp
// Implementation of the SP::EWin32LogError class members.
//
// Developer(s): Sergei Pavlovsky
// sergei_vp@hotmail.com
// Copyright (c) 2005
//
// Description: EWin32LogError keeps information about Win32 error.
//
// 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 "spWin32LogError.h"
#include "spStringUtils.h"
namespace SP
{
/////////////////////////////////////////////////////////////////////////////////////////
// EWin32LogError class
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
// Operations: Cloning
ELogError* EWin32LogError::Clone() const throw()
{
EWin32LogError* pClone;
try
{
pClone = new EWin32LogError(*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 EWin32LogError::GetSource() const throw()
{
return _T("Win32");
}
UINT EWin32LogError::GetCode() const throw()
{
return m_dwCode;
}
SIZE_T EWin32LogError::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_WIN32,
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 EWin32LogError::GetDescription(LPTSTR lpszBuffer, SIZE_T cchBuffer) const throw()
{
ATLASSERT( !::IsBadWritePtr(lpszBuffer, cchBuffer * sizeof(TCHAR)) );
const 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, m_dwCode, landID,
reinterpret_cast<LPTSTR>(&hlocal), 0, NULL);
ATLASSERT( cchRes );
if ( !cchRes )
{
HMODULE hNetMsgDll = ::LoadLibraryEx(_T("netmsg.dll"), NULL,
DONT_RESOLVE_DLL_REFERENCES);
if ( !hNetMsgDll )
return 0;
cchRes = ::FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|
FORMAT_MESSAGE_ALLOCATE_BUFFER|
FORMAT_MESSAGE_FROM_HMODULE,
hNetMsgDll, m_dwCode, landID,
reinterpret_cast<LPTSTR>(&hlocal), 0, NULL);
::FreeLibrary(hNetMsgDll);
if ( !hNetMsgDll )
return 0;
// MQUTIL.DLL
}
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;
}
DWORD EWin32LogError::GetHelpContext() const throw()
{
return ELogError::GetHelpContext();
}
SIZE_T EWin32LogError::GetHelpFile(LPTSTR lpszBuffer, SIZE_T cchBuffer) const throw()
{
return ELogError::GetHelpFile(lpszBuffer, cchBuffer);
}
}; // namespace SP