わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

Win32_OperatingSystemの表示

Win32_OperatingSystemの内容を表示します。

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <rpc.h>
#include <comutil.h>
#include <atlbase.h>
#include <Wbemcli.h>
#include <wbemidl.h>



#pragma comment(lib,"comsuppw.lib")
#pragma comment(lib, "wbemuuid.lib")



/*
	valiant値の取得
*/
std::wstring GetValiantValue
(
	VARIANT& var
)
{
	HRESULT hResult;
	TCHAR waBuf[ 1024 ];
	switch( var.vt ) {
	case VT_NULL:
		{
			::_swprintf_p( waBuf, _countof( waBuf ), L"(VT_NULL)" );
		}
		break;

	case VT_I2:
		{
			::_swprintf_p( waBuf, _countof( waBuf ), L"%d", var.lVal );
		}
		break;

	case VT_I4:
		{
			::_swprintf_p( waBuf, _countof( waBuf ), L"%d", var.lVal );
		}
		break;

	case VT_BSTR:
		{
			::_swprintf_p( waBuf, _countof( waBuf ), L"%s", var.bstrVal );
		}
		break;

	case VT_BOOL:
		{
			::_swprintf_p( waBuf, _countof( waBuf ), L"%s", ( FALSE == var.boolVal )? L"false" : L"true" );
		}
		break;

	case VT_UI1:
		{
			::_swprintf_p( waBuf, _countof( waBuf ), L"%d", var.uiVal );
		}
		break;

	case VT_BSTR | VT_ARRAY:
		{
			SAFEARRAY* pNames = var.parray;
			LPWSTR* p;
			LONG lIndexMin;
			LONG lIndexMax;

			hResult = SafeArrayGetLBound( pNames, 1, &lIndexMin );
			if ( hResult != hResult ) {
				std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
			}

			hResult = SafeArrayGetUBound( pNames, 1, &lIndexMax );
			if ( hResult != hResult ) {
				std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
			}

			hResult = SafeArrayAccessData( pNames, (void **)&p );
			if ( hResult != hResult ) {
				std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
			}

			std::wstring strRet;

			if ( lIndexMin <= lIndexMax ) {
				strRet = L"[ ";

				for ( int nI = lIndexMin; nI <= lIndexMax; nI++ ) {
					std::wcout << L"ARRAY(VT_BSTR)[" << nI << "]"<< p[ nI ] << std::endl;
					strRet += p[ nI ];
					if ( nI > lIndexMax ) {
						strRet += L", ";
					}
				}
				strRet += L" ]";
			}

			SafeArrayUnaccessData( pNames );
			waBuf[0]=L'¥0';
			return( strRet );
		}
		break;

	case VT_I4 | VT_ARRAY:
		{
			SAFEARRAY* pNames = var.parray;
			int* p;
			LONG lIndexMin;
			LONG lIndexMax;

			hResult = SafeArrayGetLBound( pNames, 1, &lIndexMin );
			if ( hResult != hResult ) {
				std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
			}

			hResult = SafeArrayGetUBound( pNames, 1, &lIndexMax );
			if ( hResult != hResult ) {
				std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
			}

			hResult = SafeArrayAccessData( pNames, (void **)&p );
			if ( hResult != hResult ) {
				std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
			}

			std::wstring strRet;

			if ( lIndexMin <= lIndexMax ) {
				strRet = L"[ ";

				for ( int nI = lIndexMin; nI <= lIndexMax; nI++ ) {
					std::wcout << L"ARRAY(VT_BSTR)[" << nI << "]"<< p[ nI ] << std::endl;
					::_swprintf_p( waBuf, _countof( waBuf ), L"%d", p[ nI ] );
					strRet += waBuf;
					if ( nI > lIndexMax ) {
						strRet += L", ";
					}
				}
				strRet += L" ]";
			}

			SafeArrayUnaccessData( pNames );
			waBuf[0]=L'¥0';
			return( strRet );
		}
		break;

	default:
		{
			::_swprintf_p( waBuf, _countof( waBuf ), L"<はてな?>0x%08x", var.vt );
		}
		break;
	}
	return( waBuf );
}



/*
	Win32_OperatingSystemの表示
*/
int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	/*
		std::wcoutのロケールを設定
		 これを設定するだけで、std::wcoutで日本語が表示される
		 ようになります。
	*/
	std::wcout.imbue( std::locale( "", std::locale::ctype ) );

	// COM初期化
	CoInitializeEx( NULL, COINIT_MULTITHREADED );
	{
		HRESULT hResult;

		// COM-Security初期化
		hResult = CoInitializeSecurity(
				  NULL
				, -1
				, NULL
				, NULL
				, RPC_C_AUTHN_LEVEL_PKT
				, RPC_C_IMP_LEVEL_IMPERSONATE
				, NULL
				, EOAC_NONE
				, 0
			);

		CComPtr<IWbemLocator> pIWbemLocator;
		CComPtr<IWbemServices> pIWbemServices;
		CComPtr<IEnumWbemClassObject> pIEnumObject;

		// CLSID_WbemAdministrativeLocator
		hResult = pIWbemLocator.CoCreateInstance(
				  CLSID_WbemAdministrativeLocator
				, NULL
				, CLSCTX_INPROC_SERVER
				| CLSCTX_LOCAL_SERVER
			);
		if ( S_OK == hResult ) {

			CComBSTR bstrNamespace( _T("root¥¥cimv2") );

			// ConnectServer
			hResult = pIWbemLocator->ConnectServer(
					  bstrNamespace
					, NULL
					, NULL
					, NULL
					, 0
					, NULL
					, NULL
					, &pIWbemServices
				);
			if ( S_OK == hResult ) {

				CComBSTR bstrQuery(_T("Select * from Win32_OperatingSystem"));
				CComBSTR bstrQL(_T("WQL"));

				// ExecQuery
				hResult = pIWbemServices->ExecQuery(
						  bstrQL
						, bstrQuery
						, WBEM_FLAG_RETURN_IMMEDIATELY
						, NULL
						, &pIEnumObject
					);
				if ( S_OK == hResult ) {

					ULONG uCount = 1;
					ULONG uReturned; 

					hResult = pIEnumObject->Reset(); 
					do {
						CComPtr<IWbemClassObject> pClassObject = NULL;
						hResult = pIEnumObject->Next( WBEM_INFINITE, uCount, &pClassObject, &uReturned );
						if ( S_OK != hResult ) {
							break;
						}

						SAFEARRAY* pNames;
						LPWSTR* p;
						LONG lIndexMin;
						LONG lIndexMax;

						hResult = pClassObject->GetNames( NULL, WBEM_FLAG_ALWAYS, NULL, &pNames );
						if ( S_OK != hResult ) {
							std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
						}

						hResult = SafeArrayGetLBound( pNames, 1, &lIndexMin );
						if ( S_OK != hResult ) {
							std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
						}

						hResult = SafeArrayGetUBound( pNames, 1, &lIndexMax );
						if ( S_OK != hResult ) {
							std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
						}

						hResult = SafeArrayAccessData( pNames, (void **)&p );
						if ( S_OK != hResult ) {
							std::wcout << L"hResult = " << std::hex << hResult << std::dec << std::endl;
						}

						for ( int nI = lIndexMin; nI <= lIndexMax; nI++ ) {
							CComVariant var;
							pClassObject->Get( p[ nI ], 0, &var, NULL, NULL );
							std::wstring str = GetValiantValue( var );
							std::wcout << L"[" << nI << "]"<< p[ nI ] << L" = " << str.c_str() << std::endl;

						}
						SafeArrayUnaccessData( pNames );
						SafeArrayDestroy( pNames );

					} while( S_OK == hResult );
				}
			}
		}
	}

	// COM終了処理
	::CoUninitialize();

	// 正常終了
	return( 0 );
}

実行結果

[0]__GENUS = 2
[1]__CLASS = Win32_OperatingSystem
[2]__SUPERCLASS = CIM_OperatingSystem
[3]__DYNASTY = CIM_ManagedSystemElement
[4]__RELPATH = Win32_OperatingSystem=@
[5]__PROPERTY_COUNT = 64
ARRAY(VT_BSTR)[0]CIM_OperatingSystem
ARRAY(VT_BSTR)[1]CIM_LogicalElement
ARRAY(VT_BSTR)[2]CIM_ManagedSystemElement
[6]__DERIVATION = [ CIM_OperatingSystemCIM_LogicalElementCIM_ManagedSystemElement ]
[7]__SERVER = DESKTOP-AAAAAAA
[8]__NAMESPACE = root\cimv2
[9]__PATH = \\DESKTOP-AAAAAAA\root\cimv2:Win32_OperatingSystem=@
[10]BootDevice = \Device\HarddiskVolume1
[11]BuildNumber = 10586
[12]BuildType = Multiprocessor Free
[13]Caption = Microsoft Windows 10 Pro
[14]CodeSet = 932
[15]CountryCode = 81
[16]CreationClassName = Win32_OperatingSystem
[17]CSCreationClassName = Win32_ComputerSystem
[18]CSDVersion = (VT_NULL)
[19]CSName = DESKTOP-AAAAAAA
[20]CurrentTimeZone = 540
[21]DataExecutionPrevention_32BitApplications = true
[22]DataExecutionPrevention_Available = true
[23]DataExecutionPrevention_Drivers = true
[24]DataExecutionPrevention_SupportPolicy = 2
[25]Debug = false
[26]Description =
[27]Distributed = false
[28]EncryptionLevel = 256
[29]ForegroundApplicationBoost = 2
[30]FreePhysicalMemory = 3135120
[31]FreeSpaceInPagingFiles = 1256268
[32]FreeVirtualMemory = 3429628
[33]InstallDate = 20151130162025.000000+540
[34]LargeSystemCache = (VT_NULL)
[35]LastBootUpTime = 20160404225300.493451+540
[36]LocalDateTime = 20160415135216.466000+540
[37]Locale = 0411
[38]Manufacturer = Microsoft Corporation
[39]MaxNumberOfProcesses = -1
[40]MaxProcessMemorySize = 137438953344
ARRAY(VT_BSTR)[0]ja-JP
[41]MUILanguages = [ ja-JP ]
[42]Name = Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition3
[43]NumberOfLicensedUsers = 0
[44]NumberOfProcesses = 116
[45]NumberOfUsers = 2
[46]OperatingSystemSKU = 48
[47]Organization =
[48]OSArchitecture = 64 ビット
[49]OSLanguage = 1041
[50]OSProductSuite = 256
[51]OSType = 18
[52]OtherTypeDescription = (VT_NULL)
[53]PAEEnabled = (VT_NULL)
[54]PlusProductID = (VT_NULL)
[55]PlusVersionNumber = (VT_NULL)
[56]PortableOperatingSystem = false
[57]Primary = true
[58]ProductType = 1
[59]RegisteredUser = AAAAAAA
[60]SerialNumber = AAAAA-BBBBB-CCCCC-DDDDD
[61]ServicePackMajorVersion = 0
[62]ServicePackMinorVersion = 0
[63]SizeStoredInPagingFiles = 1310720
[64]Status = OK
[65]SuiteMask = 272
[66]SystemDevice = \Device\HarddiskVolume3
[67]SystemDirectory = C:\WINDOWS\system32
[68]SystemDrive = C:
[69]TotalSwapSpaceSize = (VT_NULL)
[70]TotalVirtualMemorySize = 9576568
[71]TotalVisibleMemorySize = 8265848
[72]Version = 10.0.10586
[73]WindowsDirectory = C:\WINDOWS






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。