1 de junio de 2014

Como saber si MySQL esta instalado en la PC

Autor: Roberto Reátegui Kanashiro

Rutinas para saber si MySQL esta instalado y corriendo, también para saber en que directorio esta instalado
#DEFINE SC_MANAGER_CONNECT          0x0001
#DEFINE GENERIC_READ                0x80000000
#DEFINE SERVICE_RUNNING             0x00000004
#DEFINE SERVICE_QUERY_CONFIG        0x0001
#DEFINE ERROR_INSUFFICIENT_BUFFER   0x7A

Clear
? "MySQL está Instaldo:", IIf(isServiceInstalled("MySQL"), "SI", "NO")
? "MySQL está Corriendo:", IIf(isServiceRunning("MySQL"), "SI", "NO")
? "Donde se encuentra:", GetServicePath("MySQL")

Function isServiceInstalled(tcServiceName)
Local llResult, lnManager, lnService
  llResult = .F.
  Declare Integer OpenSCManager In Advapi32.dll Integer, Integer, Integer
  Declare Integer OpenService In Advapi32.dll Integer, String, Integer
  Declare Integer CloseServiceHandle In Advapi32.dll Integer
  lnManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT)
  If lnManager <> 0
     lnService = OpenService(lnManager, tcServiceName, GENERIC_READ)
     If lnService <> 0
        CloseServiceHandle(lnService)
        llResult = .T.
     EndIf
     CloseServiceHandle(lnManager)
  EndIf
  Clear Dlls "OpenSCManager", "OpenService", "CloseServiceHandle"
  Return llResult
EndFunc

Function isServiceRunning(tcServiceName)
Local llResult, lnManager, lnService, lcBuffer
   Declare Integer OpenSCManager In Advapi32.dll Integer, Integer, Integer
   Declare Integer OpenService In Advapi32.dll Integer, String, Integer
   Declare Integer CloseServiceHandle In Advapi32.dll Integer
   Declare Integer QueryServiceStatus In Advapi32.dll Integer, String
   llResult = .F.
   lnManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT)
   If lnManager <> 0
      lnService = OpenService(lnManager, tcServiceName, GENERIC_READ)
      If lnService <> 0 then
         lcBuffer = Space(1024)
         If QueryServiceStatus(lnService, @lcBuffer) <> 0
            llResult = CToBin(Substr(lcBuffer, 5, 4), "4RS") = SERVICE_RUNNING
         EndIf
         CloseServiceHandle(lnService)
     EndIf
     CloseServiceHandle(lnManager)
   EndIf
   Clear Dlls "OpenSCManager", "OpenService", "QueryServiceStatus", "CloseServiceHandle"
   Return llResult
EndFunc

Function GetServicePath(tcServiceName)
Local lcResult, lnManager, lnService, lcBuffer, lnSize
   Declare Integer OpenSCManager In Advapi32.dll Integer, Integer, Integer
   Declare Integer OpenService In Advapi32.dll Integer, String, Integer
   Declare Integer CloseServiceHandle In Advapi32.dll Integer
   Declare Integer QueryServiceConfig In Advapi32.dll Integer, String@, Integer, Integer@
   Declare Integer GetLastError In Kernel32.dll
   lcResult = ""
   lnManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT)
   If lnManager <> 0
      lnService = OpenService(lnManager, tcServiceName, SERVICE_QUERY_CONFIG)
      If lnService <> 0 then
         lcBuffer = "" 
         lnSize = 0
         If QueryServiceConfig(lnService, @lcBuffer, 0, @lnSize) = 0 And GetLastError() = ERROR_INSUFFICIENT_BUFFER
            lcBuffer = Space(lnSize)
            If QueryServiceConfig(lnService, @lcBuffer, lnSize, 2) = 1
               lcResult = JustPath(Substr(lcBuffer, 37, AtC("--defaults", lcBuffer) - 37))
            EndIf
         EndIf
         CloseServiceHandle(lnService)
     EndIf
     CloseServiceHandle(lnManager)
   EndIf
   Clear Dlls "OpenSCManager", "OpenService", "QueryServiceConfig", "CloseServiceHandle"
   Return lcResult
EndFunc

1 comentario :

  1. Muchas gracias, es lo que estaba buscando, y te agradezco el código

    ResponderEliminar