WMI Programming with Visual Basic.NET: Managing the OS, continued - How the above program works
(Page 5 of 6 )
I already explained (in previous sections) up until the initialization of object ‘oItem’ with ‘ar(0)’. Proceeding further, we have a comment ‘Execute the method’. From that comment we are actually working with the service to change its state. Let me explain each statement separately.
DimoutParamsAsManagementBaseObject = oitem.InvokeMethod("StopService",Nothing,Nothing)
The above statement calls the method ‘StopService’ which exists in the ‘Win32_Service’ class of WMI (through ‘InvokeMethod’ routine) and returns the result into ‘outParams’ of type ‘ManagementBaseObject’. The first ‘Nothing’ informs that there exist no input parameters to the method ‘StopService’. The second ‘Nothing’ states that no other options (like time to execute, etc.) are necessary to execute it and considers all the defaults.
DimobjAsObject= outParams("ReturnValue")
The above statement retrieves the ‘ReturnValue’ of the operation. A zero results in success. A non zero results in failure.
Ifobj.ToString = "0"Then
MessageBox.Show("Succesfully stopped..")
lstServices_SelectedIndexChanged(Nothing,Nothing)
Else
MessageBox.Show("Could not Stop, Returned code:" & obj.ToString)
EndIf
This is very straightforward. It just displays the proper message based on the return value and finally updates the status using the following statement:
lstServices_SelectedIndexChanged(Nothing,Nothing)
The above statement gets the code fragment explained in the second section of this article, which actually fetches the latest status of the selected service.
Next: How will I know the proper description of the error? >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee