|
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.
|
|
|
|
|
Did you try to set
"C:\Program Files\Microsoft SDKs\Windows\v6.0\Include" first in include paths?
|
|
|
|
|
When I set C:\Program Files\Microsoft SDKs\Windows\v6.0\Include on top of the include path list, I get the following message:
Cannot open include file: 'sal.h'
Please advice.
|
|
|
|
|
Did you set it before "$(VCInstallDir)PlatformSDK\include"?
|
|
|
|
|
Where and how can I set it?
|
|
|
|
|
Set "C:\Program Files\Microsoft SDKs\Windows\v6.0\Include" before "$(VCInstallDir)PlatformSDK\include".
|
|
|
|
|
Hi all,
I want to count number of words depend on the space character. That means I want to tokernize a string. How can I do that. I'm looking at the strtok() function. But I can't use char* there in the following code.
int word()<br />
{<br />
char *token;<br />
std::string str = "This is a line of text";<br />
<br />
token = strtok(str, " ");<br />
do<br />
{<br />
printf("token: %s\n", token);<br />
}<br />
while (token = strtok(NULL, " "));<br />
}
Can somebody help me to do this.
Thanks
I appreciate your help all the time...
CodingLover
|
|
|
|
|
Have you tried using CStringT::Tokenize ?
|
|
|
|
|
Ah, actually I just want to use standard C++ only. Not with MFC pal. Reason is that until now whole my application develop on without C++ and I want to keep it as it is.
I appreciate your help all the time...
CodingLover
|
|
|
|
|
If you want to stick to C++, then better use std::stringstream . Check this link[^] about how to split strings using stringstream .
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
The function isspace will help you in this case. Loop through & check character by character. If you encounter a space, increment the count..
Regards,
Rane
|
|
|
|
|
CodingLover wrote: Ah, actually I just want to use standard C++ only. Not with MFC pal. Reason is that until now whole my application develop on without C++ and I want to keep it as it is.
???????????????????????????
Bram van Kampen
|
|
|
|
|
CodingLover wrote: my application develop on without C++
???????
|
|
|
|
|
Hi,
Why not go back to basics, read chars one by one, and use isspace(x)
About three nested loops will do the trick, and you can debug it yourself.
You then do not need to spend hours scouring the Internet for obscure specifications, and even more hours studying same.
I know the performance argument. Unless if you have gazillions of data to sift thru, I personally have found that issue largely accademic.
Regards
Bram van Kampen
|
|
|
|
|
Hi,
I have a Dialog Based App, which requires different menu's in the Main Dialog depending on a situation investigated during InitInstance(). dlg.m_nMode is set by InitInstance();
CMenu * pMenu=GetMenu();
pMenu->Detach();
switch(m_nMode){
case MODE_NOT_STARTED:
ASSERT(0);//Should Never Happen
exit(0);
break;
case MODE_MASTER:
pMenu->LoadMenu(IDR_MENU_MASTER);
SetMenu(pMenu);
SetStatusText("Running Process...");
break;
case MODE_SLAVE:
pMenu->LoadMenu(IDR_MENU_SLAVE);
SetMenu(pMenu);
SetStatusText("Monitoring Process...");
break;
}
All behaves as expected, except that on rare occasions I get an exception when closing down relating to the unwinding of the handle map.
Bram van Kampen
|
|
|
|
|
Bram van Kampen wrote: CMenu * pMenu=GetMenu();
pMenu->Detach();
.
.
pMenu->LoadMenu(IDR_MENU_MASTER);
I think the problem is because of using the pointer returned by GetMenu() function. Such pointers are actually temporary objects and will be destroyed in while process is Idle.
So instead of
CMenu * pMenu=GetMenu();<br />
pMenu->Detach();
try
CMenu * pMenu= new CMenu();
And dont forget to delete the pointer after use.
|
|
|
|
|
Well, that's how I Started. If I do not use pMenu->Detach() I get other Runtime Errors. Also, the temporary nature of some pointers manifest itself between framework calls. The Only thread is running in My Module, so there is nothing that can (or should be able to)change things between the lines of code in the one function. In Short: The thread does Not do OnIdle() between the Lines of a function I wrote, It may only do so after I leave a Framework Call.
Thanks,
Bram van Kampen
modified on Monday, September 1, 2008 9:52 PM
|
|
|
|
|
Did you remove previous menu before load new menu?
|
|
|
|
|
CMenu * pMenu=GetMenu();
pMenu->Detach();
What else should or could I do?
regards
Bram van Kampen
|
|
|
|
|
And did you use of DestroyMenu?
Of MSDN: The DestroyMenu function destroys the specified menu and frees any memory that the menu occupies.
|
|
|
|
|
Sorry for the Dalay,
Hamid. wrote: And did you use of DestroyMenu?
No, I Did not, and that solved it.
Thanks.
Bram van Kampen
|
|
|
|
|
And vice versa Im very fast.
|
|
|
|
|
Hi,
I have a DOS console application which sends a lot of data to STDOUT
I would like to make it more readable Create a WIN32 GUI to Display
the information maybe in a EditBox
I Statred by Doing a CreateProcess to Create the WIN32 app
My question What is the best way of Communcating the Data Between
The Dos Console process and the Win32 GUI process
I have done some reading and it seems Anoymous pipes are best suited
for what I have in mind
Any comments
Greatly appreciated
|
|
|
|
|
Well, you have One App running as a Console Process. To pipe the Output to a gui, you need a gui in another app that monitors your Console Process as a Debugger. Matt Pietreck wrote a lot about this, and it is a way of doing things.
The quickest and easiesr way is I think, to start a New Windows GUI based App, (Probably Dialog Based, give it a main() global function which calls your App, and write a number of Macro's which convert the stdout calls to writing to an edit box in your Main Dlg.
BTW Have you ever thought of piping your output to a file, and then reading it with say Notepad.
Console Programs and GUI programs have entirely different design phylosophies as underlay, and cannot be readily converted from one into the other.
Bram van Kampen
|
|
|
|
|
If I am to understand your suggestion re: writting macros to convert STDOUt calls to Dialog base writes e.g Writting to an EditBox Dialog
Would I stil have tso apps Dos Win GUI
re: writting the Dos to GUI would be a huge Task
|
|
|
|
|