// BaseFun.cpp : implementation file
|
//
|
|
#include "stdafx.h"
|
#include "BaseFun.h"
|
|
|
|
#define REG_SUBKEY _T( "Software\\Activesoft\\MBClient" )
|
|
CString m_strBinPath;
|
// CBaseFun
|
|
CBaseFun::CBaseFun()
|
{
|
m_strBinPath = _T("");
|
}
|
|
CBaseFun::~CBaseFun()
|
{
|
}
|
|
|
// CBaseFun member functions
|
bool CBaseFun::CStringToAnsi( LPCTSTR pszT, LPSTR *ppszA )
|
{
|
ULONG cbAnsi, cCharacters;
|
|
if ( pszT == NULL )
|
{
|
*ppszA= NULL;
|
return false;
|
}
|
#ifdef _UNICODE
|
cCharacters = wcslen(pszT);
|
cbAnsi = ::WideCharToMultiByte( CP_ACP, 0, pszT, -1, NULL, 0, NULL, NULL );
|
*ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi+1);
|
if ( NULL == *ppszA )
|
return false;
|
ZeroMemory(*ppszA,cbAnsi+1);
|
// Convert to ANSI.
|
if (0 == WideCharToMultiByte(CP_ACP, 0, pszT, cCharacters, *ppszA,
|
cbAnsi, NULL, NULL))
|
{
|
CoTaskMemFree(*ppszA);
|
*ppszA = NULL;
|
return false;
|
}
|
#else
|
cCharacters = strlen(pszT);
|
*ppszA = (LPSTR) CoTaskMemAlloc(cCharacters+1);
|
if ( NULL == *ppszA )
|
return false;
|
ZeroMemory(*ppszA,cCharacters+1);
|
memcpy(*ppszA,pszT,cCharacters);
|
#endif
|
return true;
|
}
|
|
|
// дÈÕÖ¾
|
void CBaseFun::WriteLog( CString strLog )
|
{
|
//return;
|
CTime tmCurrent = CTime::GetCurrentTime();
|
CString strTime = tmCurrent.Format( _T(" %Y-%m-%d %H:%M:%S = ") );
|
CString strEnter = _T(" \r\n ");
|
CString strBuff;
|
LPSTR pDest ;
|
CFile file;
|
CString strLogFile;
|
CString strLogDir;
|
DWORD dwAttributes = DWORD(-1);
|
|
strLogDir.Format( _T("E:\\Log"));
|
dwAttributes = GetFileAttributes( strLogDir) ;
|
if(dwAttributes == DWORD(-1) || (dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 )
|
::CreateDirectory( strLogDir, NULL );
|
strLogFile.Format( _T("%s\\STKInside.log"), strLogDir );
|
if ( !file.Open( strLogFile, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate) )
|
{
|
return ;
|
}
|
file.Seek( 0, CFile::end );
|
|
strBuff.Format( _T("%s%s%s"), strTime, strLog,strEnter );
|
if ( CStringToAnsi(strBuff, &pDest))
|
{
|
file.Write( (void*)pDest, strlen(pDest));
|
CoTaskMemFree(pDest);
|
}
|
file.Close();
|
}
|
|
CString CBaseFun::GetSTKWorkDir()
|
{
|
CString strWorkDir;
|
CRegKey reg;
|
CString strSubKey;
|
TCHAR szValue[256] = { 0 };
|
DWORD dwReaded;
|
|
strSubKey.Format( _T("%s"), REG_SUBKEY );
|
if ( ERROR_SUCCESS == reg.Open( HKEY_LOCAL_MACHINE, strSubKey, KEY_READ ) )
|
{
|
dwReaded = 255;
|
if ( ERROR_SUCCESS == reg.QueryStringValue( _T( "WorkDir" ), szValue, &dwReaded ) && dwReaded > 0 )
|
{
|
strWorkDir = szValue;
|
strWorkDir.TrimRight();
|
strWorkDir.TrimRight( '\\' );
|
}
|
|
reg.Close();
|
}
|
if ( strWorkDir.IsEmpty() )
|
strWorkDir =GetModulePath();
|
return strWorkDir;
|
}
|
|
// »ñȡִÐгÌÐòËùÔÚµÄĿ¼
|
CString CBaseFun::GetModulePath()
|
{
|
CString strFileName;
|
HINSTANCE hInstance = NULL;
|
hInstance = ::GetModuleHandle( _T("mbInside.dll") );
|
if (m_strBinPath.IsEmpty( ))
|
{
|
GetModuleFileName( hInstance, strFileName.GetBuffer( MAX_PATH ), MAX_PATH );
|
strFileName.ReleaseBuffer();
|
strFileName = GetPathName( strFileName );
|
return strFileName;
|
}
|
else
|
return m_strBinPath;
|
}
|
// ȡȫ·¾¶ÎļþÃûÖеÄ·¾¶
|
CString CBaseFun::GetPathName( CString strFileName )
|
{
|
int nPos;
|
CString strPathName;
|
|
if ( strFileName.IsEmpty() )
|
return _T( "" );
|
|
nPos = strFileName.ReverseFind( '\\' );
|
if ( nPos < 0 )
|
return _T( "" );
|
|
strPathName = strFileName.Left( nPos );
|
return strPathName;
|
}
|