PBOSL Beispiel von NT Service / NT dienste

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
PAMKKKKK
Beiträge: 321
Registriert: 21.04.2005 22:08
Wohnort: Braunschweig
Kontaktdaten:

PBOSL Beispiel von NT Service / NT dienste

Beitrag von PAMKKKKK »

Hallo!
Rings hat ja ne geile PBOSL Lib geschrieben. :allright:
Hier mal ein (hoffentlich) verständliches Beispiel um einen eigenen Dienst (Service) zu schreiben:

Code: Alles auswählen

; For this code you need PBOSL
; If you dont know:PBOSL means "PureBasic Open-Source Libraries"
; you can get and involved on:
; http://pbosl.purearea.net
; Make a *.exe from this Code (e.g. = "Service.exe")
; Install and start the Service with calling your *.exe (e.g. = "Service.exe /install")
; now the Service ist Installed an runnnig,
; you can control it by the Windows Servicesmanager (stop resume .....)
; to deinstall it call e.g. = ("Service.exe /remove")
; Original by Rings Edited by PAMKKKKK
; PB 3.93 13.August 2005

Global ServiceDisplayName.s ; Global for use with the MyServiveFunction()

; BEGINNING MAIN RANGE FOR YOUR CHANGES >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

ServiceDisplayName = "A Service by Rings" ;The displayed name of the Service, in the SCM and sent To functions query its display name

Procedure MyServiveFunction()
  ; This Procedure Runs in the background as a Windows Service
  ; it shows a Nag-Window ca. all  800Milliseconds (nag.. nag... ;)
  Count = 800
  If OpenWindow(0, 0, 0, 282, 92,  #PB_Window_ScreenCentered | #PB_Window_SystemMenu , "Purebasic Service Demo")
    If CreateGadgetList(WindowID())
      TextGadget(0, 10, 10, 260, 30, "Service : " + Chr(34) +  ServiceDisplayName  + Chr(34) +  " ist started", #PB_Text_Center)
      TextGadget(1, 70, 50, 130, 30, "Close Window in :", #PB_Text_Center)
    EndIf
    
    Repeat
      EventID.l = WindowEvent()
      If EventID = #PB_Event_CloseWindow
        CloseWindow(0)
      EndIf
      Delay(1)
      Count -1
      If Count > 300
        If IsWindow(0)
          SetGadgetText(1,"Close Window in :" + Str(Count) + " ticks")
        EndIf
      EndIf
      If Count = 300
        CloseWindow(0)
      EndIf
      If Count = 1
        MyServiveFunction()
        ProcedureReturn
      EndIf
    ForEver
  EndIf
EndProcedure
; ENDING MAIN RANGE FOR YOUR CHANGES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


Procedure NTNotify(Parameter)
  ; This Procedure Handels the Messages that are incomming from the Windows ServiceManager 
  Select  Parameter
    Case 2
      MessageRequester("Info","Service is paused",0) ; <<<<<<<<<<<<< CHANGE THIS
    Case 3
      MessageRequester("Info","Service is resumed",0); <<<<<<<<<<<<< CHANGE THIS
  EndSelect
EndProcedure

;- Beginn the Main things....

; Set start Parameter >>>>

; get myself Path to call myself as a Service
PathToServiceEXE.s=Space(256)
GetModuleFileName_(GetModuleHandle_(0), @PathToServiceEXE, 256)
ServiceExplanation.s = "This is Service is a Nag-Window  from the PureBasic Service Demo by Mr. Rings"
ServiceName.s = "MyService" ; Only a Internal Name ; <<<<<<<<<<<<< CHANGE THIS
command.s=LCase(ProgramParameter())

If command = "/?" Or command = "/h" Or command = "help" Or command = "/help"
  command = "/help"
EndIf

; Selecting start Commands >>>>

Select command
  Case "/install"
    ; start types
    #SERVICE_BOOT_START     = $00000000
    #SERVICE_SYSTEM_START   = $00000001
    #SERVICE_AUTO_START     = $00000002
    #SERVICE_DEMAND_START   = $00000003
    #SERVICE_DISABLED       = $00000004
    ; Create the Service an run....
    Installservice(ServiceName,ServiceDisplayName,PathToServiceEXE,ServiceExplanation,#SERVICE_AUTO_START)
    End
  Case "/remove"
    RemoveService(ServiceName)
    End
  Case "/help"
    MessageRequester("Info","Start Service with " + Chr(34) +   PathToServiceEXE +" /install" + Chr(34) +  Chr(10) +"stop it with" + Chr(34) +   PathToServiceEXE + " /remove" + Chr(34),0)
    End
EndSelect

; Without Installservice() the Service command dos´nt run !
;NTService(ServiceName.s,@MyFunction())
Service(ServiceName.s,@MyServiveFunction(),@NTNotify()) ;the Notify is optional
Achtung der Code Lahmt den Rechner etwas! :mrgreen:
Wir Schreiben ein PureBasic Buch.
Auch du kannst mitmachen!
http://www.purearea.net/pb/english/pure ... :Main_Page