⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 global.cpp

📁 一个管理ie的软件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// Global.cpp
//

#include "stdafx.h"
#include "resource.h"       // main symbols


/////////////////////////////////////////////////////////////////////////////
// global functions

BOOL WriteLog(LPCTSTR pszLog)
{
	static CRWLock lockWriteLog;
	BOOL bRet = TRUE;
	HANDLE hFile;
	DWORD dwSize;

	lockWriteLog.WriteLock();

	hFile = CreateFile(g_Global.m_sLogFilePath, GENERIC_READ | GENERIC_WRITE, 
		FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		bRet = FALSE;
	}
	else
	{
		SetFilePointer(hFile, 0, NULL, FILE_END);

		WriteFile(hFile, pszLog, _tcslen(pszLog), &dwSize, NULL);

		CloseHandle(hFile);
	}

	lockWriteLog.WriteUnlock();
	return bRet;
}

BOOL WipeFile(LPCTSTR szDir, LPCTSTR szFile)
{
	CString sPath;
	HANDLE	hFile;
	DWORD	dwSize;
	DWORD	dwWrite;
	char	sZero[SWEEP_BUFFER_SIZE];
	memset(sZero, 0, SWEEP_BUFFER_SIZE);

	sPath = szDir;
	sPath += _T('\\');
	sPath += szFile;

	hFile = CreateFile(sPath, GENERIC_WRITE, 
		FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 
		FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}

	dwSize = GetFileSize(hFile, NULL);

	//skip file header (actually, I don't know the file format of index.dat)
	dwSize -= 64;
	SetFilePointer(hFile, 64, NULL, FILE_BEGIN);

	while (dwSize > 0)
	{
		if (dwSize > SWEEP_BUFFER_SIZE)
		{
			WriteFile(hFile, sZero, SWEEP_BUFFER_SIZE, &dwWrite, NULL);
			dwSize -= SWEEP_BUFFER_SIZE;
		}
		else
		{
			WriteFile(hFile, sZero, dwSize, &dwWrite, NULL);
			break;
		}
	}

	CloseHandle(hFile);
	return TRUE;
}

void GetErrorMessage(DWORD dwErrorCode, CString* psErrorMsg)
{
	LPTSTR lpBuffer = NULL;
	psErrorMsg->Empty();

	if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
			NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
			(LPTSTR)&lpBuffer, 0, NULL))
	{
		if (lpBuffer)
		{
			*psErrorMsg = lpBuffer;
			LocalFree(lpBuffer);
		}
	}
}

BOOL GetUserSid(PSID* ppSid)
{
	HANDLE hToken;
	BOOL bRes;
	DWORD cbBuffer, cbRequired;
	PTOKEN_USER pUserInfo;

	// The User's SID can be obtained from the process token
	bRes = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken);
	if (FALSE == bRes)
	{
		return FALSE;
	}

	// Set buffer size to 0 for first call to determine
	// the size of buffer we need.
	cbBuffer = 0;
	bRes = GetTokenInformation(hToken, TokenUser, NULL, cbBuffer, &cbRequired);
	if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
	{
		return FALSE;
	}

	// Allocate a buffer for our token user data
	cbBuffer = cbRequired;
	pUserInfo = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), 0, cbBuffer);
	if (NULL == pUserInfo)
	{
		return FALSE;
	}

	// Make the "real" call
	bRes = GetTokenInformation(hToken, TokenUser, pUserInfo, cbBuffer, &cbRequired);
	if (FALSE == bRes) 
	{
		return FALSE;
	}

	// Make another copy of the SID for the return value
	cbBuffer = GetLengthSid(pUserInfo->User.Sid);

	*ppSid = (PSID) HeapAlloc(GetProcessHeap(), 0, cbBuffer);
	if (NULL == *ppSid)
	{
		return FALSE;
	}

	bRes = CopySid(cbBuffer, *ppSid, pUserInfo->User.Sid);
	if (FALSE == bRes)
	{
	    HeapFree(GetProcessHeap(), 0, *ppSid);
		return FALSE;
	}

	bRes = HeapFree(GetProcessHeap(), 0, pUserInfo);

	return TRUE;
}

void GetSidString(PSID pSid, LPTSTR szBuffer)
{
	//convert SID to string
	SID_IDENTIFIER_AUTHORITY *psia = ::GetSidIdentifierAuthority( pSid );
	DWORD dwTopAuthority = psia->Value[5];
	_stprintf(szBuffer, _T("S-1-%lu"), dwTopAuthority);

	TCHAR szTemp[32];
	int iSubAuthorityCount = *(GetSidSubAuthorityCount(pSid));
	for (int i = 0; i<iSubAuthorityCount; i++) 
	{
		DWORD dwSubAuthority = *(GetSidSubAuthority(pSid, i));
		_stprintf(szTemp, _T("%lu"), dwSubAuthority);
		_tcscat(szBuffer, _T("-"));
		_tcscat(szBuffer, szTemp);
	}
}

BOOL IsWindowsNT()
{
	BOOL bRet = FALSE;
	BOOL bOsVersionInfoEx;
	OSVERSIONINFOEX osvi;

	// Try calling GetVersionEx using the OSVERSIONINFOEX structure,
	// If that fails, try using the OSVERSIONINFO structure.
	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

	if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
	{
		// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
		osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
		if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) 
			return bRet;
	}

	if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && osvi.dwMajorVersion <= 4)
	{
		bRet = TRUE;
	}

	return bRet;
}

BOOL IsWindows2k()
{
	BOOL bRet = FALSE;
	BOOL bOsVersionInfoEx;
	OSVERSIONINFOEX osvi;

	// Try calling GetVersionEx using the OSVERSIONINFOEX structure,
	// If that fails, try using the OSVERSIONINFO structure.
	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

	if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
	{
		// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
		osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
		if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) 
			return bRet;
	}

	if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && osvi.dwMajorVersion >= 5)
	{
		bRet = TRUE;
	}

	return bRet;
}
/*
BOOL IsIE5()
{
	BOOL bRet = FALSE;
	DWORD dwHandle;
	DWORD dwSize;
	UINT uLen;
	BYTE* pBuffer = NULL;
	VS_FIXEDFILEINFO* pInfo;

	dwSize = GetFileVersionInfoSize(_T("wininet.dll"), &dwHandle); 
	if (dwSize == 0)
		goto cleanup;

	pBuffer = new BYTE[dwSize]; 
	if (!GetFileVersionInfo(_T("wininet.dll"), 0, dwSize, (LPVOID)pBuffer))
		goto cleanup;

	VerQueryValue(pBuffer, _T("\\"), (LPVOID*)(&pInfo), &uLen);
	if(HIWORD(pInfo->dwProductVersionMS) < 5)
		goto cleanup;

	bRet = TRUE;
cleanup:
	if (pBuffer)
	{
		delete pBuffer;
	}
	return bRet;
} 
*/
void HexString2Char(const TCHAR* string, TCHAR* pchar)
{
	int nTemp;
	*pchar = _T('\0');

	for (int i=0; i<2; i++)
	{
		if (_istdigit(string[i]))
		{
			nTemp = string[i] - _T('0');
		}
		else
		{
			nTemp = string[i] - _T('A') + 10;
		}

		*pchar = *pchar | (nTemp << 4*(1 - i));
	}
}

void UnEscape(const TCHAR* string, tstring* pstrResult)
{
	if (string == NULL || *string == '\0')
	{
		*pstrResult = _T("");
		return;
	}

	USES_CONVERSION;
	const TCHAR* pSrc = string;
	TCHAR* pDest;
	TCHAR* pszTemp;
	TCHAR szBuffer[4];
	long lLen = _tcslen(pSrc);

	pstrResult->resize(lLen);
	pDest = &pstrResult->at(0);

	szBuffer[2] = _T('\0');
	szBuffer[3] = _T('\0');

	while (*pSrc != _T('\0'))
	{
		if (*pSrc == _T('%'))
		{
			if (*(pSrc + 1) == _T('u') && string + lLen - pSrc >= 6)
			{
				HexString2Char(pSrc + 2, &szBuffer[1]);
				HexString2Char(pSrc + 4, &szBuffer[0]);
				pszTemp = W2A((wchar_t*)szBuffer);
				strncpy(pDest, pszTemp, 2);

				pSrc += 6;
				pDest += 2;
			}
			else if (string + lLen - pSrc >= 3)
			{
				HexString2Char(pSrc + 1, pDest);
				pSrc += 3;
				pDest ++;
			}
		}
		else
		{
			*pDest = *pSrc;
			pSrc ++;
			pDest ++;
		}
	}

	lLen = pDest - &pstrResult->at(0);
	pstrResult->resize(lLen);
}

void CreateUrlFromPath(const TCHAR* string, tstring* pstrResult)
{
	*pstrResult = _T("file:///");
	*pstrResult += string;

	TCHAR* pBuffer = &pstrResult->at(0);
	for (int i=0; i<pstrResult->size(); i++)
	{
		if (*pBuffer == _T('\\'))
		{
			*pBuffer = _T('/');
		}
		pBuffer ++;
	}
}


/////////////////////////////////////////////////////////////////////////////
// CGlobal

CGlobal::CGlobal()
{
	m_nVScroolWidth = GetSystemMetrics(SM_CXVSCROLL);
	m_nBorderWidth = GetSystemMetrics(SM_CXEDGE);

	//get file path
	TCHAR szBuffer[MAX_PATH];
	long lLength;

	GetModuleFileName(NULL, szBuffer, MAX_PATH);
	m_sModuleFilePath = szBuffer;

	lLength = _tcslen(szBuffer) - 1;
	while (lLength >= 0 && szBuffer[lLength] != _T('.'))
	{
		szBuffer[lLength] = _T('\0');
		lLength--;
	}

	m_sIniFilePath = szBuffer;
	m_sIniFilePath += _T("ini");

	m_sFilterFilePath = szBuffer;
	m_sFilterFilePath += _T("flt");

	m_sLogFilePath = szBuffer;
	m_sLogFilePath += _T("log");

	lLength = _tcslen(szBuffer) - 1;
	while (lLength >= 0 && szBuffer[lLength] != _T('\\'))
	{
		szBuffer[lLength] = _T('\0');
		lLength--;
	}
	m_sWorkDir = szBuffer;
}

CGlobal::~CGlobal()
{
	ClearLog();
}

void CGlobal::ReadConfig(LPCTSTR pszFilePath)
{
	m_dwIeWindowSettings = (DWORD)GetPrivateProfileInt(
		APP_NAME, _T("IeWindowSettings"), 0, pszFilePath);
	m_dwFilterSettings = (DWORD)GetPrivateProfileInt(
		APP_NAME, _T("FilterSettings"), 0, pszFilePath);
	m_dwClearSettings = (DWORD)GetPrivateProfileInt(
		APP_NAME, _T("ClearSettings"), 0, pszFilePath);
	m_bDisable = (BOOL)GetPrivateProfileInt(
		APP_NAME, _T("Disable"), 0, pszFilePath);
	m_bAutorun = (BOOL)GetPrivateProfileInt(
		APP_NAME, _T("Autorun"), 0, pszFilePath);
	m_bEnableSound = (BOOL)GetPrivateProfileInt(
		APP_NAME, _T("EnableSound"), 0, pszFilePath);
	m_bClearAtExit = (BOOL)GetPrivateProfileInt(
		APP_NAME, _T("ClearAtExit"), 0, pszFilePath);
	m_lWindowSizeThreshold = GetPrivateProfileInt(
		APP_NAME, _T("WindowSizeThreshold"), 0, pszFilePath);
	m_nKeyDisable = GetPrivateProfileInt(
		APP_NAME, _T("KeyDisable"), 0x5A, pszFilePath);
	m_nKeyHideIE = GetPrivateProfileInt(
		APP_NAME, _T("KeyHideIE"), 0x58, pszFilePath);
	m_nKeyShowIE = GetPrivateProfileInt(
		APP_NAME, _T("KeyShowIE"), 0x58, pszFilePath);
}

void CGlobal::WriteConfig(LPCTSTR pszFilePath)
{
	CString sTemp;

	sTemp.Format(_T("%ld"), m_dwIeWindowSettings);
	WritePrivateProfileString(APP_NAME, _T("IeWindowSettings"), sTemp, pszFilePath);

	sTemp.Format(_T("%ld"), m_dwFilterSettings);
	WritePrivateProfileString(APP_NAME, _T("FilterSettings"), sTemp, pszFilePath);

	sTemp.Format(_T("%ld"), m_dwClearSettings);
	WritePrivateProfileString(APP_NAME, _T("ClearSettings"), sTemp, pszFilePath);

	sTemp.Format(_T("%ld"), m_bDisable);
	WritePrivateProfileString(APP_NAME, _T("Disable"), sTemp, pszFilePath);
	
	sTemp.Format(_T("%ld"), m_bAutorun);
	WritePrivateProfileString(APP_NAME, _T("Autorun"), sTemp, pszFilePath);

	sTemp.Format(_T("%ld"), m_bEnableSound);
	WritePrivateProfileString(APP_NAME, _T("EnableSound"), sTemp, pszFilePath);

	sTemp.Format(_T("%ld"), m_bClearAtExit);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -