Click here to Skip to main content
15,885,782 members
Articles / Desktop Programming / MFC

WMI Sample Get IP address

Rate me:
Please Sign up or sign in to vote.
1.30/5 (10 votes)
11 Jun 2009CPOL1 min read 74.8K   2.3K   24   9
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)


Written By
Technical Lead Tata Consultancy Servcies
India India
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.

Comments and Discussions

 
GeneralThis article was helpful Pin
dixdesire18-Sep-16 7:09
dixdesire18-Sep-16 7:09 
GeneralMy vote of 5 Pin
Atique Rahman22-Apr-11 1:59
professionalAtique Rahman22-Apr-11 1:59 
Questionquestion about multiple adapter Pin
fkeujjpdc19-Dec-10 20:13
fkeujjpdc19-Dec-10 20:13 
GeneralMemory Leak Pin
zhan8261-Dec-09 16:29
zhan8261-Dec-09 16:29 
Generalsrc Pin
ischalli12-Sep-08 4:10
ischalli12-Sep-08 4:10 
GeneralThank very much Pin
NovaNuker7-Aug-08 1:13
NovaNuker7-Aug-08 1:13 
GeneralThank you my friend !!! Pin
AWERT123200324-Jun-08 17:35
AWERT123200324-Jun-08 17:35 
GeneralYou missed a bit when copying Pin
David Pritchard24-Jun-08 3:53
David Pritchard24-Jun-08 3:53 
GeneralCompile Error - Kindly help! Pin
Priya_Sundar2-Oct-07 20:36
Priya_Sundar2-Oct-07 20:36 

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

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