Working with WMI (windows manager instrumentation)

Published on by Yousfi Benameur

 
this firts code lists all Shortcuts existing on the Computer using Win32_ShortcutFile class from :
https://msdn.microsoft.com/en-us/library/aa394438%28v=vs.85%29.aspx
its a school prototype  example showing how to use WMI to gather on a cursor all the class parameters.
The Win32_ShortcutFile WMI class represents files that are shortcuts to other files, directories,
 and commands.
In my computer there is 828 shortcuts throw all directories.

Warning: this code takes many minutes to accomplish its task.....be patient!

all the shortcut information are gathered in the memo field( mouseover to see the tooltip) or dblclick
to show the memo contents.
the code uses the browse command in oop window as object and apply some cosmetics.


Click on code to select [then copy] -click outside to deselect

 
    *1*
*this code lists all Shortcuts existing on the Computer using Win32_ShortcutFile class
*https://msdn.microsoft.com/en-us/library/aa394438%28v=vs.85%29.aspx
*The Win32_ShortcutFile WMI class represents files that are shortcuts to other files, directories,
* and commands.
*In my computer there is 828 shortcuts throw all directories.

*Warning: this code takes many minutes to accomplish its task.....be patient!

Clea All
Close Data All

Local m.x,m.lcdest,m.cr,m.y,m.xtime
m.xtime=datetime()

strComputer = "."
objWMIService = Getobject("winmgmts:" ;
    + "{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
colItems = objWMIService.ExecQuery("Select * from Win32_ShortcutFile")
m.y=""
m.x=""
Create Cursor ycurs(num i,yshortcut c(125),Info m)
cr=Chr(13)+Chr(10)
i=0
_Screen.MousePointer=11
For Each objItem In colItems
	m.y=""
	m.y=m.y+  objItem.EightDotThreeFileName+cr
	i=i+1

	m.x=""
	m.x=m.x+"Eight Dot Three File Name:"+m.y+cr
	m.x=m.x+ "File Name: " + objItem.FileName+cr
	strCreationDate = yconvert(Trans(objItem.CreationDate)+cr)
	m.x=m.x+ "Creation Date: " +strCreationDate+cr
	m.x=m.x+ "Drive: " + objItem.Drive+cr
	m.x=m.x+ "Extension: " + objItem.Extension+cr
	m.x=m.x+ "File Size: " + objItem.FileSize+cr
	m.x=m.x+ "File Type: " + objItem.FileType+cr
	m.x=m.x+ "File System Name: " + objItem.FSName+cr
	m.x=m.x+ "Hidden: " + Trans(objItem.Hidden)+cr
	strLastAccessed = yconvert(Trans(objItem.LastAccessed)+cr)
	m.x=m.x+ "Last Accessed: " + strLastAccessed+cr
	strLastModified =yconvert(Trans(objItem.LastModified))+cr
	m.x=m.x+ "Last Modified: " + strLastModified+cr
	m.x=m.x+ "Name: " + objItem.Name+cr
	m.x=m.x+ "Path: " + objItem.Path+cr
	m.x=m.x+ "Target: " + objItem.Target+cr+cr
	Insert Into ycurs Values(i,m.y,m.x)
Next
_Screen.MousePointer=0
m.xtime=datetime()-m.xtime


Sele ycurs
Locate
Browse  Name ybrow   Title Trans(Reccount())+" shortcuts on system )"  Nowait    &&window as oop object
With ybrow
	.DeleteMark=.F.
	.GridLines=0
	.FontBold=.T.
	.Themes=.F.
	.RecordMark=.F.
	.SetAll("forecolor",255,"header")
	.SetAll("backcolor",Rgb(0,255,0),"header")
	.Columns(1).DynamicBackColor="RGB(128,128,128)"
	.Columns(2).DynamicBackColor="RGB(100,140,240)"
	.HeaderHeight=24
	For i=1 To .ColumnCount
		.Columns(i).header1.FontSize=14
		If i=3
			.Columns(i).MousePointer=15
		Endi
	Endfor
Endwith
messagebox("Calculation time="+trans(xtime)+" seconds.",0+32+4096,'')

Function yconvert(x)  &&UTC time to readable string
m.x=Allt(m.x)
m.x="year="+Substr(m.x,1,4)+" month="+Substr(m.x,5,2)+" Day="+Substr(m.x,7,2)+" hour= "+Substr(m.x,9,2)+" mn= "+Substr(m.x,11,2)+  " sec="+ Substr(m.x,13,2)
Return m.x
Endfunc





can copy the cursor to a physical table.

can copy the cursor to a physical table.

 
this follow code gets (from the class network  internet) parameters whose are updated from vfp  with a timer dynamically.
*!*--Time stamp lundi 21 juin 2010; 14:46:56
*!*--devices...'Win32_PerfRawData_Tcpip_NetworkInterface '
*!*-- https://msdn.microsoft.com/en-us/library/aa394340%28v=vs.85%29.aspx
*!*--The Win32_PerfRawData_Tcpip_NetworkInterface raw performance data class provides raw data from
*!*--performance counters that monitor the rates at which bytes and packets are sent and received over
*!*--a TCP/IP network connection. 


Click on code to select [then copy] -click outside to deselect


*2*
Sys(2002)  &&set curs off
Publi yform
yform=Createobject("asup")
yform.Show
Read Events
Return

Define Class asup As Form
    Height = 505
	Width = 433
	ShowWindow=2
	AutoCenter = .T.
	Caption = "WMI Network Interface"
	Name = "form1"

	Add Object timer1 As Timer With ;
		Top = 12, ;
		Left = 396, ;
		Height = 23, ;
		Width = 23, ;
		Interval = 2000, ;
		Name = "Timer1"

	Add Object edit1 As EditBox With ;
		FontSize = 8, ;
		Anchor = 15, ;
		BackStyle = 0, ;
		Height = 504, ;
		Left = 0, ;
		fontbold=.T.,;
		ReadOnly = .T., ;
		Top = 0, ;
		Width = 433, ;
		ForeColor = Rgb(0,0,255), ;
		Name = "Edit1"

	Procedure timer1.Timer
	If Lastkey()=27
		This.Enabled=.F.
		Thisform.Release
	Endi

	strComputer = "."
	objWMIService = Getobject("winmgmts:\\" + strComputer + "\root\cimv2")
	colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_Tcpip_NetworkInterface",,48)
	Local m.x,m.cr
	m.x=""
	m.cr=Chr(13)
	For Each objItem In colItems  &&all PC devices concerned with network
		m.x=m.x+Repli("-",60)+cr
		m.x=m.x+objItem.Name+cr
		m.x=m.x+Repli("-",60)+cr+cr

		m.x=m.x+( "1- BytesReceivedPersec: " +Trans( objItem.BytesReceivedPersec) ) + cr
		m.x=m.x+( "2- BytesSentPersec: " +Trans( objItem.BytesSentPersec)) + cr
		m.x=m.x+( "3- BytesTotalPersec: " +Trans( objItem.BytesTotalPersec)) + cr
		m.x=m.x+( "4- Caption: " +Trans( objItem.Caption)) + cr
		m.x=m.x+( "5- CurrentBandwidth: " +Trans( objItem.CurrentBandwidth)) + cr
		m.x=m.x+( "6- Description: " +Trans( objItem.Description)) + cr
		m.x=m.x+( "7-Frequency_Object: " +Trans( objItem.Frequency_Object)) + cr
		m.x=m.x+( "8-Frequency_PerfTime: " +Trans( objItem.Frequency_PerfTime)) + cr
		m.x=m.x+( "9-Frequency_Sys100NS: " +Trans( objItem.Frequency_Sys100NS)) + cr
		m.x=m.x+( "10- Name: " +Trans( objItem.Name)) + cr
		m.x=m.x+( "11- OutputQueueLength: " +Trans( objItem.OutputQueueLength)) + cr
		m.x=m.x+( "12- PacketsOutboundDiscarded: " +Trans( objItem.PacketsOutboundDiscarded)) + cr
		m.x=m.x+( "13- PacketsOutboundErrors: " +Trans( objItem.PacketsOutboundErrors)) + cr
		m.x=m.x+( "14- PacketsPersec: " +Trans( objItem.PacketsPersec)) + cr
		m.x=m.x+( "15- PacketsReceivedDiscarded: " +Trans( objItem.PacketsReceivedDiscarded)) + cr
		m.x=m.x+( "16- PacketsReceivedErrors: " +Trans( objItem.PacketsReceivedErrors)) + cr
		m.x=m.x+( "17- PacketsReceivedNonUnicastPersec: " +Trans( objItem.PacketsReceivedNonUnicastPersec)) + cr
		m.x=m.x+( "18- PacketsReceivedPersec: " +Trans( objItem.PacketsReceivedPersec)) + cr
		m.x=m.x+( "19- PacketsReceivedUnicastPersec: " +Trans( objItem.PacketsReceivedUnicastPersec)) + cr
		m.x=m.x+( "20- PacketsReceivedUnknown: " +Trans( objItem.PacketsReceivedUnknown)) + cr
		m.x=m.x+( "21- PacketsSentNonUnicastPersec: " +Trans( objItem.PacketsSentNonUnicastPersec)) + cr
		m.x=m.x+( "22- PacketsSentPersec: " +Trans( objItem.PacketsSentPersec)) + cr
		m.x=m.x+( "23- PacketsSentUnicastPersec: " +Trans( objItem.PacketsSentUnicastPersec)) + cr
		m.x=m.x+( "24- Timestamp_Object: " +Trans( objItem.Timestamp_Object)) + cr
		m.x=m.x+( "25- Timestamp_PerfTime: " +Trans( objItem.Timestamp_PerfTime)) + cr
		m.x=m.x+( "26- Timestamp_Sys100NS: " +Trans( objItem.Timestamp_Sys100NS)) + cr+cr

	Next
	Thisform.edit1.Value=""
	Thisform.edit1.Value=x
	Endproc

	Procedure Destroy
	Set Curs On
	Clea Events
	Endproc

Enddefine
*





Working with  WMI (windows manager instrumentation)

Click on code to select [then copy] -click outside to deselect


*3*
 *this code lists all windows services on a computer
*Win32_Service :https://msdn.microsoft.com/en-us/library/aa394418%28v=vs.85%29.aspx
*can add other parameters returned by the class if desired...

Create Cursor ycurs (num i,service c(40),Status c(8),Description c(150))
Clea
lcComputer = "."
loWMIService = Getobject("winmgmts:" ;
    + "{impersonationLevel=impersonate}!\\" + lcComputer + "\root\cimv2")
colServices = loWMIService.ExecQuery ("Select * from Win32_Service")
i=1
For Each loService In colServices
	Insert Into ycurs Values (i,loService.Name,loService.Status,Nvl(loService.Description,""))
	i=i+1
Next
Sele ycurs
xtitle=Trans(Reccount())+" windows services list and status ------ ("+Trans(Reccount())+" services)"
*browse title xtitle  font "courrier"

Sele ycurs
Locate
Browse  Name ybrow   Title m.xtitle  Nomodify Nowait    &&window as oop object
With ybrow
	.DeleteMark=.F.
	.GridLines=0
	.FontBold=.T.
	.Themes=.F.
	.RecordMark=.F.
	.SetAll("forecolor",255,"header")
	.SetAll("backcolor",Rgb(0,255,0),"header")
	.Columns(1).DynamicBackColor="RGB(128,128,128)"
	.Columns(2).DynamicBackColor="RGB(100,140,240)"
	.HeaderHeight=24
	For i=1 To .ColumnCount
		.Columns(i).header1.FontSize=14
		If i=3
			.Columns(i).MousePointer=15
		Endi
	Endfor
Endwith



Working with  WMI (windows manager instrumentation)

Click on code to select [then copy] -click outside to deselect


*4*
*The Win32_DesktopMonitor WMI class represents the type of monitor or display device attached to the
*computer system. Starting with Windows Vista, properties of this class replace Win32_DisplayConfiguration.
*Hardware that is not compatible with Windows Display Driver Model (WDDM) returns inaccurate property
* values for instances of this class.
*https://msdn.microsoft.com/en-us/library/aa394122%28v=vs.85%29.aspx

lcComputer = "."
loWMIService = Getobject("winmgmts:\\" + lcComputer + "\root\cimv2")
colItems = loWMIService.ExecQuery("Select * from Win32_DesktopMonitor")
Local m.x,m.cr
m.cr=Chr(13)
m.x=""
For Each loItem In colItems
    m.x=m.x+"Availability: " + Transform(loItem.Availability)+cr
	m.x=m.x+"Bandwidth: " + Trans(loItem.Bandwidth)+cr
	m.x=m.x+"Description: " + loItem.Description+cr
	m.x=m.x+"Device ID: " + Trans(loItem.DeviceID)+cr
	m.x=m.x+"Display Type: " + loItem.DisplayType+cr
	m.x=m.x+"Is Locked: " + Trans(loItem.IsLocked)+cr
	m.x=m.x+"Monitor Manufacturer: " + loItem.MonitorManufacturer+cr
	m.x=m.x+"Monitor Type: " + loItem.MonitorType+cr
	m.x=m.x+"Name: " + loItem.Name+cr
	m.x=m.x+"Pixels Per X Logical Inch: " + Transform(loItem.PixelsPerXLogicalInch)+cr
	m.x=m.x+"Pixels Per Y Logical Inch: " + Transform(loItem.PixelsPerYLogicalInch)+cr
	m.x=m.x+"PNP Device ID: " + Trans(loItem.PNPDeviceID)+cr
	m.x=m.x+"Screen Height: " + Transform(loItem.ScreenHeight)+cr
	m.x=m.x+"Screen Width: " + Transform(loItem.ScreenWidth)+cr
Next
_Cliptext=m.x
Messagebox(m.x,0+32+4096,"Desktop monitor(result in clipboard).")



Working with  WMI (windows manager instrumentation)
 
*Important:*the code above is tested on visual foxpro 9 sp2-under windows 10 pro

Published on Visual foxpro, WMI, shortcut

To be informed of the latest articles, subscribe:
Comment on this post