パソコンに積まれたメモリ容量を取得します。いくつか方法があるがここではIShellDispatch2を利用した。
このCOMインターフェースを利用するとCPUクロックも取得できるはずだが、Core Duo環境では失敗したのでクロック取得には使わないほうがいいだろう。それでもクロックを取得したい場合はL"PhysicalMemoryInstalled"の部分をL"ProcessorSpeed"にすればいい。
依存環境:ATL#include "atlstr.h"
#include "shldisp.h"
//
// PCに載っている物理メモリ量を取得する
//
//単位はbytes
//対応はWindows Me/2000以降
//
//失敗時はゼロを返す
//
ULONGLONG GetPhysicalMemoryInstalled(void)
{
IShellDispatch2* pIShellDispatch2;
HRESULT hr;
VARIANT val;
BSTR bstr;
pIShellDispatch2 = NULL;
hr = ::CoCreateInstance(CLSID_Shell,NULL,CLSCTX_INPROC_SERVER,IID_IShellDispatch2,(void**)&pIShellDispatch2);
if(FAILED(hr) || pIShellDispatch2 == NULL)
return 0;
::VariantClear(&val);
bstr = ::SysAllocString(L"PhysicalMemoryInstalled");
hr = pIShellDispatch2->GetSystemInformation(bstr,&val);
::SysFreeString(bstr);
pIShellDispatch2->Release();
if(FAILED(hr) || val.vt != VT_R8)
return 0;
return V_R8(&val);
}
void Test(void)
{
ULONGLONG nMemory;
CAtlString strMessage;
::CoInitialize(NULL);
nMemory = GetPhysicalMemoryInstalled();
strMessage.Format(_T("%ld MB"),nMemory / (1024*1024));
::MessageBox(NULL,strMessage,_T(""),MB_OK);
::CoUninitialize();
}
