Click here to Skip to main content
Licence CPOL
First Posted 1 Jul 2007
Views 35,434
Downloads 832
Bookmarked 23 times

WMI Sample Get IP address

By | 11 Jun 2009 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

chaitanya shah

Technical Lead
Tata Consultancy Servcies
India India

Member

Follow on Twitter Follow on Twitter
I have experience in iOs, Objective C,Java,C++/Vc++ 6.0,vc.Net,MFC,ATL,COM,WTL.
 
You can contact me on chaitanya.ce@gmail.com if you have any query.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberSaint Atique1:59 22 Apr '11  
Questionquestion about multiple adapter Pinmemberfkeujjpdc20:13 19 Dec '10  
GeneralMemory Leak Pinmemberzhan82616:29 1 Dec '09  
Generalsrc Pinmemberischalli4:10 12 Sep '08  
GeneralThank very much PinmemberNovaNuker1:13 7 Aug '08  
GeneralThank you my friend !!! PinmemberAWERT123200317:35 24 Jun '08  
GeneralYou missed a bit when copying PinmemberDavid Pritchard3:53 24 Jun '08  
GeneralCompile Error - Kindly help! PinmemberPriya_Sundar20:36 2 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 11 Jun 2009
Article Copyright 2007 by chaitanya shah
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid