PDA

View Full Version : QT WMI Classes



justatiq
1st February 2010, 09:22
Greetings to Everybody:
I want to use WMI Classes in QT/2009.05 C++ but I do not know how to, I am using QT seperately mean without Eclipse, VS or anything...
I want to create a System Restore point using QT/C++ but not able to do it...in VB it is simple two line like


Set SRP = GetObject( "winmgmts:\\.\root\default:Systemrestore" )
CSRP = SRP.CreateRestorePoint( "Hacked the registry", 0, 100 )


but when I use in QT like


#include <comdef.h>
#include <Wbemidl.h>
#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)) .........

then it gives errors like
Wbemidl.h no such file or directory...
comdef.h no such file or directory...
CoIntialize is not declared in this context...

Please help me, how can I use WMI classes in QT/2009.05/C++ . Also let me know if I have to add some libraries or something...
Any help would be appreciated.
Many thanks in advance

high_flyer
1st February 2010, 09:48
Your project is not configured to find the WMI environment.
You have to add to your pro file (or project configuration under MSVS) the include and lib paths, as well as the libs you are linking against.

justatiq
3rd February 2010, 14:24
Thanks alot for your reply, but I am not using QT with MSVS, so is there any way arround to let QT go without MSVS or I have to use it anyway.
and if yes then would you please tell me how to configure/include the WMI lib paths...in MSVS/QT
Any help would be a great favour.
thanks in advance.

high_flyer
3rd February 2010, 18:35
Read my post again, I only mentioned MSVC in case you were using it.
You have to add the include and lib paths to you pro file.
Don't forget to specify the libs to link against.

justatiq
4th February 2010, 10:39
Dear I have already added libs path in QT but no success in my .pro like


LIBS += WbemUuid.Lib \
LIBS += C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib\WbemUuid.Lib

I have used both the above ways but no success.

If you know how to then why you not write me a short code or paste your .pro file in which you have done it...
or let me know how I can do it step by step.
Please do not think me that I am expert in QT I am just using it since three weeks or so...
So please help me to done it.

high_flyer
4th February 2010, 12:43
f you know how to then why you not write me a short code or paste your .pro file in which you have done it...
Because we are not here to do your work for you, but to help you learn how to do it your self.
Besides, I don't know which libs you want to link and where they are on your system, which is almost all the info you have to type anyway.

Your syntax is wrong for the lib path.
Read here (http://doc.trolltech.com/4.5/qmake-variable-reference.html#libs)on how to specify linkage libs and paths.

Oh, and you also have to add the include paths!!

justatiq
2nd March 2010, 14:08
If my syntax is wrong for Lib path then why does not QT gives error or something to notify me that I am doing that bit wrong ? is it a bug in QT ?
and as you said you are here to help others then try to be polite with others not rude and please in reply use some useful things not only description like do this and do this give some examples as well, which will help others to learn who are not well familiar with QT.
And I already told you that I need WMI classes so I need WMI library. Please write some sort of code examples if you know.

kuzulis
2nd March 2010, 14:47
justatiq, IMHO, there is the problem


...
# include <Wbemidl.h>
...
# Pragma comment (lib, "wbemuuid.lib")


Let me explain (IMHO): these headers <Wbemidl.h> and the library "wbemuuid.lib" is used only for MSVS compiler (but you're using MinGW). Ie This library is the wrappers on the shared libraries from the /Windows/System32/.
ie WMI classes really need is in the system *. dll Windows - and you just Use QLoader to access the *. dll.
ie you try to determine in what their *. dll are required for you classes WMI. Read what is said in MSDN on this subject.

PS: I may be mistaken.

justatiq
4th March 2010, 10:43
Hi kuzulis:
Nice to see your reply and it really helped me to boost my moral. I went on MSDN and read about WMI Classes but they have the same code as I have mentioned in my first Post.
But I found one line in the comment i.e.


add #pragma comment(lib, "comsuppw.lib") line

and one more thing they stated that, the above mentioned code is working in latest vc++. I think I should install Microsoft Visual Studio 2008 ?
Please let me know am I going on the right track or not ?
As you said WMI Classes really need is in the system *.dll windows - and you just use QLoader to access the *.dll
Would you please tell me which files should i put in System or system32 and which files I load using Qloader (Please do not mind my silly questions - I do not know much about QT)

and again thanks for your help.

wysota
4th March 2010, 11:10
is it a bug in QT ?
No, it is not. And it's Qt, not QT.

and as you said you are here to help others then try to be polite with others not rude and please in reply use some useful things not only description like do this and do this give some examples as well, which will help others to learn who are not well familiar with QT.
Please read qmake documentation if you haven't already done so. Also please browse the net searching for a general answer what to do when a compiler issues a "no such file or directory" error.

And I already told you that I need WMI classes so I need WMI library. Please write some sort of code examples if you know.
Please read WMI documentation on how to enable it in your projects.

Examples for respective problems will be found in documentation for each of the components.

You can't just shoot blindly hoping to find a proper solution by chance.

kuzulis
4th March 2010, 12:35
justatiq,

here's a link how to make the example of Delphi: http://www.delphikingdom.com/asp/viewitem.asp?catalogid=698

I think along the lines you do.

PS:
1. only for you there is one problem - article in Russian :). But do not worry - use translate.google :)
2. even as an option - look for something similar in your language.

kuzulis
4th March 2010, 12:59
maybe use: http://disphelper.sourceforge.net/

wysota
4th March 2010, 13:41
maybe use: http://disphelper.sourceforge.net/

There is ActiveQt, you know...

justatiq
8th March 2010, 09:42
Kuzulis:
Your replies are really helping me to go ahead... thanks for all that.
Dear I have already a complete project and also working in Delphi with WMI and have no problems at all. even I can do it in VB script ...

But now I want to do the same in Qt but do not know how ?

Here are many people who come and comments do this and do this.. but they do not tell how ? like you give me some key points, thanks for that.
Dear if you have something related to Qt then please let me know more.

to wysota:
I have googled but did not find any solutions (I think you also tried) thats why i put the question here, if you do not know then this is not my fault... I put the question for those who knows.
It is very easy to pin point mistakes of others but I think difficult to write code what is requied... I think you need not to come and pick something from the replies and distrub others.
so if you do not know then please keep away. sorry for any mistakes Many thanks

high_flyer
8th March 2010, 09:49
But now I want to do the same in Qt but do not know how ?

Here are many people who come and comments do this and do this.. but they do not tell how ? like you give me some key points, thanks for that.
Dear if you have something related to Qt then please let me know more.

You are missing the point.
People are giving you pointers so that you can know what to look for in the documentation and READ and LARN from it.
No one is going to chew anything for you.
So if wysota says "ActiveQt", it means, you have to open the documentation about ActiveQt, and then you will KNOW.


to wysota:
I have googled but did not find any solutions (I think you also tried) thats why i put the question here, if you do not know then this is not my fault... I put the question for those who knows.
be careful.
look at the wysotas statistics on this forum.
You will see, that appart from being the top poster here, you can see also see how many thanked posts he has, which would tell you, that when it comes to Qt, he knows and helps A LOT!

Posting questions on the forum is good and nice, but you have to use the help you get, and not complain about people not supllying you wiht the full solution to your problem!

wysota
8th March 2010, 10:25
But now I want to do the same in Qt but do not know how ?

I think the problem is you are expecting to be given a complete piece of working code that does what you want. Which is unlikely to happen :) If you know how to do the thing in Delphi or VB then just port the same idea to C++. Qt won't help you much here as it is meant mainly for cross-platform solutions and obviously what you are trying to do is not cross-platform. If the module you are trying to access has a (D)COM interface (and from what I see it probably does) then you can use ActiveQt to use it. If it doesn't have a COM interface then I'm sure MSDN contains at least one snippet of code showing how to use WMI in C++. It may not do exactly what you want and in such case you have to adapt the code to do what you need (i.e. call a different routine from the library).

justatiq
14th March 2010, 07:33
I'm sure MSDN contains at least one snippet of code showing how to use WMI in C++.

Yes you are right, there is a snippet for that... and I did that but I got some errors which are reported in my first post at the top.

I would like to tell that I do not need complete code or any piece of code I just want to configure Qt with WMI, I tried alot but still getting errors (sometime library not found sometime no such file or directory, same errors as reported in first post).
I would be very thankful if somebody tell me how can I configure Qt with WMI, I do not need code, once I configure Qt to use WMI code I will do coding at my end. I have already done in delphi but Qt and delphi are different (as for as I know) as in Delphi there is a way to use WMI Classes... something like that
Delphi:
Project -> Import type library
select Microsoft WMI script v1.2 library
install it
and you are able to use WMI classes in Delphi.
I thought there would be something similar in Qt and this is what I am trying to ask other people on this forum not any code.
If someone knows that then please let me know how can I do that in Qt.
Thanks in advance.

wysota
14th March 2010, 09:28
Yes you are right, there is a snippet for that... and I did that but I got some errors which are reported in my first post at the top.
So fix them, A missing file from Windows SDK doesn't have anything to do with Qt.


I would be very thankful if somebody tell me how can I configure Qt with WMI
What do you mean by "configure Qt with WMI"?

I thought there would be something similar in Qt and this is what I am trying to ask other people on this forum not any code.
Again, Qt has nothing to do with this. Create a new project, strip it all of Qt code and focus on including the offending file. Before other things, check if you have this file on your disk at all and where it is.

justatiq
14th March 2010, 10:23
What do you mean by "configure Qt with WMI"?
I mean to use WMI Classes using Qt, If I write some code which is pointing to WMI classes like

#include <Wbemidl.h> then it should work but it does not work. and I think you did not read all the posts. because no file is missing all are on my computer and I can see them but when I reference them then Qt does not find them and gives error.

Create a new project, strip it all of Qt code and focus on including the offending file. Before other things, check if you have this file on your disk at all and where it is.
I have done this and I have all the files but Qt is not finding them. I am getting error like there is no file or directory (but the file is there) then after this I have error on CoIntializeEx...
I request you to read all the posts and then reply because whatever you have said has already been discussed at the top.
So please try to help on configuring Qt with WMI. Can you please try at your PC ?
Thanks

wysota
14th March 2010, 16:23
then it should work but it does not work
And what does Qt have to do with this?


and I think you did not read all the posts. because no file is missing all are on my computer and I can see them but when I reference them then Qt does not find them and gives error.
"Qt" does not look for any files. Your compiler does. So tell your compiler where the file is instead of blaming Qt for things it has nothing to do with. Locate the file in question and see if it is in your compiler include search path. If not then either reference it in your code using a path that the compiler (or the C preprocessor, to be exact) can resolve (i.e. using an absolute path or path relative to one of the search paths of your compiler) or add the path where the file resides to your compiler search path (you can do that for a specific project using QMake's INCLUDEPATH variable).

ChrisW67
15th March 2010, 01:27
To summarise for justatiq:

The C++ compiler needs to be able to find include files that you do not specify a complete path to, e.g Wbemidl.h. You are using MingW, try issuing
gcc -v -c helloworld.c to see where it will look by default. The GNU C++ compiler uses "-I" options to add to the include search path.
"#pragma" directives in source files are compiler specific. Any example on the Microsoft site is specific to their compiler but can give you some hints as to which libraries to link.
The linker needs to be know which libraries must be linked to satisfy calls you have made, e.g. wbemuuid.lib. The GNU ld linker uses option "-l" (lower case L) for this.
The linker needs to be able to find libraries to satisfy calls you have made. The GNU ld linker uses option "-L" for this. The default search path is built in. In MingW's case it will be limited to stuff inside MingW.
Spaces in paths need careful quoting.

These problems are generic compile/link problems and not Qt related.

The qmake system can help invoke the compiler correctly once you know the necessary include paths, library search path, and library names:

You can adjust the INCLUDEPATH in a Qt Pro file help the compiler find include files.
You can adjust the LIBS in a Qt Pro file help the linker find library files but you need to get the syntax right.

In Assistant, under "QMake Manual", look at the section headed "qmake Reference"

justatiq
15th March 2010, 13:14
Qt has to provide me with something I can configure like delphi does... as I mentioned in my previous post that I can easily use WMI after some settings
and I am not blaming Qt but Qt should provide this or tell me how ?

Yes I know it is compiler who looks for the files( I just said for the argument only).
I have used all the paths, absolute paths but the problem is not solved yet.
But I am trying and hopefully it would be solved.

wysota
15th March 2010, 14:31
Qt has to provide me with something I can configure like delphi does...
Qt is a library.

If you buy a car, the producer doesn't supply you with a parking lot, mechanic workshop, garage, streets or a camping compartments to hook up to your brand new car. You have to take care of such things yourself.


as I mentioned in my previous post that I can easily use WMI after some settings
and I am not blaming Qt but Qt should provide this or tell me how ?
Any tool you use won't magically determine where respective files are located on your system (and if they are there at all). If you are not up to the task of locating them yourself, maybe you should pay someone to do it for you.


Yes I know it is compiler who looks for the files( I just said for the argument only).
So why involve Qt in this? Leave Qt out for now, first be able to compile any C/C++ application that uses WMI and only then add Qt to the equation.

ChrisW67
16th March 2010, 00:47
Qt has to provide me with something I can configure like delphi does...
The bundled compiler you are using "can easily use WMI after some settings", it is just that the settings you crave are not some pretty, no-brainer check box but a few entries in a configuration file. Delphi's configuration is telling it to use the relevant COM object. C++ is not Deplhi; you will have to do a lot of what the Delphi IDE is hiding from you by yourself.


I have used all the paths, absolute paths but the problem is not solved yet. But I am trying and hopefully it would be solved.
Then there is still something wrong in your paths or the way you have tried to use them. You have not posted much about what you have actually tried so potential helpers can only guess.

Have you tried building a basic WMI example without Qt involved i.e. single file and generic gcc/g++ commands? When you succeed at this you will have gained an insight into how to roll WMI into a wider Qt-based project. Then you should use a simple Qt project to combine with WMI, just read something and print it, before moving on to a larger project.

Your 4 Feb 2010 post indicated that you had not quite grasped the LIBS variable in your pro file and you were given guidance that the syntax was wrong. Have you fixed that? Did you look at the suspect path I pointed out? What do the relevant parts of the pro file look like? Have the error messages from your build changed?

There ultimately should be bits something like:


INCLUDEPATH += "Q:\...\...\include"
LIBS += -L"Q:\...\Microsoft SDKs\Windows\...\lib" -lwbemuuid -l... -l...

All of these suggestions have already been made, but the specifics are left for you to fill in.

faldzip
16th March 2010, 09:23
@justatiq
Did you ever tried Qt Assistant? I guess you did not... It took me aprox 1min 30s to find answer to your question in Assistant. It is rather faster than 1.5month (it is time elapsed from thread start) so take those advices you got here and RTFM!

MaikoID
31st March 2011, 21:04
Hi I've this code and it's working fine on Qt 4.6.3 but if I use Qt 4.7 this function returns me nothing. What am I doing wrong ?



std::string DMI::wmiquery(std::string domain, std::string field)
{
std::string ret;

if (!objWMIService)
{
Log::RegistrarEvento("Leitura abortada: WMI não inicializado.");
return ret;
}

//Attempt to use some WMI functions...
QAxObject *returnList = objWMIService->querySubObject("ExecQuery(QString&)",
QString("select * from %1").arg(domain.c_str()));

QAxObject * result = returnList->querySubObject("ItemIndex(int)", 0);
result = result->querySubObject("Properties_()");

QAxObject *enum1 = result->querySubObject("_NewEnum");
IEnumVARIANT* enumInterface; //to get this, include <windows.h>
QUuid uuid("{00020404-0000-0000-C000-000000000046}");
enum1->queryInterface(uuid, (void**)&enumInterface);

QString text;

enumInterface->Reset(); //start at the beginning of the list.
for (int i=0;i<result->property("Count").toInt();i++)
{
VARIANT theItem;
unsigned long count;
enumInterface->Next(1,&theItem,&count);
if (!count)
break;
QAxObject *bla = new QAxObject((IUnknown *)theItem.punkVal);
// The problem is here.. the Property Name or any other property has empty value
if (field == QString(bla->property("Name").toString().toLatin1().data()).trimmed().toStdStr ing())
{
ret = QString(bla->property("Value").toString().toLatin1().data()).trimmed().toStdStr ing();
break;
}
}

return ret;
}


thx,

high_flyer
1st April 2011, 09:45
What am I doing wrong ?
1. Poting on the wrong thread (just bacuse your problem has to do with WMI doesn't mean any other thread that has to do with it it a proper place for it).
2. Do some research you self, and tell us the resutls for exmple:

I have stepped through the code in the debugger and saw that 'objWMIService' is null.
I then check my code where 'objWMIService' should be initialized, and it is.

OR


I have stepped through the code in the debugger and saw that result->property("Count") retuns 0,
but I have no idea where the property list is filled to check why it is empty...

Or any other stuff you actually tried to solve YOUR problem - before just laying it for us to do for you!

MaikoID
1st April 2011, 17:23
1. Poting on the wrong thread (just bacuse your problem has to do with WMI doesn't mean any other thread that has to do with it it a proper place for it).
2. Do some research you self, and tell us the resutls for exmple:

OR

Or any other stuff you actually tried to solve YOUR problem - before just laying it for us to do for you!

I don't think so. This thread is: How to use WMI, I shown what I did to solve it. I just forgot to mention this part:



#ifndef WIN32
#include <QProcess>
#endif
#include <ctype.h>
#include <algorithm>
#include <QMessageBox>
#include <QList>
#include <QByteArray>
#include <QVariant>
#ifdef WIN32
QAxObject *DMI::objIWbemLocator;
QAxObject *DMI::objWMIService;
#endif

DMI::DMI(void)
{
}

void DMI::Initialize(void)
{
#ifdef WIN32
CoInitialize(0);

//get the locator object
objIWbemLocator = new QAxObject("WbemScripting.SWbemLocator");

//get the main WMI Service
objWMIService = objIWbemLocator->querySubObject("ConnectServer(QString&,QString&)",QString("."),QString("root\\cimv2"));
#endif
}


That it's all what you need to do to communicate by WMI with Windows on Qt.

I don't give more details because this code worked in Qt 4.6.3 and stops to work in Qt 4.7.2 without any apparently error.

I said what I did, on commented code. All worked perfectly but the properties have no Value. I really don't know what I can do or which Qt 4.7.2 change affect the properly behavior of QAxObject.


QAxObject *bla = new QAxObject((IUnknown *)theItem.punkVal);

bye and sorry.

wysota
1st April 2011, 18:05
Make sure this is really caused by a change to 4.7 as it is very unlikely. Maybe something else has also changed in your system apart Qt.

high_flyer
1st April 2011, 18:08
I said what I did, on commented code.
Ok, now I see it (would be a good idea to state in the question that there are comments about your findings in the code).
I did react a bit hard since in recent times there a really a lot of people posting with out putting any thought of their own in to their own problems, expecting to be spoon fed - and since I read the forum between while I build, I read fast and often miss details - so I got a bit edgy.
Sorry about that.

Why do you use the following:


i if (field == QString(bla->property("Name").toString().toLatin1().data()).trimmed().toStdStr ing())
{
ret = QString(bla->property("Value").toString().toLatin1().data()).trimmed().toStdStr ing();
break;
}


and not


if (field == bla->property("Name").toString().trimmed().toStdString())
{
ret = bla->property("Value").toString().trimmed().toStdString();
break;
}

MaikoID
8th April 2011, 20:31
Ok, now I see it (would be a good idea to state in the question that there are comments about your findings in the code).
I did react a bit hard since in recent times there a really a lot of people posting with out putting any thought of their own in to their own problems, expecting to be spoon fed - and since I read the forum between while I build, I read fast and often miss details - so I got a bit edgy.
Sorry about that.

Why do you use the following:


i if (field == QString(bla->property("Name").toString().toLatin1().data()).trimmed().toStdStr ing())
{
ret = QString(bla->property("Value").toString().toLatin1().data()).trimmed().toStdStr ing();
break;
}


and not


if (field == bla->property("Name").toString().trimmed().toStdString())
{
ret = bla->property("Value").toString().trimmed().toStdString();
break;
}


Hi.. I've installed the Qt 4.6.2 to continue developing. Today I install the current version of Qt (4.7.0). If I replace my code for yours the problem persist, all bla's properties have null value.

high_flyer
10th April 2011, 16:44
If I replace my code for yours the problem persist, all bla's properties have null value.
My code was not supposed to change anything in the functionality, which is exactly the point!
I asked why you used your code the way you did (with (what seems to be) superfluous conversions)