Page 1 of 1

Easy Creating and Controlling Services with Rings pbosl Lib

Posted: Fri Aug 12, 2005 10:07 pm
by PAMKKKKK
Code updated For 5.20+

Hi!
If you dont know:PBOSL means "PureBasic Open-Source Libraries"
you can get and involved on:
http://pbosl.purearea.net

Here is an Example to create Windows Services with ease....
Original by Rings :mrgreen:

Code: Select all

; 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 contol 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
IncludeFile "PBOSL_NTService.pb" ; download at http://pbosl.purearea.net/index.php?site=Libs ( NTService )

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 , "Purebasic Service Demo",  #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
    
    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
:!: Be careful the Example makes your Machine slow :!:

Posted: Fri Aug 12, 2005 10:37 pm
by HeX0R
This little example would be much helpfuler within the PBOSL pack.

Me too had a fight with this lib a few weeks ago and had wished to have a tiny example (though it is open source i have serious problems reading Rings' code... very unstructured at least for me ;) )

Hexor, you forgot a vital componnent..

Posted: Fri Aug 12, 2005 10:55 pm
by Fangbeast
The reason you can't read the code is that you didn't have enough beer. Rings wrote it with beer and when you have had the right amount, the code looks readable.

You'd better contact him about the correct beer to readability ratio.

Posted: Fri Aug 12, 2005 11:04 pm
by fweil
Better than dongles, license keys and other ...

A beer stamp protection ! Well I will get some packs for this WE and probably some good coding will occur then.

Re: Hexor, you forgot a vital componnent..

Posted: Fri Aug 12, 2005 11:34 pm
by HeX0R
Fangbeast wrote:The reason you can't read the code is that you didn't have enough beer. Rings wrote it with beer and when you have had the right amount, the code looks readable.

You'd better contact him about the correct beer to readability ratio.
Hehehe, well then i try to "finetune" the amount of drunken beer, but i have the strange feeling this will be a long night ;)

Posted: Sat Aug 13, 2005 9:10 am
by Rings
arh, just waked up after a long partynight.
maybe some time to write a kernel-driver or a complete OS ;)

Posted: Tue Aug 23, 2005 8:49 pm
by Beach
Just want to says thanks for this contribution to the PBOSL, I am finding many good uses for it.

Question, is there any way to add a start-in path for 'InstallService()'? I have an app that needs the actual path from where the app exists. As it is now, GetCurrentDirectory_() shows 'C:\Windows\System32'

Thanks!

Posted: Tue Aug 23, 2005 10:58 pm
by okasvi
does it require to be administrator when "installing" service? |: