Sunday, November 9, 2008

Windows IP address and WMI

There exist many ways to get the IP address in Windows. But which is the most suitable way?. Some method didn't give gateway address.some didn't give MTU settings. So my search led to the WMI method. Most of the portion of this code is copied from other online sources. This example prints out the DNSHostName, DHCPEnabled status, the IP addresses, subnet masks, GATEWAY details , MTU details, MAC address and many other details. It demonstrates the basics of wmi and how to get things done using WMI....


// WMITest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define _WIN32_DCOM
#pragma warning( disable: 4267 )
#pragma warning( disable: 4331 )
#pragma warning( disable: 4311 )
#pragma warning( disable: 4996 )

#include < iostream >
#include < list >

using namespace std;
#include < comdef.h >
#include < wbemidl.h >

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

// Convert BSTR string to ascii
char* BSTRToAscii(const BSTR pwszString)
{
char* pszURL = new char[200+1];
::WideCharToMultiByte(CP_ACP, 0, pwszString, -1,
pszURL,200, NULL, NULL);
return pszURL;
}


int main(int argc, char **argv)
{
HRESULT hres;

// Initialize COM.
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{ // Program has failed.
cout << "Failed to initialize COM library. " << "Error code = 0x" << hres =" CoInitializeSecurity(" code =" 0x" plocator =" 0;" hres =" CoCreateInstance(" code =" 0x" pservice =" 0;" hres =" pLocator-">ConnectServer(
_bstr_t(L"root\\cimv2"), // WMI namespace
NULL, // User name
NULL, // User password
NULL, // Locale
0, // Security flags
NULL, // Authority
NULL, // Context object
&pService // IWbemServices proxy
);

if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x" <<>Release();
CoUninitialize();
return 1; // Program has failed.
}

cout << "Connected to ROOT\\CIMV2 WMI namespace" << hres =" CoSetProxyBlanket(" code =" 0x">Release();
pLocator->Release();
CoUninitialize();
return 1; // Program has failed.
}


BSTR strQuery = (L"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
BSTR strQL = (L"WQL");

IEnumWbemClassObject *pEnumerator = NULL;

hres = pService->ExecQuery( strQL, strQuery,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL, &pEnumerator);

if (FAILED(hres))
{
cout << "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'" <<>Release();
pLocator->Release();
CoUninitialize();
return -1;
}

ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;

int nRez = 0;
char sRezDescr[256] = {'\0'};
char sRezModel[128] = {'\0'};

while (pEnumerator)
{
VARIANT vtProp;
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pClassObject, &uReturned);

if(0 == uReturned)
{
break;
}

hr = pClassObject->Get(L"IPEnabled", 0, &vtProp, 0, 0);

if(vtProp.boolVal)
{
hr = pClassObject->Get(L"Description", 0, &vtProp, 0, 0);
if(!FAILED(hr)&& (V_VT(&vtProp) == VT_BSTR) ) {
wcout << "Description: " << hr =" pClassObject-">Get(L"DNSHostName", 0, &vtProp, 0, 0);
if(!FAILED(hr)&& (V_VT(&vtProp) == VT_BSTR) ) {
wcout << "DNS:" << hr =" pClassObject-">Get(L"DHCPEnabled", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
wcout << "DHCPEnabled:" << hr =" pClassObject-">Get(L"DHCPServer", 0, &vtProp, 0, 0);
if(!FAILED(hr)&& (V_VT(&vtProp) == VT_BSTR) ) {
wcout << "DHCPServer:" << hr =" pClassObject-">Get(L"DNSEnabledForWINSResolution", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
wcout << "DNSEnabledForWINSResolution:" << hr =" pClassObject-">Get(L"IPXEnabled", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
wcout << "IPXEnabled:" << hr =" pClassObject-">Get(L"IPXAddress", 0, &vtProp, 0, 0);
if(!FAILED(hr)&& (V_VT(&vtProp) == VT_BSTR) ) {
wcout << "IPXAddress:" << index =" -1;" hr =" pClassObject-">Get(L"IPAddress", 0, &vtProp, 0, 0);

// assign the Safearray
sa = vtProp.parray;

// Get the lower and upper bound
hr = SafeArrayGetLBound( sa, 1, &lstart );
if(FAILED(hr))
return hr;

hr = SafeArrayGetUBound( sa, 1, &lend );
if(FAILED(hr))
return hr;

// loop
hr = SafeArrayAccessData(sa,(void HUGEP**)&pbstr);
if(SUCCEEDED(hr))
{
for(index=lstart; index <= lend; index++) wcout << "IP:" << hr =" SafeArrayUnaccessData(sa);" hr =" pClassObject-">Get(L"IPSubnet", 0, &vtProp, 0, 0);
sa = vtProp.parray;

hr = SafeArrayGetLBound( sa, 1, &lstart );
if(FAILED(hr))
return hr;

hr = SafeArrayGetUBound( sa, 1, &lend );
if(FAILED(hr))
return hr;

// loop
hr = SafeArrayAccessData(sa,(void HUGEP**)&pbstr);
if(SUCCEEDED(hr))
{
for(index=lstart; index <= lend; index++) wcout << "SUBNET:" << hr =" SafeArrayUnaccessData(sa);" hr =" pClassObject-">Get(L"DefaultIPGateway", 0, &vtProp, 0, 0);
sa = vtProp.parray;

// Get the lower and upper bound
hr = SafeArrayGetLBound( sa, 1, &lstart );
if(FAILED(hr))
return hr;

hr = SafeArrayGetUBound( sa, 1, &lend );
if(FAILED(hr))
return hr;

// loop
hr = SafeArrayAccessData(sa,(void HUGEP**)&pbstr);
if(SUCCEEDED(hr))
{
for(index=lstart; index <= lend; index++) wcout << "GATEWAY:" << hr =" SafeArrayUnaccessData(sa);" hr =" pClassObject-">Get(L"MACAddress", 0, &vtProp, 0, 0);
if(!FAILED(hr)&& (V_VT(&vtProp) == VT_BSTR) ) {
wcout << "MACAddress:" << hr =" pClassObject-">Get(L"ServiceName", 0, &vtProp, 0, 0);
if(!FAILED(hr)&& (V_VT(&vtProp) == VT_BSTR) ) {
wcout << "ServiceName:" << hr =" pClassObject-">Get(L"WINSEnableLMHostsLookup", 0, &vtProp, 0, 0);
if(!FAILED(hr)) {
wcout << "WINSEnableLMHostsLookup:" << hr =" pClassObject-">Get(L"WINSPrimaryServer", 0, &vtProp, 0, 0);
if(!FAILED(hr) && (V_VT(&vtProp) == VT_BSTR) ) {
wcout << "WINSPrimaryServer:" << hr =" pClassObject-">Get(L"WINSSecondaryServer", 0, &vtProp, 0, 0);
if(!FAILED(hr) && (V_VT(&vtProp) == VT_BSTR) ) {
wcout << "WINSSecondaryServer:" <<>Release();
pService->Release();
if(pEnumerator)
pEnumerator->Release();
if(pClassObject)
pClassObject->Release();

CoUninitialize();

// Program successfully completed.
return 0;
}


No comments: