Click here to Skip to main content
15,920,217 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Linking error! Pin
Naveen31-Aug-08 20:44
Naveen31-Aug-08 20:44 
GeneralRe: Linking error! Pin
Dhiraj kumar Saini31-Aug-08 20:52
Dhiraj kumar Saini31-Aug-08 20:52 
GeneralRe: Linking error! Pin
Dhiraj kumar Saini31-Aug-08 20:25
Dhiraj kumar Saini31-Aug-08 20:25 
QuestionCustomizing a title bar (mfc and/or windows forms applications) Pin
James_Zhang31-Aug-08 17:38
James_Zhang31-Aug-08 17:38 
AnswerRe: Customizing a title bar (mfc and/or windows forms applications) Pin
Hamid_RT31-Aug-08 18:13
Hamid_RT31-Aug-08 18:13 
GeneralRe: Customizing a title bar (mfc and/or windows forms applications) Pin
James_Zhang1-Sep-08 8:40
James_Zhang1-Sep-08 8:40 
GeneralRe: Customizing a title bar (mfc and/or windows forms applications) Pin
Hamid_RT1-Sep-08 22:46
Hamid_RT1-Sep-08 22:46 
QuestionProblem using WMI Pin
lhkok31-Aug-08 17:29
lhkok31-Aug-08 17:29 
HI all,

I am quite new to C++. Recently, I am working on a project that requires me to get the processorId from computers. After a quick search on the internet, I found that WMI is the what I need. Subsequently, I get the following code from MSDN (http://msdn.microsoft.com/en-us/library/aa390423(VS.85).aspx):

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

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

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

    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------

    hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres))
    {
        cout << "Failed to initialize COM library. Error code = 0x" 
            << hex << hres << endl;
        return 1;                  // Program has failed.
    }

    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------
    // Note: If you are using Windows 2000, you need to specify -
    // the default authentication credentials for a user by using
    // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
    // parameter of CoInitializeSecurity ------------------------

    hres =  CoInitializeSecurity(
        NULL, 
        -1,                          // COM authentication
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
        );

                      
    if (FAILED(hres))
    {
        cout << "Failed to initialize security. Error code = 0x" 
            << hex << hres << endl;
        CoUninitialize();
        return 1;                    // Program has failed.
    }
    
    // Step 3: ---------------------------------------------------
    // Obtain the initial locator to WMI -------------------------

    IWbemLocator *pLoc = NULL;

    hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLoc);
 
    if (FAILED(hres))
    {
        cout << "Failed to create IWbemLocator object."
            << " Err code = 0x"
            << hex << hres << endl;
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    // Step 4: -----------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method

    IWbemServices *pSvc = NULL;
	
    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
         NULL,                    // User name. NULL = current user
         NULL,                    // User password. NULL = current
         0,                       // Locale. NULL indicates current
         NULL,                    // Security flags.
         0,                       // Authority (e.g. Kerberos)
         0,                       // Context object 
         &pSvc                    // pointer to IWbemServices proxy
         );
    
    if (FAILED(hres))
    {
        cout << "Could not connect. Error code = 0x" 
             << hex << hres << endl;
        pLoc->Release();     
        CoUninitialize();
        return 1;                // Program has failed.
    }

    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


    // Step 5: --------------------------------------------------
    // Set security levels on the proxy -------------------------

    hres = CoSetProxyBlanket(
       pSvc,                        // Indicates the proxy to set
       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
       NULL,                        // Server principal name 
       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
       NULL,                        // client identity
       EOAC_NONE                    // proxy capabilities 
    );

    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    // For example, get the name of the operating system
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("SELECT * FROM Win32_OperatingSystem"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);
    
    if (FAILED(hres))
    {
        cout << "Query for operating system name failed."
            << " Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------
 
    IWbemClassObject *pclsObj;
    ULONG uReturn = 0;
   
    while (pEnumerator)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
            &pclsObj, &uReturn);

        if(0 == uReturn)
        {
            break;
        }

        VARIANT vtProp;

        // Get the value of the Name property
        hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
        wcout << " OS Name : " << vtProp.bstrVal << endl;
        VariantClear(&vtProp);
    }

    // Cleanup
    // ========
    
    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    pclsObj->Release();
    CoUninitialize();

    return 0;   // Program successfully completed.
	
}</wbemidl.h></comdef.h></iostream>


I added ->#include "stdafx.h"<->
I get the following Error:
fatal error C1083: Cannot open include file: 'Wbemidl.h': No such file or directory

I search my computer and found that the header file is located at C:\PROGRAM FILES\MICROSOFT SDKS\WINDOWS\V6.0A\INCLUDE.
I then included the path C:\PROGRAM FILES\MICROSOFT SDKS\WINDOWS\V6.0A\INCLUDE under tools->options->categories.
I proceeded to compile it and get the following errors (shortened version):

c:\program files\microsoft sdks\windows\v6.0a\include\wbemcli.h(1737) : error C2061: syntax error : identifier '__RPC__in'<br />
c:\program files\microsoft sdks\windows\v6.0a\include\wbemcli.h(1744) : error C2061: syntax error : identifier '__RPC__in_opt'<br />
c:\program files\microsoft sdks\windows\v6.0a\include\wbemcli.h(1748) : error C2061: syntax error : identifier '__RPC__deref_out_opt'<br />
c:\program files\microsoft sdks\windows\v6.0a\include\wbemcli.h(1751) : error C2061: syntax error : identifier '__RPC__in'<br />
c:\program files\microsoft sdks\windows\v6.0a\include\wbemcli.h(3071) : error C2061: syntax error : identifier '__RPC__in'<br />
c:\program files\microsoft sdks\windows\v6.0a\include\wbemcli.h(3723) : error C2061: syntax error : identifier '__RPC__in_opt'<br />
c:\program files\microsoft sdks\windows\v6.0a\include\wbemprov.h(273) : error C2146: syntax error : missing ';' before identifier 'LPWSTR'<br />
c:\program files\microsoft sdks\windows\v6.0a\include\wbemprov.h(273) : fatal error C1004: unexpected end of file found<br />

Again, I searched around the internet. From other people's question and answers, there is one suggesting that I should add
#include <rpcsal.h></rpcsal.h>

I added #include <rpcsal.h></rpcsal.h> and get the following error:
fatal error C1083: Cannot open include file: 'sal.h': No such file or directory

Then, I add the following diretory to the the categories: C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO 9.0\VC\INCLUDE
Next, I get this error: error C2061: syntax error : identifier 'DWORD_PTR'
I have no idea how to continue from here. I am afraid I made some mistake in the middle of some where.

For your information, I installed the Visual Studio Express 2008 before installing Visual Studio 6.0.

Is there anyone can help me?
I would like to learn to use the WMI.
I have read another tutorial/sample from the Code Project, but I got the same problem since it was also using #include <wbemidl.h>.


**Update**

To let you better understand my situation, I would like to add some details.
I have multiple version of visual studio(VS Express 2008, VS 6) installed in my PC with VS 6 being the latest install. I have installed the service pack 6 as well.
In my Tools-> Option-> Directories, the default paths included are:
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE<br />
C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE<br />
C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE


Due to the fatal error C1083: Cannot open include file: 'Wbemidl.h': No such file or directory error, I included C:\PROGRAM FILES\MICROSOFT SDKS\WINDOWS\V6.0A\INCLUDE

As I browse through some forum, there is one guy(http://www.tech-archive.net/Archive/Media/microsoft.public.windowsmedia.sdk/2007-02/msg00105.html) suggested to include rpcsal.h . Again, I have to include C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO 9.0\VC\INCLUDE since the header file is not available in those four directories which I have included earlier. However, the error persist as mentioned above in the first post.

This morning, I changed the order of the path in Tools-> Option-> Directories from:
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE
C:\PROGRAM FILES\MICROSOFT SDKS\WINDOWS\V6.0A\INCLUDE
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO 9.0\VC\INCLUDE
To:
C:\PROGRAM FILES\MICROSOFT SDKS\WINDOWS\V6.0A\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE
C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO 9.0\VC\INCLUDE
Without changing any line of the codes, I get a different error messages which are:

c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(3417) : warning C4035: 'ReadPMC' : no return value<br />
c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(3447) : warning C4035: 'ReadTimeStampCounter' : no return value<br />
c:\program files\microsoft sdks\windows\v6.0a\include\wingdi.h(4340) : warning C4068: unknown pragma<br />
c:\program files\microsoft sdks\windows\v6.0a\include\wingdi.h(4341) : warning C4068: unknown pragma<br />
c:\program files\microsoft visual studio\vc98\include\comdef.h(695) : error C2065: 'IAsyncSetup' : undeclared identifier<br />
c:\program files\microsoft visual studio\vc98\include\comdef.h(710) : error C2065: 'IClassAccess' : undeclared identifier<br />
c:\program files\microsoft visual studio\vc98\include\comdef.h(712) : error C2065: 'IClassAdmin' : undeclared identifier<br />
c:\program files\microsoft visual studio\vc98\include\comdef.h(715) : error C2065: 'IClassRefresh' : undeclared identifier<br />
c:\program files\microsoft visual studio\vc98\include\comdef.h(750) : error C2065: 'IEnumClass' : undeclared identifier<br />
c:\program files\microsoft visual studio\vc98\include\comdef.h(762) : error C2065: 'IEnumPackage' : undeclared identifier<br />
c:\program files\microsoft visual studio\vc98\include\comdef.h(1078) : error C2065: 'IXMLElementNotificationSink' : undeclared identifier<br />
Error executing cl.exe.<br />

Is this a problem of my installation?
Or there are multiple version of header files with the same file names?
I don't understand why the header files is scattering in different places. Also, why rpcsal.h is not available in VC98\include folder nor is it available in C:\PROGRAM FILES\MICROSOFT SDKS\WINDOWS\V6.0A\INCLUDE?
Did I included any functions all files which is meant for .NET?

I am so confused. Please advice.
AnswerRe: Problem using WMI Pin
Hamid_RT31-Aug-08 18:20
Hamid_RT31-Aug-08 18:20 
GeneralRe: Problem using WMI Pin
lhkok31-Aug-08 21:21
lhkok31-Aug-08 21:21 
GeneralRe: Problem using WMI Pin
Hamid_RT1-Sep-08 6:36
Hamid_RT1-Sep-08 6:36 
GeneralRe: Problem using WMI Pin
lhkok1-Sep-08 21:58
lhkok1-Sep-08 21:58 
GeneralRe: Problem using WMI Pin
Hamid_RT1-Sep-08 22:47
Hamid_RT1-Sep-08 22:47 
QuestionString tokernize looking at the space character Pin
CodingLover31-Aug-08 17:14
CodingLover31-Aug-08 17:14 
QuestionRe: String tokernize looking at the space character Pin
bob1697231-Aug-08 17:27
bob1697231-Aug-08 17:27 
NewsRe: String tokernize looking at the space character Pin
CodingLover31-Aug-08 18:03
CodingLover31-Aug-08 18:03 
GeneralRe: String tokernize looking at the space character Pin
Jijo.Raj31-Aug-08 19:46
Jijo.Raj31-Aug-08 19:46 
GeneralRe: String tokernize looking at the space character Pin
Rane31-Aug-08 19:51
Rane31-Aug-08 19:51 
GeneralRe: String tokernize looking at the space character Pin
Bram van Kampen1-Sep-08 16:15
Bram van Kampen1-Sep-08 16:15 
GeneralRe: String tokernize looking at the space character Pin
bob169726-Sep-08 5:06
bob169726-Sep-08 5:06 
AnswerShould we have a Function for Every Flavour of Char Operations ? Pin
Bram van Kampen1-Sep-08 16:11
Bram van Kampen1-Sep-08 16:11 
QuestionOccasional Exceptions when Setting Menus. Pin
Bram van Kampen31-Aug-08 14:48
Bram van Kampen31-Aug-08 14:48 
AnswerRe: Occasional Exceptions when Setting Menus. Pin
Naveen31-Aug-08 15:23
Naveen31-Aug-08 15:23 
GeneralRe: Occasional Exceptions when Setting Menus. [modified] Pin
Bram van Kampen1-Sep-08 15:09
Bram van Kampen1-Sep-08 15:09 
AnswerRe: Occasional Exceptions when Setting Menus. Pin
Hamid_RT31-Aug-08 18:28
Hamid_RT31-Aug-08 18:28 

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.