Windows NT Service

Just starting out? Need help? Post your questions and find answers here.
CoderLaureate
User
User
Posts: 50
Joined: Fri Apr 25, 2003 7:21 pm
Location: The World is my country, all mankind are my brethren, and to do good is my religion.
Contact:

Windows NT Service

Post by CoderLaureate »

Does anybody know how to go about creating a Windows NT Service with PB? I really DON'T want to use VB.Net if I don't have to.

-Jim
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

The search feature is your friend! Is this what you looked for?
viewtopic.php?t=5810
CoderLaureate
User
User
Posts: 50
Joined: Fri Apr 25, 2003 7:21 pm
Location: The World is my country, all mankind are my brethren, and to do good is my religion.
Contact:

Post by CoderLaureate »

No Fair!

I searched, and couldn't find anyting.
Maybe it was my wording...

:?
Skipsy
User
User
Posts: 98
Joined: Wed Apr 30, 2003 12:26 pm
Location: France

Post by Skipsy »

Hi,

There is a tool (I have never tested) that allow to convert any .EXE to
NT service.

http://www.prismmicrosys.com/atyourserv ... tm#CREATE1

Enjoy,
Beware of the man who has the solution before he understands the problem...
CoderLaureate
User
User
Posts: 50
Joined: Fri Apr 25, 2003 7:21 pm
Location: The World is my country, all mankind are my brethren, and to do good is my religion.
Contact:

Post by CoderLaureate »

Thanks for the pointer!
I'll check it out today.

-Jim
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

Does anybody know how to go about creating a Windows NT Service with PB? I really DON'T want to use VB.Net if I don't have to.
i'm on it in pure PureBasic , but please allow me to take a holidaybreak before releasing such stuff :)
SPAMINATOR NR.1
CoderLaureate
User
User
Posts: 50
Joined: Fri Apr 25, 2003 7:21 pm
Location: The World is my country, all mankind are my brethren, and to do good is my religion.
Contact:

Post by CoderLaureate »

I'm all for a holiday break.

I'll be on one all next week in Wisconsin, just sitting back, fishing, sleeping, sleeping some more, fishing..........

-Jim
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

Okay, all main stuff (Create NT Services from the scratch) is done and works as its should.Extended Fucntions to Install/Remove/start/Stop/Pause NT-service also.
I have to clean everything from unneeded stuff and worked out a lot of tests .so releasing it after holidays can be possible.
SPAMINATOR NR.1
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post by Berikco »

Rings wrote:Okay, all main stuff (Create NT Services from the scratch) is done and works as its should.Extended Fucntions to Install/Remove/start/Stop/Pause NT-service also.
I have to clean everything from unneeded stuff and worked out a lot of tests .so releasing it after holidays can be possible.
no time for holidays, first release it ;)
CoderLaureate
User
User
Posts: 50
Joined: Fri Apr 25, 2003 7:21 pm
Location: The World is my country, all mankind are my brethren, and to do good is my religion.
Contact:

Post by CoderLaureate »

Wow Rings!
You ROCK!
eikeland
New User
New User
Posts: 9
Joined: Fri Apr 25, 2003 5:35 pm
Location: USA

Post by eikeland »

Hi,
I posted this one for a while ago...here it's again:

Code: Select all

; Apr. 12, 2003 
; Converted to PB by Richard Eikeland 
; This code is posted as is with out any waranties. 
; 
#SERVICE_WIN32_OWN_PROCESS = $10 
#SERVICE_WIN32_SHARE_PROCESS = $20 
#SERVICE_WIN32 = #SERVICE_WIN32_OWN_PROCESS + #SERVICE_WIN32_SHARE_PROCESS 
#SERVICE_ACCEPT_STOP = $1 
#SERVICE_ACCEPT_PAUSE_CONTINUE = $2 
#SERVICE_ACCEPT_SHUTDOWN = $4 
#SC_MANAGER_CONNECT = $1 
#SC_MANAGER_CREATE_SERVICE = $2 
#SC_MANAGER_ENUMERATE_SERVICE = $4 
#SC_MANAGER_LOCK = $8 
#SC_MANAGER_QUERY_LOCK_STATUS = $10 
#SC_MANAGER_MODIFY_BOOT_CONFIG = $20 

#STANDARD_RIGHTS_REQUIRED = $F0000 
#SERVICE_QUERY_CONFIG = $1 
#SERVICE_CHANGE_CONFIG = $2 
#SERVICE_QUERY_STATUS = $4 
#SERVICE_ENUMERATE_DEPENDENTS = $8 
#SERVICE_START = $10 
#SERVICE_STOP = $20 
#SERVICE_PAUSE_CONTINUE = $40 
#SERVICE_INTERROGATE = $80 
#SERVICE_USER_DEFINED_CONTROL = $100 
#SERVICE_ALL_ACCESS = #STANDARD_RIGHTS_REQUIRED | #SERVICE_QUERY_CONFIG | #SERVICE_CHANGE_CONFIG | #SERVICE_QUERY_STATUS | #SERVICE_ENUMERATE_DEPENDENTS | #SERVICE_START | #SERVICE_STOP | #SERVICE_PAUSE_CONTINUE | #SERVICE_INTERROGATE |#SERVICE_USER_DEFINED_CONTROL 

#SERVICE_DEMAND_START = $3 
#SERVICE_ERROR_NORMAL = $1 

;- SERVICE_CONTROL 
#SERVICE_CONTROL_STOP = $1 
#SERVICE_CONTROL_PAUSE = $2 
#SERVICE_CONTROL_CONTINUE = $3 
#SERVICE_CONTROL_INTERROGATE = $4 
#SERVICE_CONTROL_SHUTDOWN = $5 


;-SERVICE_STATE 
#SERVICE_STOPPED = $1 
#SERVICE_START_PENDING = $2 
#SERVICE_STOP_PENDING = $3 
#SERVICE_RUNNING = $4 
#SERVICE_CONTINUE_PENDING = $5 
#SERVICE_PAUSE_PENDING = $6 
#SERVICE_PAUSED = $7 



Global ServiceStatus.SERVICE_STATUS, hServiceStatus.l, SERVICE_NAME.s, Finish.l 
Declare Handler(fdwControl.l) 
Declare ServiceMain(dwArgc.l, lpszArgv.l) 
Declare WriteLog(Value.s) 

Procedure Main() 

hSCManager.l 
hService.l 
ServiceTableEntry.SERVICE_TABLE_ENTRY 
b.l 
cmd.s 
;Change SERVICE_NAME and app name as needed 
AppPath.s = "C:\Dev\PureBasic\workdir\NTService\MyService.exe" 
SERVICE_NAME = "MyService" 

cmd = Trim(LCase(ProgramParameter())) 
Select cmd 
Case "install" ;Install service on machine 
hSCManager = OpenSCManager_(0, 0, #SC_MANAGER_CREATE_SERVICE) 
hService = CreateService_(hSCManager, SERVICE_NAME, SERVICE_NAME, #SERVICE_ALL_ACCESS, #SERVICE_WIN32_OWN_PROCESS, #SERVICE_DEMAND_START, #SERVICE_ERROR_NORMAL, AppPath, 0, 0, 0, 0, 0) 
CloseServiceHandle_(hService) 
CloseServiceHandle_(hSCManager) 
finish = 1 
Case "uninstall" ;Remove service from machine 
hSCManager = OpenSCManager_(0, 0, #SC_MANAGER_CREATE_SERVICE) 
hService = OpenService_(hSCManager, SERVICE_NAME, #SERVICE_ALL_ACCESS) 
DeleteService_(hService) 
CloseServiceHandle_(hService) 
CloseServiceHandle_(hSCManager) 
finish = 1 
Default 
*sname.s = SERVICE_NAME 
;Start the service 
ServiceTableEntry\lpServiceName = @SERVICE_NAME 
ServiceTableEntry\lpServiceProc = @ServiceMain() 
b = StartServiceCtrlDispatcher_(@ServiceTableEntry) 
WriteLog("Starting Service bResult=" + Str(b)) 
If b = 0 
finish = 1 
EndIf 
EndSelect 

Repeat 

Until Finish =1 

End 

EndProcedure 

Procedure Handler(fdwControl.l) 
b.l 

Select fdwControl 
Case #SERVICE_CONTROL_PAUSE 
;** Do whatever it takes To pause here. 
ServiceStatus\dwCurrentState = #SERVICE_PAUSED 
Case #SERVICE_CONTROL_CONTINUE 
;** Do whatever it takes To continue here. 
ServiceStatus\dwCurrentState = #SERVICE_RUNNING 
Case #SERVICE_CONTROL_STOP 
ServiceStatus\dwWin32ExitCode = 0 
ServiceStatus\dwCurrentState = #SERVICE_STOP_PENDING 
ServiceStatus\dwCheckPoint = 0 
ServiceStatus\dwWaitHint = 0 ;Might want a time estimate 
b = SetServiceStatus_(hServiceStatus, ServiceStatus) 
;** Do whatever it takes to stop here. 
Finish = 1 
ServiceStatus\dwCurrentState = #SERVICE_STOPPED 
Case #SERVICE_CONTROL_INTERROGATE 
;Fall through To send current status. 
Finish = 1 
;Else 
EndSelect 
;Send current status. 
b = SetServiceStatus_(hServiceStatus, ServiceStatus) 
EndProcedure 

Procedure ServiceMain(dwArgc.l, lpszArgv.l) 
b.l 
WriteLog("ServiceMain") 
;Set initial state 
ServiceStatus\dwServiceType = #SERVICE_WIN32_OWN_PROCESS 
ServiceStatus\dwCurrentState = #SERVICE_START_PENDING 
ServiceStatus\dwControlsAccepted = #SERVICE_ACCEPT_STOP | #SERVICE_ACCEPT_PAUSE_CONTINUE | #SERVICE_ACCEPT_SHUTDOWN 
ServiceStatus\dwWin32ExitCode = 0 
ServiceStatus\dwServiceSpecificExitCode = 0 
ServiceStatus\dwCheckPoint = 0 
ServiceStatus\dwWaitHint = 0 

hServiceStatus = RegisterServiceCtrlHandler_(SERVICE_NAME, @Handler()) 
ServiceStatus\dwCurrentState = #SERVICE_START_PENDING 
b = SetServiceStatus_(hServiceStatus, ServiceStatus) 

;** Do Initialization Here 

ServiceStatus\dwCurrentState = #SERVICE_RUNNING 
b = SetServiceStatus_(hServiceStatus, ServiceStatus) 

;** Perform tasks -- If none exit 

;** If an error occurs the following should be used for shutting 
;** down: 
; SetServerStatus SERVICE_STOP_PENDING 
; Clean up 
; SetServerStatus SERVICE_STOPPED 
EndProcedure 

Procedure WriteLog(Value.s) 
sfile.s = "MyServiceLog.txt" 
If OpenFile(0, sfile) 
WriteStringN(Value) 
CloseFile(0) 
Else 
If CreateFile(0,sfile) 

WriteStringN(Value) 
CloseFile(0) 
EndIf 
EndIf 

EndProcedure 

Main()
CoderLaureate
User
User
Posts: 50
Joined: Fri Apr 25, 2003 7:21 pm
Location: The World is my country, all mankind are my brethren, and to do good is my religion.
Contact:

Post by CoderLaureate »

Thanks Eikeland!
I'll go over your code this evening.

I take it, this is skeleton for a program that will run as a Windows NT service then? Very useful indeed.

Thanks again,

-Jim
Post Reply