Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WMI Sample Get IP address

0.00/5 (No votes)
11 Jun 2009 1  
This application is for programmer who want to get IP address of there system and this is also sample application how to use WMI in vc++.

Introduction

This application is for programmer who want to retrieve IP address of there system. This is also sample application how to use WMI classes. WMI is very important for retrieving System Information. We need to execute quires and we will get System Information as a result.

Background

If we want to get System Information using WMI then we need to have Windows Management Instrumentation Service running in our System. This service is available with windows OS after XP. But if you want to get infomation in 98 then you need to install it.

Using the code

I have tried to retrive IP and Mac address but I was unable to retreive Mac address but I have retreived IP using WMI.

//
// Any source code blocks look like this
//
// TODO: Add your control notification handler code here
 CoInitialize(NULL);
 //Security needs to be initialized in XP first and this was the major problem 
 //why it was not working in XP.
 if(CoInitializeSecurity( NULL,
 -1,
 NULL,
 NULL,
 RPC_C_AUTHN_LEVEL_PKT,
 RPC_C_IMP_LEVEL_IMPERSONATE,
 NULL,
 EOAC_NONE,
 0
 ) != S_OK)
  return 0;
 IWbemLocator * pLoc = NULL; // IWbemLocator * pIWbemLocator = NULL;
 IWbemServices * pSvc = NULL; // IWbemServices * pWbemServices = NULL;
 
 IEnumWbemClassObject * pEnumerator = NULL;// IEnumWbemClassObject * pEnumObject  = NULL;
 // new
 
 
 
 BSTR bstrNamespace = (L"root\\cimv2");
 if(CoCreateInstance (
            CLSID_WbemAdministrativeLocator,
            NULL ,
            CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER , 
            IID_IUnknown ,
            ( void ** ) & pLoc
            ) != S_OK)
    return 0;
 if(pLoc->ConnectServer(
                bstrNamespace,  // Namespace
                NULL,          // Userid
                NULL,           // PW
                NULL,           // Locale
                0,              // flags
                NULL,           // Authority
                NULL,           // Context
                &pSvc
                ) != S_OK)
    return 0;
 
 HRESULT hres;
 hres = pSvc->ExecQuery(
 bstr_t("WQL"), 
 
 //bstr_t("SELECT MACAddress FROM Win32_NetworkAdapter"),
 bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"),
 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
 NULL,
 &pEnumerator);
 if (FAILED(hres))
 {
 pSvc->Release();
 pLoc->Release();
 CoUninitialize();
 return -1; 
 }
 ULONG uCount = 1, uReturn;
 IWbemClassObject * pclsObj = NULL;
 int nRez = 0;
 char sRezDescr[256] = {'\0'};
 char sRezModel[128] = {'\0'};
 
 CString szTmp = "";
 while (pEnumerator)
 {
 VARIANT vtProp;
 HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
 &pclsObj, &uReturn);
 if(0 == uReturn) break;
 hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);
 if(vtProp.boolVal)
 { 
 LONG lstart, lend;
 LONG idx = -1;
 BSTR* pbstr;
 SAFEARRAY *sa;
 hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);
 if(!FAILED(hr))
 {
  int i_str_no = (int)vtProp.bstrVal; 
  char ch [20];
  itoa(i_str_no,ch,10);
  AfxMessageBox(ch);
  //cout << "Description: " << vtProp.bstrVal << endl; 
 }
 hr = pclsObj->Get(L"DNSHostName", 0, &vtProp, 0, 0);
 if(!FAILED(hr))
 {
  int i_str_no = (int)vtProp.bstrVal; 
  char ch [20];
  itoa(i_str_no,ch,10);
  AfxMessageBox(ch);
  //cout << "DNS:" << vtProp.bstrVal << endl;
 }
 hr = pclsObj->Get(L"IPAddress", 0, &vtProp, 0, 0);
 //hr = pclsObj->Get(L"MACAddress", 0, &vtProp, 0, 0);
  

This part of code is initializing WMI and then creating Query and getting IP address.

Points of Interest

Purpose of this artical is to get IP address and know how to use WMI. Even I am also beginner for WMI.When I was searching on the net how to get IP address It was difficualt for me to get that information. Thats why I have posted this articale.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here