Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / Visual Basic 6

Professional System Library: Introduction

Rate me:
Please Sign up or sign in to vote.
4.84/5 (93 votes)
22 Nov 2010CPOL14 min read 191.2K   3.4K   232  
A simplified and unified way for accessing most frequently used information about Process, System, and Environment.
#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;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sibedge IT
Ireland Ireland
My online CV: cv.vitalytomilov.com

Comments and Discussions