わびさびサンプルソース

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

Win32_NetworkAdapterConfigurationの表示

Win32_NetworkAdapterConfigurationの内容を表示します。

#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_NetworkAdapterConfigurationの表示
*/
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_NetworkAdapterConfiguration"));
				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_NetworkAdapterConfiguration
[2]__SUPERCLASS = CIM_Setting
[3]__DYNASTY = CIM_Setting
[4]__RELPATH = Win32_NetworkAdapterConfiguration.Index=0
[5]__PROPERTY_COUNT = 61
ARRAY(VT_BSTR)[0]CIM_Setting
[6]__DERIVATION = [ CIM_Setting ]
[7]__SERVER = DESKTOP-QD55N38
[8]__NAMESPACE = root\cimv2
[9]__PATH = \\DESKTOP-666677\root\cimv2:Win32_NetworkAdapterConfiguration.Index=0
[10]ArpAlwaysSourceRoute = (VT_NULL)
[11]ArpUseEtherSNAP = (VT_NULL)
[12]Caption = [00000000] Intel(R) Ethernet Connection (2) I219-V
[13]DatabasePath = (VT_NULL)
[14]DeadGWDetectEnabled = (VT_NULL)
[15]DefaultIPGateway = (VT_NULL)
[16]DefaultTOS = (VT_NULL)
[17]DefaultTTL = (VT_NULL)
[18]Description = Intel(R) Ethernet Connection (2) I219-V
[19]DHCPEnabled = true
[20]DHCPLeaseExpires = (VT_NULL)
[21]DHCPLeaseObtained = (VT_NULL)
[22]DHCPServer = (VT_NULL)
[23]DNSDomain = (VT_NULL)
[24]DNSDomainSuffixSearchOrder = (VT_NULL)
[25]DNSEnabledForWINSResolution = (VT_NULL)
[26]DNSHostName = (VT_NULL)
[27]DNSServerSearchOrder = (VT_NULL)
[28]DomainDNSRegistrationEnabled = (VT_NULL)
[29]ForwardBufferMemory = (VT_NULL)
[30]FullDNSRegistrationEnabled = (VT_NULL)
[31]GatewayCostMetric = (VT_NULL)
[32]IGMPLevel = (VT_NULL)
[33]Index = 0
[34]InterfaceIndex = 8
[35]IPAddress = (VT_NULL)
[36]IPConnectionMetric = (VT_NULL)
[37]IPEnabled = false
[38]IPFilterSecurityEnabled = (VT_NULL)
[39]IPPortSecurityEnabled = (VT_NULL)
[40]IPSecPermitIPProtocols = (VT_NULL)
[41]IPSecPermitTCPPorts = (VT_NULL)
[42]IPSecPermitUDPPorts = (VT_NULL)
[43]IPSubnet = (VT_NULL)
[44]IPUseZeroBroadcast = (VT_NULL)
[45]IPXAddress = (VT_NULL)
[46]IPXEnabled = (VT_NULL)
[47]IPXFrameType = (VT_NULL)
[48]IPXMediaType = (VT_NULL)
[49]IPXNetworkNumber = (VT_NULL)
[50]IPXVirtualNetNumber = (VT_NULL)
[51]KeepAliveInterval = (VT_NULL)
[52]KeepAliveTime = (VT_NULL)
[53]MACAddress = 30:55:33:77:51:90
[54]MTU = (VT_NULL)
[55]NumForwardPackets = (VT_NULL)
[56]PMTUBHDetectEnabled = (VT_NULL)
[57]PMTUDiscoveryEnabled = (VT_NULL)
[58]ServiceName = e1dexpress
[59]SettingID = {E9DBD811-5555-4049-8DE3-384101021F3C}
[60]TcpipNetbiosOptions = (VT_NULL)
[61]TcpMaxConnectRetransmissions = (VT_NULL)
[62]TcpMaxDataRetransmissions = (VT_NULL)
[63]TcpNumConnections = (VT_NULL)
[64]TcpUseRFC1122UrgentPointer = (VT_NULL)
[65]TcpWindowSize = (VT_NULL)
[66]WINSEnableLMHostsLookup = (VT_NULL)
[67]WINSHostLookupFile = (VT_NULL)
[68]WINSPrimaryServer = (VT_NULL)
[69]WINSScopeID = (VT_NULL)
[70]WINSSecondaryServer = (VT_NULL)




わびさびサンプルソース

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