📄 utility.cpp
字号:
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT AtomicFreeAndCopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc)
{
BSTR bstr = 0;
bstr = reinterpret_cast<BSTR>(InterlockedExchangePointer(&pbstrDst, 0));
SysFreeString(bstr);
bstr = SysAllocString(pszSrc);
bstr = reinterpret_cast<BSTR>(InterlockedExchangePointer(&pbstrDst, bstr));
SysFreeString(bstr);
return S_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT allocateAndCopy(WCHAR ** target, const WCHAR * source)
//
// parameters:
//
// description:
// generic helper function to take a source and save a copy of it
// in target, with allocations if necessary
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT allocateAndCopy(WCHAR ** target, const WCHAR * source)
{
HRESULT hr = S_OK;
if (*target)
{
delete[] (*target);
*target = NULL;
}
if (source)
{
*target = new WCHAR[wcslen(source)+1];
CHK_BOOL (*target != NULL, E_OUTOFMEMORY);
wcscpy(*target, source);
}
Cleanup:
ASSERT(hr==S_OK);
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CAutoFormat::swprintf(const TCHAR *pchFormatString, ...)
//
// parameters:
//
// description:
// takes care of allocating a buffer
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CAutoFormat::sprintf(const TCHAR *pchFormatString, const TCHAR *pchPartOne)
{
HRESULT hr;
CHK_BOOL(pchPartOne, E_UNEXPECTED);
CHK_BOOL(pchFormatString, E_UNEXPECTED);
CHK(verifyBuffer(_tcslen(pchFormatString) + _tcslen(pchPartOne)));
hr = StringCbPrintf(m_pchBuffer, m_ulSize*sizeof(TCHAR), pchFormatString, pchPartOne);
Cleanup:
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CAutoFormat::copy(const TCHAR *pchFormatString, )
//
// parameters:
//
// description:
// takes care of allocating a buffer
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CAutoFormat::copy(const TCHAR *pchSource)
{
HRESULT hr;
CHK_BOOL(pchSource, E_UNEXPECTED);
CHK(verifyBuffer(_tcslen(pchSource)));
_tcscpy(m_pchBuffer, pchSource);
Cleanup:
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CAutoFormat::swprintf(const TCHAR *pchFormatString, )
//
// parameters:
//
// description:
// takes care of allocating a buffer
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CAutoFormat::sprintf(const TCHAR *pchFormatString, const TCHAR *pchPartOne, const TCHAR *pchPartTwo)
{
HRESULT hr;
CHK_BOOL(pchPartOne, E_UNEXPECTED);
CHK_BOOL(pchPartTwo, E_UNEXPECTED);
CHK_BOOL(pchFormatString, E_UNEXPECTED);
CHK(verifyBuffer(_tcslen(pchFormatString) +_tcslen(pchPartOne)+_tcslen(pchPartTwo)));
hr = StringCbPrintf(m_pchBuffer, m_ulSize*sizeof(TCHAR), pchFormatString, pchPartOne, pchPartTwo);
Cleanup:
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CAutoFormat::swprintf(const TCHAR *pchFormatString, )
//
// parameters:
//
// description:
// takes care of allocating a buffer
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CAutoFormat::sprintf(const TCHAR *pchFormatString, const TCHAR *pchPartOne, const TCHAR *pchPartTwo, const TCHAR *pchPartThree)
{
HRESULT hr;
CHK_BOOL(pchPartOne, E_UNEXPECTED);
CHK_BOOL(pchPartTwo, E_UNEXPECTED);
CHK_BOOL(pchPartThree, E_UNEXPECTED);
CHK_BOOL(pchFormatString, E_UNEXPECTED);
CHK(verifyBuffer(_tcslen(pchFormatString) +_tcslen(pchPartOne)+_tcslen(pchPartTwo)+_tcslen(pchPartThree)));
hr = StringCbPrintf(m_pchBuffer, m_ulSize*sizeof(TCHAR), pchFormatString, pchPartOne, pchPartTwo, pchPartThree);
Cleanup:
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CAutoFormat::swprintf(const TCHAR *pchFormatString, )
//
// parameters:
//
// description:
// takes care of allocating a buffer
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CAutoFormat::sprintf(const TCHAR *pchFormatString, const TCHAR *pchPartOne, const TCHAR *pchPartTwo, const TCHAR *pchPartThree, const TCHAR *pchPartFour)
{
HRESULT hr;
CHK_BOOL(pchPartOne, E_UNEXPECTED);
CHK_BOOL(pchPartTwo, E_UNEXPECTED);
CHK_BOOL(pchPartThree, E_UNEXPECTED);
CHK_BOOL(pchPartFour, E_UNEXPECTED);
CHK_BOOL(pchFormatString, E_UNEXPECTED);
CHK(verifyBuffer(_tcslen(pchFormatString) +_tcslen(pchPartOne)+_tcslen(pchPartTwo)+_tcslen(pchPartThree)+_tcslen(pchPartFour)));
hr = StringCbPrintf(m_pchBuffer, m_ulSize*sizeof(TCHAR), pchFormatString, pchPartOne, pchPartTwo, pchPartThree, pchPartFour);
Cleanup:
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT CAutoFormat::verifyBuffer(ULONG ulNewLen)
//
// parameters:
//
// description:
// takes care of allocating a buffer
// returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CAutoFormat::verifyBuffer(ULONG ulNewLen)
{
HRESULT hr;
CHK_BOOL(ulNewLen > m_ulSize, S_OK);
delete [] m_pchBuffer;
m_ulSize = 0;
m_pchBuffer = new TCHAR[ulNewLen+1];
CHK_BOOL(m_pchBuffer, E_OUTOFMEMORY);
m_ulSize = ulNewLen;
CHK(S_OK);
Cleanup:
return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: LONG AtomicIncrement(LPLONG lpAddend)
//
// parameters:
//
// description:
//
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined _M_IX86 && !defined UNDER_CE
__declspec(naked)
LONG __stdcall AtomicIncrement(LPLONG lpAddend)
{
__asm
{
mov ecx,dword ptr [esp+04h]
mov eax, 1
lock xadd dword ptr [ecx],eax
inc eax
ret 04h
}
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: LONG AtomicDecrement(LPLONG lpAddend)
//
// parameters:
//
// description:
//
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined _M_IX86 && !defined UNDER_CE
__declspec(naked)
LONG __stdcall AtomicDecrement(LPLONG lpAddend)
{
__asm
{
mov ecx,dword ptr [esp+04h]
mov eax,0FFFFFFFFh
lock xadd dword ptr [ecx],eax
dec eax
ret 04h
}
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: HRESULT InitSoaputil(void)
//
// parameters:
//
// description:
// Initializes Global Interface Table
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT InitSoaputil(void)
{
HRESULT hr;
#ifndef UNDER_CE
CHK(CoCreateInstance(CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&g_pGIT));
Cleanup:
#else
hr = S_OK;
#endif
return hr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -