Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor


Windows Management Instrumentation (WMI) is a powerful interface to the operating system, you can find more information here. In this blog I describe how to use WMI inside ABAP.

Here a report how to use the WMI inside ABAP:
"-Begin-----------------------------------------------------------------
Program zUseWMI.

"-Variables---------------------------------------------------------
Data:
VBSCode Type String,
WMI Type OLE2_OBJECT,
CompName Type String,
WMICmd Type String,
oWBEM Type OLE2_OBJECT,
Model Type String,
Manufacturer Type String,
TotalPhyMem Type String
.

"-Macros------------------------------------------------------------
Define _.
Concatenate VBSCode &1 cl_abap_char_utilities=>cr_lf
Into VBSCode.
End-Of-Definition.

"-Main--------------------------------------------------------------
PerForm GetWMIObj Changing WMI.
If WMI-Handle <> 0 And WMI-Type = 'OLE2'.

"-Now you can use the Windows Management Instrumentation (WMI)--

PerForm GetComputerName Changing CompName.
Concatenate 'Win32_ComputerSystem=''' CompName ''''
Into WMICmd.
Call Method Of WMI 'Get' = oWBEM Exporting #1 = WMICmd.

Get Property Of oWBEM 'Model' = Model.
Get Property Of oWBEM 'Manufacturer' = Manufacturer.
Get Property Of oWBEM 'TotalPhysicalMemory' = TotalPhyMem.

Write: / Model.
Write: / Manufacturer.
Write: / TotalPhyMem.

"---------------------------------------------------------------

Free Object WMI.

EndIf.

"-End-------------------------------------------------------------------

"-SubRoutines begin-----------------------------------------------------

"-RunScript-----------------------------------------------------------
Form RunScript Using Cmd Type String Changing Result Type Any.

"-Variables-------------------------------------------------------
Data ScriptCtrl Type OBJ_RECORD.

Create Object ScriptCtrl 'MSScriptControl.ScriptControl'.
If sy-subrc = 0.
Set Property Of ScriptCtrl 'AllowUI' = 1.
Set Property Of ScriptCtrl 'Language' = 'VBScript'.
Call Method Of ScriptCtrl 'AddCode' Exporting #1 = VBSCode.
Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.
If sy-subrc = 0.
Call Method Of ScriptCtrl 'Eval' = Result Exporting #1 = Cmd.
Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.
EndIf.
EndIf.
Free Object ScriptCtrl.

EndForm.

"-GetWMIObj-----------------------------------------------------------
Form GetWMIObj Changing Result Type OBJ_RECORD.

Clear VBSCode.

_ 'Function GetWMIObj()'.
_ ' Dim oObj'.
_ ' Set oObj = GetObject("winmgmts:", "")'.
_ ' If IsObject(oObj) Then'.
_ ' Set GetWMIObj = oObj'.
_ ' End If'.
_ 'End Function'.

PerForm RunScript Using 'GetWMIObj()' Changing Result.

EndForm.

"-GetComputerName-----------------------------------------------------
Form GetComputerName Changing Result Type String.

Clear VBSCode.

_ 'Function GetComputerName()'.
_ ' Set oWSN = CreateObject("WScript.Network")'.
_ ' If IsObject(oWSN) Then'.
_ ' GetComputerName = oWSN.ComputerName'.
_ ' Set oWSN = Nothing'.
_ ' End If'.
_ 'End Function'.

PerForm RunScript Using 'GetComputerName()' Changing Result.

EndForm.

"-Subroutines end-------------------------------------------------------

The form routine GetWMIObj delivers the OLE object of the WMI and GetComputerName delivers the name of the computer. Both routines works with VBScript in the routine RunScript. The Main routine uses three methods of the Win32_ComputerSystem class: Model, Manufacturer and TotalPhysicalMemory and writes this Information on the screen.

The WMI offers thousands of classes, e.g. about BIOS, Boot configuration, printers, network, installed software etc. etc. etc.

 

Here another example to get the screen resolution:
"-Begin-----------------------------------------------------------------
REPORT ZGETSCREENRESOLUTION.

DATA:
ScriptCtrl TYPE OBJ_RECORD,
VBSCode TYPE String,
Result TYPE String
.

DEFINE _.
VBSCode = VBSCode && &1 && cl_abap_char_utilities=>cr_lf.
END-OF-DEFINITION.

_ 'Function GetScreenResolution()'.
_ ' Set oObj = GetObject("winmgmts:\\.\root\cimv2", "")'.
_ ' If IsObject(oObj) Then'.
_ ' Set colItems = oObj.ExecQuery("Select * from Win32_VideoController")'.
_ ' For Each oItem in colItems'.
_ ' Res = Res & oItem.CurrentHorizontalResolution & ":" & _'.
_ ' oItem.CurrentVerticalResolution & " / "'.
_ ' Next'.
_ ' GetScreenResolution = Res'.
_ ' End If'.
_ 'End Function'.

Create Object ScriptCtrl 'MSScriptControl.ScriptControl'.
Check sy-subrc = 0 And ScriptCtrl-Handle <> 0 And ScriptCtrl-Type = 'OLE2'.

"Set Property Of ScriptCtrl 'AllowUI' = 1.
Set Property Of ScriptCtrl 'Language' = 'VBScript'.
Call Method Of ScriptCtrl 'AddCode' Exporting #1 = VBSCode.
Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.
Call Method Of ScriptCtrl 'Eval' = Result Exporting #1 = 'GetScreenResolution()'.
Call Function 'AC_SYSTEM_FLUSH' Exceptions Others = 1.
Free Object ScriptCtrl.

Write: / Result.

"-End-------------------------------------------------------------------


 

To browse inside the WMI tree with its thousands of classes you can use the very good free WMI Explorer tool.



Labels in this area