WMI Programming with Visual Basic.NET: Managing the OS - How the above program works
(Page 5 of 6 )
I already explained the process, in previous sections, through 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 the ‘InvokeMethod’ routine) and returns the result into ‘outParams’ of type ‘ManagementBaseObject’. The first ‘Nothing’ informs us that there exist no input parameters to the method ‘StopService’. The second ‘Nothing’ states that no other options (such as time to execute) are necessary to execute it and consider all the defaults.
DimobjAsObject= outParams("ReturnValue")
The above statement retrieves the ‘ReturnValue’ of the operation. Zero results in success. 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 is just displaying the proper message based on the return value and, finally, updating 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