初始化检测:
1、检查是否已经运行:
bool IsOnlyInstance(LPCTSTR gameTitle)
{
HANDLE handle = CreateMutex(NULL, TRUE, gameTitle);
if (GetLastError()!=ERROR_SUCCESS)
{
//Retrieves a handle to the top-level window whose
//class name and window name match the specified strings.
//This function does not search child windows. This function
//does not perform a case-sensitive search.
//The class name or a class atom created by a previous call
//to the RegisterClass or RegisterClassEx function.
HWND hWnd = FindWindow(gameTitle, NULL);
if (hWnd)
{
ShowWindow(hWnd, SW_SHOWNORMAL);
SetFocus(hWnd);
//Brings the thread that created the specified window into the foreground and activates the window.
SetForegroundWindow(hWnd);
SetActiveWindow(hWnd);
return false;
}
}
return true;
}
相关MSDN:
createMutex,
getlasterror,
findwindow,
shwowindow,
setfocus,
setforegroundwindow,
setactivewindow
2、检查磁盘空间
bool CheckStorage(const DWORDLONG diskSpaceNeeded)
{
int const drive = _getdrive();
struct _diskfree_t diskfree;
_getdiskfree(drive, &diskfree);
unsigned __int64 const neededClusters =
diskSpaceNeeded / (diskfree.sectors_per_cluster * diskfree.bytes_per_sector);
if (diskfree.avail_clusters < neededClusters)
{
KE_ERROR("CheckStorage failure: not enough disk space");
return false;
}
return true;
}
MSDN:
_getdrive,
_getdiskfree,
备注:这里得到的是可用簇,需要进行计算。
3、检查物理内存和虚拟内存
bool CheckMemory(const DWORDLONG physicalRAMNeeded, const DWORDLONG virtualRAMNeeded)
{
MEMORYSTATUSEX status;
GlobalMemoryStatusEx(&status);
if (status.ullTotalPhys < physicalRAMNeeded)
{
KE_ERROR("CheckMemory failure: not enough physical memory");
return false;
}
if (status.ullAvailVirtual < virtualRAMNeeded)
{
KE_ERROR("CheckMemory failure: not enough virtual memory");
return false;
}
char *buff = KE_NEW char[(unsigned int)virtualRAMNeeded];
if (buff)
{
delete[] buff;
}else{
KE_ERROR("CheckMemory failure: not enough continugous available memory.");
return false;
}
return true;
}
GlobalMemoryStatusEx
4、获取CPU主频
DWORD ReadCPUSpeed()
{
DWORD BufSize = sizeof(DWORD);
DWORD dwMHz = 0;
DWORD type = REG_DWORD;
HKEY hKey;
// open the key where the proc speed is hidden:
long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
0, KEY_READ, &hKey);
if(lError == ERROR_SUCCESS)
{
// query the key:
RegQueryValueEx(hKey, L"~MHz", NULL, &type, (LPBYTE) &dwMHz, &BufSize);
}
return dwMHz;
}
RegOpenKeyEx,
RegQueryValueEx