?? stutil.cpp
字號:
/////////////////////////////////////////////////////////////////////////////
// LICENSE
//
// Free for uncommercial use. Cannot be used in commercial projects.
// Contact author to get commercial version.
//
// THIS LIBRARY IS DISTRIBUTED "AS IS". NO WARRANTY OF ANY
// KIND IS EXPRESSED OR IMPLIED. YOU USE AT YOUR OWN RISK.
// THE AUTHOR WILL NOT BE LIABLE FOR DATA LOSS, DAMAGES, LOSS
// OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING OR MISUSING
// THIS SOFTWARE.
#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;
}
//////////////////////////////////////////////////////////////////////
// CSTComError
//////////////////////////////////////////////////////////////////////
CSTComError::CSTComError()
: m_hrError(S_OK)
{
}
CSTComError::CSTComError(HRESULT hrError)
: m_hrError(hrError)
{
}
CSTComError::CSTComError(const CSTComError &src)
: m_hrError(src.m_hrError)
{
}
CSTComError& CSTComError::operator=(const CSTComError &src)
{
m_hrError = src.m_hrError;
return *this;
}
HRESULT CSTComError::Error() const
{
return m_hrError;
}
CSTComError lastComError;
CSTComError GetComError()
{
return lastComError;
}
//////////////////////////////////////////////////////////////////////
// COM error handlers
//////////////////////////////////////////////////////////////////////
void __stdcall _com_issue_error(HRESULT hResult)
{
#ifndef ST_IGNORE_COM_EXCEPTIONS
DWORD *data = new DWORD[1];
data[0] = hResult;
RaiseException(EXCEPTION_COMERROR, 0, 1, data);
#endif /*ST_IGNORE_COM_EXCEPTIONS*/
}
void __stdcall _com_issue_errorex(HRESULT hResult, IUnknown* pUnknown, REFIID refid)
{
#ifndef ST_IGNORE_COM_EXCEPTIONS
DWORD *data = new DWORD[1];
data[0] = hResult;
RaiseException(EXCEPTION_COMERROR, 0, 1, data);
#endif /*ST_IGNORE_COM_EXCEPTIONS*/
}
int ComErrorFilterFunction(DWORD dwCode, LPEXCEPTION_POINTERS info)
{
HRESULT hrResult = info->ExceptionRecord->ExceptionInformation[0];
lastComError = CSTComError(hrResult);
return (dwCode==EXCEPTION_COMERROR);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -