FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Controlar un servicio de windows...
Posts: 632
Joined: Thu Jan 19, 2006 10:45 AM
Controlar un servicio de windows...
Posted: Mon May 02, 2011 06:23 AM
Lo dicho, lo que quiero hacer es saber si un servicio de windows esta arrancado o arrancarlo simplemente. Con el nuevo servipack de windows XP el servicio mensajero está desabilitado. Este servicio controla los mensajes mediante el comando net send. Si me voy enciama de Mi PC y con el boton derecho voy a administrar ahí veo como estan los servicios. El de mensajero cuando actualizas el nuevo servipack está desabilitado y desde allí lo puedes poner en automático y luego iniciar. Me preguntaba si esto lo podemos hacer para que cuando arranques el programa mire si está arrancado y en caso contrario arrancarlo.

He implementado un control de accesos a los programas y sabiendo el nombre del pc que ha entrado en un programa puedo mandarle mensajes en forma de popups que le salgan en pantalla con el comando NET SEND:
Code (fw): Select all Collapse
WinExec("CMD /C START /separate NET SEND " + ORDENADOR + " Sal del programa de instalaciones"  ,0)

Por defecto al instalar el service pack este comando no funciona por no estar arrancado el servicio mensajero. Cómo lo hago?

PD.: Sé que lo puedo hacer uno a uno de forma manual en todos los ordenadores, pero necesito hacerlo por programación.
Saludos



Andrés González desde Mallorca
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Controlar un servicio de windows...
Posted: Mon May 02, 2011 07:29 AM
I am not clear whether this is what you are looking for.

The following code will help you to Start or Stop a windows service

Code (fw): Select all Collapse
#Include "FiveWin.ch"
//-----------------------//
Function Main()

    Local oLoc,oWmi,oServiceList,oService

    oLoc:= CreateObject( "wbemScripting.SwbemLocator" )
    oWmi:= oLoc:ConnectServer()
   
    If oWmi == NIL
        ? "Sorry!! No Wmi"
        Return
    Endif
   
    // As an eg. here I am checking whether MySQL service is running or not
    oServiceList := oWmi:ExecQuery("Select * from Win32_Service where Name='MySQL'") 
   
    FOR EACH oService IN oServiceList
        MsgInfo(oService:State)   // Returns the string Running/Stopped 
        If oService:State == "Stopped"
            oErr:=oService:StartService()        
        Elseif oService:State == "Running"
            oService:StopService()
        Endif
    Next
  
Return NIL


Regards
Anser
Posts: 632
Joined: Thu Jan 19, 2006 10:45 AM
Re: Controlar un servicio de windows...
Posted: Mon May 02, 2011 07:39 AM

Correct Anser, this is what I'm looking for. Thanks.

Saludos



Andrés González desde Mallorca
Posts: 632
Joined: Thu Jan 19, 2006 10:45 AM
Re: Controlar un servicio de windows...
Posted: Mon May 02, 2011 10:09 AM
Anser, could you test if you can star and stop de service called 'Messenger'. In spanish is 'Mensajero' but the code never show to the msginfo.
Code (fw): Select all Collapse
// As an eg. here I am checking whether MySQL service is running or not
    oServiceList := oWmi:ExecQuery("Select * from Win32_Service where Name='Messenger'") 
   
    FOR EACH oService IN oServiceList
        MsgInfo(oService:State)   // Returns the string Running/Stopped 
        If oService:State == "Stopped"
            oErr:=oService:StartService() 
            MSgInfo("Servicio iniciado")       
        Elseif oService:State == "Running"
            oService:StopService()
            MsgInfo("Servicio Finalizado")
        Endif
    Next


PD.: Messenger is nothing to be with Messenger Live, is just to send pupups in a domain network
Saludos



Andrés González desde Mallorca
Posts: 632
Joined: Thu Jan 19, 2006 10:45 AM
Re: Controlar un servicio de windows...
Posted: Mon May 02, 2011 10:34 AM

Sorry Anser, it needs a delay. Working thanks.

Saludos



Andrés González desde Mallorca
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Controlar un servicio de windows...
Posted: Mon May 02, 2011 04:39 PM

Yes, you need to include delay/sleep to work properly. Sorry, I forgot to include the sleep command in the sample code.
Nice to hear that it worked for you. :D

Regards
Anser

Continue the discussion