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

📄 stutil.cpp

📁 WinCE开发技巧与实例的配套源码(chapter2)
💻 CPP
字号:
#include "stdafx.h"
#include "STUtil.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


DWORD CSTUtil::m_wClockSequenceBase = 0;
WORD CSTUtil::m_wDeviceId1 = 0;
WORD CSTUtil::m_wDeviceId2 = 0;
WORD CSTUtil::m_wDeviceId3 = 0;

//////////////////////////////////////////////////////////////////////
// CSTUtil
//////////////////////////////////////////////////////////////////////

//This method uses algorithm described in
//http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
HRESULT CSTUtil::CoCreateGuid(GUID *pguid)
{
	ASSERT(pguid);

	SYSTEMTIME systemTime;
	FILETIME fileTime;

	GetSystemTime(&systemTime);

	BOOL bResult = SystemTimeToFileTime(&systemTime, &fileTime);
	if (!bResult) {
		return E_FAIL;
	}

	//0-3    The low field of the timestamp.
	pguid->Data1 = fileTime.dwLowDateTime; 

	//4-5    The middle field of the timestamp.
	pguid->Data2 = LOWORD(fileTime.dwHighDateTime);

	//6-7    The high field of the timestamp multiplexed 
	//       with the version number.
	//       Version number is 0x0002
	pguid->Data3 = (HIWORD(fileTime.dwHighDateTime) & 0xFFF0) | 0x0002;

	//8-9    Here we store sequence number
	if (m_wClockSequenceBase==0) {
		m_wClockSequenceBase = Random();
	}
	m_wClockSequenceBase++;

	*((WORD*)pguid->Data4) = LOWORD(m_wClockSequenceBase);

	//10-15  The spatially unique node identifier.
	//       Because there is no network card we generate random number
	//       instead of unique network card id.
	if (m_wDeviceId1==0) {
		m_wDeviceId1 = LOWORD(GetTickCount()*Random());
		m_wDeviceId2 = LOWORD(Random());
		m_wDeviceId3 = LOWORD(Random());
	}
	*((WORD*)pguid->Data4+2) = m_wDeviceId1;
	*((WORD*)pguid->Data4+4) = m_wDeviceId2;
	*((WORD*)pguid->Data4+6) = m_wDeviceId3;
    
	return S_OK;
}

BOOL CSTUtil::RunExecutable(CString strExecutableFilePath, CString strArguments)
{
	PROCESS_INFORMATION pi;
	if (!::CreateProcess(strExecutableFilePath, 
	        strArguments, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi)) 
	{	
		return FALSE;
	}

	return TRUE;
}

BOOL CSTUtil::RunExecutableAndWait(CString strExecutableFilePath, CString strArguments)
{
	PROCESS_INFORMATION pi;
	if (!::CreateProcess(strExecutableFilePath, 
	        strArguments, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi)) 
	{	
		return FALSE;
	}

	// Waint until the executable will be finished
	::WaitForSingleObject(pi.hProcess, INFINITE);

	return TRUE;
}

CString CSTUtil::GetProgramFilePath()
{
	CString strFileName;
	TCHAR lpFileName[MAX_PATH+1];
	GetModuleFileName(NULL, lpFileName, MAX_PATH);
	strFileName = lpFileName;
	return strFileName;
}

CString CSTUtil::GetProgramFolder()
{
	CString strFilePath = GetProgramFilePath();
	int nLastIndex = strFilePath.ReverseFind('\\');
	if (nLastIndex!=-1) {
		return strFilePath.Left(nLastIndex);
	} else {
		return _T("\\");
	}
}


CSTUtil::CSTUtil()
	: m_hCoreDll(NULL)
{
	m_hCoreDll = LoadLibrary(_T("coredll.dll"));
	if (m_hCoreDll) {
		m_procUndergisterFunc = (UnregisterFunc1Proc)GetProcAddress(m_hCoreDll, _T("UnregisterFunc1"));
	}
}

CSTUtil::~CSTUtil()
{
	if (m_hCoreDll) {
		::FreeLibrary(m_hCoreDll);
	}
}

BOOL CSTUtil::RegisterHotKey(HWND hWnd, int id, UINT vk)
{
	ASSERT(m_procUndergisterFunc);
	BOOL bResult1 = m_procUndergisterFunc(MOD_WIN, vk);
	BOOL bResult2 = ::RegisterHotKey(hWnd, id, MOD_WIN, vk);
	return bResult2 && bResult1;
}



⌨️ 快捷键说明

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