Thread: How do you access information from win32_operatingsystem class?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    Question How do you access information from win32_operatingsystem class?

    Hi,

    I'm trying to access information from the win32_operatingsystem class using C++ I've looked at Win32_operatingsystem class on msdn but I don't understand how i would access the information and what type of headers I would require.

    I'm using windows vista home premium if that makes any difference.

    Any help would be greatly appreciated.

    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    #ifndef _WIN32_DCOM
    #   define _WIN32_DCOM
    #endif
    #ifndef UNICODE
    #   define UNICODE
    #endif
    #include <Windows.h>
    
    #include <comdef.h>
    #include <comutil.h>
    
    #include <Wbemidl.h>
    #pragma comment(lib, "wbemuuid.lib")
    
    _COM_SMARTPTR_TYPEDEF(IWbemLocator, __uuidof(IWbemLocator));
    _COM_SMARTPTR_TYPEDEF(IWbemServices, __uuidof(IWbemServices));
    _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, __uuidof(IEnumWbemClassObject));
    _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject));
    
    //------------------------------------------------------------------------------
    
    inline void TESTHR(HRESULT hr) 
    { 
        if (FAILED(hr))
            _com_issue_error(hr);
    }//TESTHR
    
    //------------------------------------------------------------------------------
    
    void GetProperty(IWbemClassObjectPtr &obj, const wchar_t *prop_name, 
                     _variant_t &prop_val)
    {
        CIMTYPE cim_type;
        TESTHR(obj->Get(prop_name, 0, &prop_val, &cim_type, 0));
    }//GetProperty
    
    //------------------------------------------------------------------------------
    
    void PrintProperty(IWbemClassObjectPtr &obj, const wchar_t *prop_name)
    {
        _variant_t prop_val;
        GetProperty(obj, prop_name, prop_val);
    
        wcout << prop_name << L" = ";
        if (prop_val.vt == VT_NULL)
            wcout  << L"NULL" << endl;
        else
        {
            bool bExcept = false;
            try
            {
                wcout << (const wchar_t*)(_bstr_t)prop_val << endl;
            }//try
            catch (_com_error&) {bExcept = true;}
            
            if (!wcout)
                wcout.clear();
            if (bExcept)
                wcout << L"{failed to convert variant of type " 
                      << prop_val.vt << L"}" << endl;
        }//else
    }//PrintProperty
    
    //------------------------------------------------------------------------------
    
    void print_OS_info()
    {
        try
        {
            IWbemLocatorPtr pLoc;
            TESTHR(CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, 
                                    IID_IWbemLocator, (void**)&pLoc));
            
            IWbemServicesPtr pSvc;
            TESTHR(pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), 
                                       0, 0, 0, 0, 0, 0, &pSvc));
    
            TESTHR(CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, 0,
                                     RPC_C_AUTHN_LEVEL_CALL, 
                                     RPC_C_IMP_LEVEL_IMPERSONATE, 0,EOAC_NONE));
    
            IEnumWbemClassObjectPtr pObjEnum;
            const long flags = WBEM_FLAG_FORWARD_ONLY | 
                               WBEM_FLAG_RETURN_IMMEDIATELY;
            TESTHR(pSvc->ExecQuery(_bstr_t("WQL"), 
                                   _bstr_t("SELECT * FROM Win32_OperatingSystem"),
                                   flags, 0, &pObjEnum));
    
            IWbemClassObjectPtr pOS;
            ULONG returned = 0;
            for (;;)
            {
                TESTHR(pObjEnum->Next(WBEM_INFINITE, 1, &pOS, &returned));
                if (!returned)
                    break;
    
                PrintProperty(pOS, L"Caption");
                PrintProperty(pOS, L"Version");
                PrintProperty(pOS, L"Codeset");
                PrintProperty(pOS, L"CSDVersion");
                PrintProperty(pOS, L"CSName");
                PrintProperty(pOS, L"TotalVisibleMemorySize");
             }//for
        }//try
        catch (_com_error &err)
        {
            cerr << "COM error : " << hex << setw(8) << err.Error() << endl;
            _bstr_t bstrSrc = err.Source();
            wchar_t *src = (wchar_t*)bstrSrc;
            if (src)
                wcout << L", src=[" << src << L"]" << endl;
                
            _bstr_t bstrDesc = err.Description();
            wchar_t *desc = (wchar_t*)bstrDesc;
            if (desc)
                wcout << L", desc=[" << desc << L"]" << endl;
    
            const wchar_t *errmsg = err.ErrorMessage();
            if (errmsg)
            {
                wcout << L", msg=[" << errmsg << L"]" << endl;
                LocalFree((HLOCAL)errmsg);
            }//if
        }//catch
    }//print_PhysicalMedia_info
    
    //------------------------------------------------------------------------------
    
    int main()
    {
        CoInitializeEx(0, COINIT_MULTITHREADED);
        TESTHR(CoInitializeSecurity(0, -1, 0, 0, RPC_C_AUTHN_LEVEL_DEFAULT,
                                    RPC_C_IMP_LEVEL_IMPERSONATE, 0, 
                                    EOAC_NONE, 0));
    
        print_OS_info();
    
        CoUninitialize();
        return 0;
    }//main
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access to members of protected base class
    By DL1 in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2009, 10:30 AM
  2. gcc: Template class child can't directly access parent fields
    By SevenThunders in forum C++ Programming
    Replies: 11
    Last Post: 03-17-2009, 06:05 AM
  3. Class access enforcement
    By Chris87 in forum C++ Programming
    Replies: 11
    Last Post: 03-09-2009, 10:12 PM
  4. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM
  5. implicit constructor and class access
    By Clarinetster in forum C++ Programming
    Replies: 1
    Last Post: 11-22-2001, 03:59 PM