News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Writing an NT service

Started by white scorpion, April 16, 2005, 10:44:05 PM

Previous topic - Next topic

white scorpion

Hi all,

i'm having difficulties interacting with the SCManager while trying to write an NT service. Somehow i get the message "Service has started and stopped. This happens sometimes........"

here's the code, the service shouldn't do anything but put a MessageBox to the screen since it is just for testing, but that won't even work :(


.686
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\advapi32.inc
include \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\user32.lib


ServiceControlHandler   PROTO :DWORD
MyFunction              PROTO
ServiceMain             PROTO
.DATA
Message         db "It worked ;-))",0
AppName         db "Test",0


.DATA?
startinfo       STARTUPINFO         <>
secat           SECURITY_ATTRIBUTES <>
procinfo        PROCESS_INFORMATION <>
ServStat        SERVICE_STATUS      <>
ServTable       SERVICE_TABLE_ENTRY <>
hServStat       DWORD ?
stopServiceEvent DWORD ?


.CODE
start:
mov ServTable.lpServiceName,offset AppName
mov ServTable.lpServiceProc,offset ServiceMain

invoke StartServiceCtrlDispatcher,addr ServTable
.IF eax==NULL
    invoke GetLastError
    .IF eax==ERROR_FAILED_SERVICE_CONTROLLER_CONNECT
    invoke MyFunction
    .ENDIF
.ELSE
    invoke ServiceMain
.ENDIF
invoke ExitProcess,0

ServiceMain PROC

mov ServStat.dwServiceType,SERVICE_WIN32
;mov ServStat.dwCurrentState,SERVICE_STOPPED
mov ServStat.dwControlsAccepted,0
mov ServStat.dwWin32ExitCode,NO_ERROR
mov ServStat.dwServiceSpecificExitCode,NO_ERROR
mov ServStat.dwCheckPoint,0
mov ServStat.dwWaitHint,0

invoke RegisterServiceCtrlHandler,addr AppName,addr ServiceControlHandler
mov hServStat,eax
.IF eax!=NULL
       mov ServStat.dwCurrentState,SERVICE_START_PENDING
       invoke SetServiceStatus,hServStat,addr ServStat

       invoke CreateEvent,0,FALSE,FALSE,0
       mov stopServiceEvent,eax

       mov ServStat.dwControlsAccepted,SERVICE_ACCEPT_STOP+SERVICE_ACCEPT_SHUTDOWN
       mov ServStat.dwCurrentState,SERVICE_RUNNING
       invoke SetServiceStatus,hServStat,addr ServStat

       invoke MyFunction

       mov ServStat.dwCurrentState,SERVICE_STOP_PENDING
       invoke SetServiceStatus,hServStat,addr ServStat

       invoke CloseHandle,stopServiceEvent
       mov stopServiceEvent,0

       mov ServStat.dwControlsAccepted,SERVICE_ACCEPT_STOP+SERVICE_ACCEPT_SHUTDOWN
       mov ServStat.dwCurrentState,SERVICE_STOPPED
       invoke SetServiceStatus,hServStat,addr ServStat
.ENDIF
ret
   
ServiceMain ENDP

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;The procedure to handle the service controls
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ServiceControlHandler PROC controlcode:DWORD

.IF controlcode==SERVICE_CONTROL_INTERROGATE
    jmp next
.ELSEIF controlcode==SERVICE_CONTROL_SHUTDOWN || controlcode==SERVICE_CONTROL_STOP
    mov ServStat.dwCurrentState,SERVICE_STOP_PENDING
    invoke SetServiceStatus,addr hServStat,addr ServStat
    invoke SetEvent,addr stopServiceEvent
    ret
.ELSEIF controlcode==SERVICE_CONTROL_PAUSE
    jmp next
.ELSEIF controlcode==SERVICE_CONTROL_CONTINUE
    jmp next
.ENDIF
next:
invoke SetServiceStatus,hServStat,addr ServStat
ret

ServiceControlHandler ENDP               

;-----------------------------------------------------
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;My function
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MyFunction PROC

invoke MessageBoxA,NULL,addr Message,addr AppName,MB_OK
invoke Sleep,1000
ret
MyFunction ENDP
;-----------------------------------------------------
end start

I'm not sure what i am doing wrong, but apparently something is wrong  :green2

Thanks in advance!


white scorpion

Thanks, i really have to figure out how to search on these forums since most of the time it returns 0 :(

perhaps i'm not using the correct search queries....

Thanks for the model anyway, i'm sure it'll solve my problem ;)