sábado, 3 de enero de 2009

Raiders of the Lost Services

Several days ago, a good friend of mine, placed a question in a newsgroup about getting access to the Windows Services installed in his computer. His question was simple: He just wanted to know if certain service, installed by himself, were running or not, and in case it wasn't running, he wanted to start it, but using his own source code without the need to run the SERVICES program in the Control Panel.

He found some answers, but no really "THE" answer.

Then I remembered that other friend of mine, Rafa Carmona (a.k.a. TheFull), had posted a very interesting sample in the Xailer's news group about how to access to the Windows Services using the WMI (Windows Management Instrumentation), which is, simplifying, a Windows API to handle different components of the Operating System using OLE.

Reviewing the Xailer's sample, I noticed how easy is to gain access to a bunch of the Operating System features, and even manipulate them, like the services themselves.

Let's see a simple example, you may wish to download a more complex sample later on, keep reading.

To connect to WMI you have to create an OLE object first, and in xHarbour you do it like this:

oWMI := CreateObject ("wbemScripting.SwbemLocator")


This just creates the object, now we have to "connect" to it:

oWMICon := oWMI:ConnectServer()

WMI is a HUGE interface, there are several interesting objects in the system, a full reference and documentatio can be found in the MSDN (Microsoft Developer's Network), I'll save you from wasting time looking into MSDN for the docs, simply click here and you will be redirected to the WMI docs in the MSDN.

The next step is to select the service we want, in our case, we want the "handling services" service, so we get all the services installed in our computer with a simply SQL query, something like this:

oServices := oWMICon:ExecQuery("Select * from Win32_Service")


That easy !, this query will return in oServices an array with "service objects", every object returned has its own methods and data that you can handle to get the results you want.

Let's play with them For example to get a list of the current installed services and their current state, we can do something like this:

For each objService in oServicios

? objService:DisplayName +": "+objService:State

Next

You can also start, stop, resume and pause services using the :StartService(), :StopService(), :ResumeService() or :PauseService() methods, and of course you can create your own services and delete current ones with :CreateService() and :DeleteService(), the complete reference to methos and classes is available clicking here.

Here is a more interesting sample: Let's suppose we want to start the Advantage Database Server service if it's stopped, then we can do something like this:

oWMI := CreateObject ("wbemScripting.SwbemLocator")
oWMICon := oWMI:ConnectServer()

oADSServ := oWMICon:ExecQuery("Select * from Win32_Service where DisplayName = 'Advantage Database Server'")

IF oADSServ:State == "Stopped"

oADSServ:StartService()

ENDIF


Rafa has written a very interesting sample in Xailer, simply click aquí to get it. If you don't have or don't want to install Xailer to take a look at it, don't worry I have included also the EXE file, the Xailer proyect and of course, the source code in PRG files is included in the ZIP file.

Thanks Rafa for such a great sample !!!!

And Thanks to Mel Smith for helping with the right translation !!!

No hay comentarios:

Publicar un comentario