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;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1;
}
hres = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1;
}
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;
}
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1;
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
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;
}
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << " OS Name : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
}
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();
return 0;
}</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.
|