flat assembler
Message board for the users of flat assembler.

Index > Windows > Windows Service Sample/Template

Author
Thread Post new topic Reply to topic
badc0de02



Joined: 25 Nov 2013
Posts: 216
Location: %x
badc0de02
run by arguments:
1 = install the service
2 = run the service

Code:
; By badc0de copy as you want/edit
format PE GUI 4.0
include 'win32ax.inc'
entry main

section 'main' code readable writeable executable

start:    invoke OpenSCManagerA,0,0,983103 ;Starting the service
          mov [svcManH],eax
          cmp eax,0
          je _err
          invoke OpenServiceA,eax,name,983551
          mov [svcSv],eax
          cmp eax,0
          je _err
          invoke Sleep,3000
          invoke StartService,dword [svcSv],0,0
          cmp eax,0
          je _err
          invoke CloseServiceHandle,dword [svcManH]
          invoke CloseServiceHandle,dword [svcSv]
          jmp errno

install:  invoke GetModuleFileNameA,0,currPath,260 ;install the service
          invoke OpenSCManagerA,0,0,983103
          mov [svcManH],eax
          cmp eax,0
          je _err
          invoke CreateServiceA,eax,name,name,983551,16,3,1,currPath,0,0,0,0,0
          mov [svcSv],eax
          cmp eax,0
          je _err
          invoke CloseServiceHandle,dword [svcManH]
          invoke CloseServiceHandle,dword [svcSv]
          jmp errno

main:     invoke GetCommandLineA
          mov esi,eax
          call arg

          cmp byte [esi+2],'1' ;install
          je install
          cmp byte [esi+2],'2' ;start
          je start

          invoke StartServiceCtrlDispatcherA,table
          cmp eax,0
          jne errno
_err:     int 3

errno:    invoke ExitProcess,1

arg:      cmp byte [esi],' '
          je ok
          cmp byte [esi],0
          je ok
          inc esi
          jmp arg
ok:       ret

svc_main: invoke RegisterServiceCtrlHandlerA,name,svc_handler ;main routine
          mov [svcStatH],eax
          invoke SetServiceStatus,eax,svcStat
          mov [svcStat.dwWaitHint],0
          mov [svcStat.dwCurrentState],04h
          invoke SetServiceStatus,dword [svcStatH],svcStat
servicing:; Do your Service stuff
          jmp servicing

svc_handler: ret

section 'data' data readable writeable

table:    dd name
          dd svc_main
          dd 0,0

name:     db 'asm_svc',0
currPath: times 260 db 0
svcManH:  dd 0
svcSv:    dd 0
svcStatH: dd 0

svcStat:
   .dwServiceType             dd 010h
   .dwCurrentState            dd 02h
   .dwControlsAccepted        dd 0
   .dwWin32ExitCode           dd 0
   .dwServiceSpecificExitCode dd 0
   .dwCheckPoint              dd 0
   .dwWaitHint                dd 3000

section 'library' import data readable writeable
        library advapi32,'ADVAPI32.DLL',kernel32,'kernel32.dll'
        include 'API\ADVAPI32.INC'
        include 'API\KERNEL32.INC'    


Last edited by badc0de02 on 31 Jan 2015, 22:16; edited 2 times in total
Post 28 Jan 2015, 17:58
View user's profile Send private message Reply with quote
badc0de02



Joined: 25 Nov 2013
Posts: 216
Location: %x
badc0de02
fixed somethig ...
Post 29 Jan 2015, 11:13
View user's profile Send private message Reply with quote
eisbaer



Joined: 25 Nov 2012
Posts: 8
eisbaer
looks interesting!

the code seems to be for windows 32 bit
can you please explain, how to modify this code for windows 64 bit ?

for me especially interesting would be for example a service,
which can react on pressing of a function keys

F1 = give back to keyboard-driver the code for INSERT
F2 = give back to keyboard-driver the code for SHIFT-F4
F7 = give back to keyboard-driver the code for FN-F7
F9 = give back to keyboard-driver the code for ALT-F4
F10 = give back to keyboard-driver the code for CTRL-ALT-DEL

would such things possible
or
what can i i do for example whithin such an installable service-program?
Post 31 Jan 2015, 18:22
View user's profile Send private message Reply with quote
badc0de02



Joined: 25 Nov 2013
Posts: 216
Location: %x
badc0de02
hm i think this can be done without a Service.
use the functions: RegisterHotKey and kbd_event,GetMessage


Last edited by badc0de02 on 31 Jan 2015, 22:13; edited 1 time in total
Post 31 Jan 2015, 20:28
View user's profile Send private message Reply with quote
badc0de02



Joined: 25 Nov 2013
Posts: 216
Location: %x
badc0de02
64bit format:
Code:
; By badc0de copy as you want/edit   64bit
format PE64 GUI 4.0
include 'win64ax.inc'
entry main

section 'main' code readable writeable executable

start:    invoke OpenSCManagerA,0,0,983103 ;Starting the service
          mov [svcManH],rax
          cmp rax,0
          je _err
          invoke OpenServiceA,rax,name,983551
          mov [svcSv],rax
          cmp rax,0
          je _err
          invoke Sleep,3000
          invoke StartService,dword [svcSv],0,0
          cmp rax,0
          je _err
          invoke CloseServiceHandle,dword [svcManH]
          invoke CloseServiceHandle,dword [svcSv]
          jmp errno

install:  invoke GetModuleFileNameA,0,currPath,260 ;install the service
          invoke OpenSCManagerA,0,0,983103
          mov [svcManH],rax
          cmp rax,0
          je _err
          invoke CreateServiceA,eax,name,name,983551,16,3,1,currPath,0,0,0,0,0
          mov [svcSv],rax
          cmp rax,0
          je _err
          invoke CloseServiceHandle,dword [svcManH]
          invoke CloseServiceHandle,dword [svcSv]
          jmp errno

main:     invoke GetCommandLineA
          mov rsi,rax
          call arg

          cmp byte [rsi+2],'1' ;install
          je install
          cmp byte [rsi+2],'2' ;start
          je start

          invoke StartServiceCtrlDispatcherA,table
          cmp rax,0
          jne errno
_err:     int 3

errno:    invoke ExitProcess,1

arg:      cmp byte [rsi],' '
          je ok
          cmp byte [rsi],0
          je ok
          inc rsi
          jmp arg
ok:       ret

svc_main: invoke RegisterServiceCtrlHandlerA,name,svc_handler ;main routine
          mov [svcStatH],rax
          invoke SetServiceStatus,rax,svcStat
          mov [svcStat.dwWaitHint],0
          mov [svcStat.dwCurrentState],04h
          invoke SetServiceStatus,dword [svcStatH],svcStat
servicing:; Do your Service stuff
          jmp servicing

svc_handler: ret

section 'data' data readable writeable

table:    dq name
          dq svc_main
          dq 0,0

name:     db 'asm_svc',0
currPath: times 260 db 0

svcManH:  dq 0
svcSv:    dq 0
svcStatH: dq 0

svcStat:
   .dwServiceType             dd 010h
   .dwCurrentState            dd 02h
   .dwControlsAccepted        dd 0
   .dwWin32ExitCode           dd 0
   .dwServiceSpecificExitCode dd 0
   .dwCheckPoint              dd 0
   .dwWaitHint                dd 3000

section 'library' import data readable writeable
        library advapi32,'ADVAPI32.DLL',kernel32,'kernel32.dll'
        include 'API\ADVAPI32.INC'
        include 'API\KERNEL32.INC'    
Post 31 Jan 2015, 20:41
View user's profile Send private message Reply with quote
badc0de02



Joined: 25 Nov 2013
Posts: 216
Location: %x
badc0de02
Code:
; By badc0de copy as you want/edit

format PE GUI 4.0
include 'win32a.inc'
entry main

SRV_NAME equ 'notlol'

section 'main' code readable writeable executable

start:    invoke OpenSCManagerA,0,0,983103 ;Starting the service
          mov [svcManH],eax
          cmp eax,0
          je return
          invoke OpenServiceA,eax,name,983551
          mov [svcSv],eax
          cmp eax,0
          je return

          invoke StartService,dword [svcSv],0,0
          cmp eax,0
          je return
          invoke CloseServiceHandle,dword [svcManH]
          invoke CloseServiceHandle,dword [svcSv]
          jmp errno

install:  invoke GetModuleFileNameA,0,currPath,260 ;install the service
          invoke OpenSCManagerA,0,0,983103
          mov [svcManH],eax
          cmp eax,0
          je return
          invoke CreateServiceA,eax,name,name,983551,0x00000110,3,1,currPath,0,0,0,0,0
          mov [svcSv],eax
          cmp eax,0
          je return
          invoke CloseServiceHandle,dword [svcManH]
          invoke CloseServiceHandle,dword [svcSv]
          jmp errno

main:     invoke GetCommandLineA
          mov esi,eax
          call arg

          invoke StartServiceCtrlDispatcherA,table
          invoke MessageBoxA,0,err_msg,err_ttl,MB_ICONHAND
errno:    invoke ExitProcess,1

arg:      cmp word [esi],'-1'
          je install
          cmp word [esi],'-2'
          je start
          cmp byte [esi],0
          je  return
          inc esi
          jmp arg
return:   ret

svc_main: invoke RegisterServiceCtrlHandlerA,name,svc_handler ;main routine
          mov [svcStatH],eax
          invoke SetServiceStatus,dword [svcStatH],svcStat

servicing:; Do your Service stuff
          jmp servicing

svc_handler: ret

section 'data' data readable writeable

table:    dd name
          dd svc_main
          dd 0,0

name:     db SRV_NAME,0
currPath: times 260 db 0

svcManH:  dd 0
svcSv:    dd 0
svcStatH: dd 0

svcStat:
   .dwServiceType             dd 0110h
   .dwCurrentState            dd 04h
   .dwControlsAccepted        dd 0
   .dwWin32ExitCode           dd 0
   .dwServiceSpecificExitCode dd 0
   .dwCheckPoint              dd 0
   .dwWaitHint                dd 0
err_ttl: db 'Error Fail!!',0
err_msg: db 'Failed Operation with the service: ',SRV_NAME,0

section 'library' import data readable writeable
        library advapi32,'ADVAPI32.DLL',kernel32,'kernel32.dll',user32,'user32.dll'
        include 'API\ADVAPI32.INC'
        include 'API\KERNEL32.INC'
        include 'API\USER32.INC'    


ASM Service Sample UPDATE incoming
Post 18 Feb 2015, 22:08
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.