#include "StdAfx.h"
#include "PSLUtilities.h"
#include "ProSysModule.h"
tstring CPSLUtilities::s_SystemFolder;
ABIG CPSLUtilities::GetVariantBig(LPVARIANT pVar, bool * pError)
{
try
{
if(pError)
*pError = true;
ABIG value = _variant_t(pVar);
if(pError)
*pError = false;
return value;
}
catch(...)
{
}
return 0;
}
void CPSLUtilities::SetVariantBig(LPVARIANT pVar, ABIG Value)
{
::VariantInit(pVar);
#ifdef _WIN64
pVar->vt = VT_UI8;
pVar->ullVal = Value;
#else
pVar->vt = VT_UI4;
pVar->ulVal = Value;
#endif
}
void CPSLUtilities::SetVariant64Bit(LPVARIANT pVar, BIG64 Value)
{
::VariantInit(pVar);
#ifdef _WIN64
pVar->vt = VT_UI8;
pVar->ullVal = Value;
#else
if(_Module.IsWin2000()) // Windows 2000 doesn't support VT_UI8;
{
pVar->vt = VT_UI4;
pVar->ulVal = (ULONG)Value;
}
else
{
pVar->vt = VT_UI8;
pVar->ullVal = Value;
}
#endif
}
void CPSLUtilities::GetLongFilePathDetails(LPCTSTR sPath, _bstr_t & filePath, _bstr_t & fileDir, _bstr_t & fileName)
{
DWORD dwSize = ::GetLongPathName(sPath, NULL, 0);
if(!dwSize)
return;
tstring sBuffer = sPath;
sBuffer.resize(dwSize);
dwSize = ::GetLongPathName(sPath, (LPTSTR)sBuffer.c_str(), dwSize);
if(dwSize > 0)
{
tstring tmp;
tmp.resize(dwSize);
size_t idx = sBuffer.rfind('\\');
if(idx > 0)
{
LPTSTR pSource = (LPTSTR)sBuffer.c_str();
LPTSTR pDest = (LPTSTR)tmp.c_str();
::_tcsncpy_s(pDest, dwSize + 1, pSource, dwSize);
filePath = pDest;
::_tcsncpy_s(pDest, dwSize + 1, pSource, idx);
fileDir = pDest;
::_tcsncpy_s(pDest, dwSize + 1, pSource + idx + 1, dwSize - idx);
fileName = pDest;
}
}
}
void CPSLUtilities::FixSysPath(tstring & path)
{
if(!s_SystemFolder.size())
{
s_SystemFolder.resize(64);
::GetEnvironmentVariable(_T("SYSTEMROOT"), (LPTSTR)s_SystemFolder.c_str(), 64);
shrink(s_SystemFolder);
if(s_SystemFolder.length() < 1)
return;
}
tstring s = path;
size_t srcLength = s.size();
make_upper(s);
tstring sys = _T("\\SYSTEMROOT");
size_t L = sys.size();
if(!s.find(sys.c_str(), 0, L))
{
tstring tmp = s_SystemFolder;
tmp.append(path.c_str(), L, srcLength - L);
path = tmp.c_str();
return;
}
sys = _T("%SYSTEMROOT%");
L = sys.size();
if(!s.find(sys.c_str(), 0, L))
{
tstring tmp = s_SystemFolder;
tmp.append(path.c_str(), L, srcLength - L);
path = tmp.c_str();
return;
}
sys = _T("\\??\\");
L = sys.size();
if(!s.find(sys.c_str(), 0, L))
{
tstring tmp;
tmp.append(path.c_str(), L, srcLength - L);
path = tmp.c_str();
return;
}
sys = _T("SYSWOW64");
bool bAppend = (s.find(sys.c_str(), 0, sys.size()) == 0);
if(!bAppend)
{
sys = _T("SYSTEM32");
bAppend = (s.find(sys.c_str(), 0, sys.size()) == 0);
}
if(bAppend)
{
tstring tmp = s_SystemFolder;
tmp.append(_T("\\"));
tmp.append(path.c_str());
path = tmp;
}
}