📄 utility.cpp
字号:
// Get needed system and custom include files
#include "Utility.h"
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <strstream>
// Generic error reporting function
// Display error text and HRESULT
void ReportError(const std::string& strErrorText, HRESULT hResult)
{
#ifdef NO_CONSOLE
std::strstream strbuff;
strbuff << "Error: \"" << strErrorText << "\"\n";
strbuff << "HRESULT: 0x" << std::hex << (ULONG) hResult << std::dec << std::ends;
MessageBox(NULL, strbuff.str(), "Error", MB_OK | MB_ICONWARNING);
#else
std::cout << "Error: \"" << strErrorText << "\"\n";
std::cout << "HRESULT: 0x" << std::hex << (ULONG) hResult << std::dec << std::endl;
#endif
}
// Convert a ULONG to a string or CString object
UTILSTR ULongToStr(const ULONG ulNumber)
{
CHAR chBuffer[33];
_ultoa(ulNumber, chBuffer, 10);
return UTILSTR(chBuffer);
}
//
// The following two methods were written by Microsoft technical support
// staff. A detailed writeup of these functions can be found in the
// Microsoft OLE Knowledge Base, article Q138813.
//
//
/*
* AnsiToUnicode converts the ANSI string pszA to a Unicode string
* and returns the Unicode string through ppszW. Space for the
* the converted string is allocated by AnsiToUnicode.
*/
HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)
{
ULONG cCharacters;
DWORD dwError;
// If input is null then just return the same.
if (NULL == pszA)
{
*ppszW = NULL;
return NOERROR;
}
// Determine number of wide characters to be allocated for the
// Unicode string.
cCharacters = strlen(pszA)+1;
// Use of the OLE allocator is required if the resultant Unicode
// string will be passed to another COM component and if that
// component will free it. Otherwise you can use your own allocator.
*ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);
if (NULL == *ppszW)
return E_OUTOFMEMORY;
// Covert to Unicode.
if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,
*ppszW, cCharacters))
{
dwError = GetLastError();
CoTaskMemFree(*ppszW);
*ppszW = NULL;
return HRESULT_FROM_WIN32(dwError);
}
return NOERROR;
}
/*
* UnicodeToAnsi converts the Unicode string pszW to an ANSI string
* and returns the ANSI string through ppszA. Space for the
* the converted string is allocated by UnicodeToAnsi.
*/
HRESULT __fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA)
{
ULONG cbAnsi, cCharacters;
DWORD dwError;
// If input is null then just return the same.
if (pszW == NULL)
{
*ppszA = NULL;
return NOERROR;
}
cCharacters = wcslen(pszW)+1;
// Determine number of bytes to be allocated for ANSI string. An
// ANSI string can have at most 2 bytes per character (for Double
// Byte Character Strings.)
cbAnsi = cCharacters*2;
// Use of the OLE allocator is not required because the resultant
// ANSI string will never be passed to another COM component. You
// can use your own allocator.
*ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
if (NULL == *ppszA)
return E_OUTOFMEMORY;
// Convert to ANSI.
if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,
cbAnsi, NULL, NULL))
{
dwError = GetLastError();
CoTaskMemFree(*ppszA);
*ppszA = NULL;
return HRESULT_FROM_WIN32(dwError);
}
return NOERROR;
}
// Function to seed the random number generator based on the system time
void SeedRandomGenerator()
{
struct _timeb timebuffer;
_ftime(&timebuffer);
srand(timebuffer.millitm);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -