Easy Creating and Controlling Services with Rings pbosl Lib

Share your advanced PureBasic knowledge/code with the community.
PAMKKKKK
User
User
Posts: 18
Joined: Sun May 01, 2005 7:55 am
Location: Germany Braunschweig
Contact:

Easy Creating and Controlling Services with Rings pbosl Lib

Post 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 :!:
Last edited by PAMKKKKK on Sat Aug 13, 2005 10:20 am, edited 2 times in total.
Develop Standards, and you develop the World!
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Post 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 ;) )
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Hexor, you forgot a vital componnent..

Post 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.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post 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.
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Hexor, you forgot a vital componnent..

Post 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 ;)
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

arh, just waked up after a long partynight.
maybe some time to write a kernel-driver or a complete OS ;)
SPAMINATOR NR.1
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post 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!
-Beach
okasvi
Enthusiast
Enthusiast
Posts: 150
Joined: Wed Apr 27, 2005 9:41 pm
Location: Finland

Post by okasvi »

does it require to be administrator when "installing" service? |:
Post Reply