Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2650
  • Last Modified:

Delphi WMI Example

First time WMI user here, go easy.

I need to access the Win32_PerfFormattedData_PerfDisk_PhysicalDisk "database"(?) to get real time info on disk I/O events.

  Locator := CoSWbemLocator.Create;
  Service := Locator.ConnectServer('','root\cimv2','','','','',0,nil);
  Output := Service.ExecQuery('SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk','WQL',wbemFlagReturnImmediately,nil);
  Enum := Output._NewEnum as IEnumVariant;

I get about this far before I go *^*&*(#@* what now. I've seen examples in every language except delphi and I'm not sure exactly how to traverse the results here. Any help?
0
ThievingSix
Asked:
ThievingSix
  • 13
  • 9
1 Solution
 
JohnjcesCommented:
Why not try posting or linking us to whatever code you found that gave the entire result set. Someone who knows that language, say C++ or BASIC, might be able to assist.

John
0
 
CodedKCommented:
Hi ThievingSix,
nice to see you. Well this is not so difficult for Delphi.
(Which version of Delphi by the way?)

Go to Project -> Import Type Library and Install "Microsoft WMI Scripting" ... Which version you have depends on the Delphi version. Doesn't matter though.
Delphi 2007 1.2 , Delphi 7 1.1 etc
Now you will be able to see the Type library in the list if it is installed on your computer.
Don't worry if you want this for Windows 2k and up.
For Windows 98 you have to download it from Microsoft and install it in the system.

I've prepared a small example.
Change jpg to rar
65.jpg
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ThievingSixAuthor Commented:
I get hit with // Trap any exceptions (Not having WMI installed will cause one!)

Being Windows 7 I would assume this is installed. Any ideas?
0
 
CodedKCommented:
Did you find and add the "Microsoft WMI Scripting 1.x" to your project?
This is strange...

There are free components for this job too :
http://www.sourcecodeonline.com/details/magenta_systems_wmi_and_smart_component.html

Maybe you need to download the WMI core from Microsoft. Go to http://www.microsoft.com/downloads/search.asp? 
and do a Keyword search for "WMI"..

http://www.microsoft.com/downloads/details.aspx?FamilyId=98A4C5BA-337B-4E92-8C18-A63847760EA5&displaylang=en



0
 
ThievingSixAuthor Commented:
Yes I did it was 1.2. And it seems that those downloads either won't run or say it's already isntalled.
0
 
CodedKCommented:
This normally works ThievingSix, i don't know what to say. Everything is in place...
0
 
CodedKCommented:
Remove try.. except and see where is the problem.
0
 
ThievingSixAuthor Commented:
Looks like it dies on on the ExecQuery with an Invalid Query error.

If I remove wbemFlagForwardOnly then Enum.Next doesn't return S_OK.
0
 
CodedKCommented:
What version of Delphi do you have ?
0
 
ThievingSixAuthor Commented:
Delphi 2009. And yes the ExecQuery had WideString declaration so that shouldn't be an issue.
0
 
CodedKCommented:
Ok... Try this...Maybe this will do the job

Go to Start/Run/CMD and then run each of these hitting enter after each:

First :
winmgmt /clearadap winmgmt /kill winmgmt /unregserver winmgmt /regserver winmgmt /resyncperf
If this doesn't work :
1. Right-click My Computer.
2. Select the Manage item.
3. Double-click Services and Applications in the left-hand pane.
4. Scroll to Windows Management Instrumentation in the right-hand pane and
right-click it.
5. Press Stop.
6. Use Windows Explorer to delete all the files in the %SystemRoot%\System32\Wbem\Repository folder.
8. Shutdown and restart your computer. The Windows Management
Instrumentation service will start and the files will be re-created.]]

Hope this will do the job :/

0
 
CodedKCommented:
From Microsoft :
WMI Diagnosis Utility
If WMI returns error messages, be aware that they may not indicate problems in the WMI service or in WMI providers. Failures can originate in other parts of the operating system and emerge as errors through WMI. Under any circumstances, do not delete the WMI repository as a first action because deleting the repository can cause damage to the system or to installed applications.
To obtain more information about the source of the problem, you can download and run the WMI Diagnosis Utility diagnostic command line tool. This tool produces a report that can usually isolate the source of the problem and provide instructions on how to fix it. The report also aids Microsoft support services in assisting you. You can download the WMI Diagnosis Utility at the Download Center.

So don't do delete it yet... Try their tool first
0
 
ThievingSixAuthor Commented:
Their tool won't run on Windows 7 unfortunately. It seems something is wrong with the ExecQuery syntax because I am still getting an

WBEM_E_INVALID_QUERY - 0x80041017 - The query was not syntactically valid.

Error.
0
 
CodedKCommented:
It works here (Vista) and in my other PC (XP)

The query is correct, the function is used by everyone (the most common).
Can you try this in XP ? Just to see what happens...
0
 
ThievingSixAuthor Commented:
In Windows XP Mode(Virtual PC) the application works fine. So it's got to be something with my PC. Are there any diagnostics, other than the one you mentioned, for this. I get an "OS Unsupported" error with it.
0
 
CodedKCommented:
No this is the only one mentioned (and the only official) by Microsoft.
The other way around is to try to fix the error like i said in my previous post.

They say not to delete the files in case there is no value in the registry that can revert the changes or create new ones.
But i don't think this is the case. I think it will recreate your files and make them good as new.
Further more you could safely try to stop and restart the service :
Like this ->
winmgmt /clearadap
winmgmt /kill
winmgmt /unregserver
winmgmt /regserver
winmgmt /resyncperf


Now there is a small chance that they changed something in WMI. I heard something about Windows Powershell. I dont know more about how this too may conflict or if there are some changed fields in Win7 stucture.
0
 
CodedKCommented:
Just to solve my curiosity do this ...
Download WMI Code Creator from here :
http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en

See if this works.
0
 
ThievingSixAuthor Commented:
Unfortunately something seems like they have changed. The winmgmt no longer has those parameter inputs. Also Win32_PerfFormattedData_PerfDisk_PhysicalDisk is not listed in the classes in the WMI Code Creator.
0
 
CodedKCommented:
Maybe it lies in the version of your Win7. Maybe in ultimate don't even know the name... is the same and in basic edition removed some stuff just to piss us off.
Anyway the Delphi code is correct though.
0
 
CodedKCommented:
Thanks for the points ThievingSix, any luck with Win7 ?
0
 
ThievingSixAuthor Commented:
Not yet, but I noticed that it has to do with something on my system alone as the same version of Win7 has the code work fine. If I don't figure it out I'll probably post a question over in the Windows section.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 13
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now