.386
.model flat, stdcall
option casemap :none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
include AdvApi32.inc
includelib AdvApi32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
stSS SERVICE_STATUS <> ;服务的状态
hSS dd ? ;服务的状态句柄
dwOption dd ?
F_STOP equ 0001h ;停止服务 \
szBuffer dd MAX_PATH dup(0)
hSCM dd ?
service dd ?
lpsb dd ?
dwThreadID DD ?
hEvent dd ?
hServiceThread dd ?
.data
szServiceName db '服务测试五',0
szDisplayName db '服务测试五',0
szServiceFile db 'C:\WINDOWS\system32\后台服务.exe',0
szSystem db 'C:\WINDOWS\system32\',0
szFile db '\后台服务.exe',0
szCreateServiceError db 'CreateService is error',0
szStartServiceError db 'StartServiceError is error',0
szQueryServiceStatussError db 'QueryServiceStatuss is error',0
szOpenSCManagerError db 'OpenSCManager is error',0
;include 后台服务.Inc
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 服务控制程序
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcHandler proc _dwControl
pushad
mov eax,_dwControl
.if eax == SERVICE_CONTROL_STOP
mov stSS.dwCurrentState,SERVICE_STOPPED
invoke SetServiceStatus,hSS,addr stSS
.elseif eax == SERVICE_CONTROL_INTERROGATE
invoke SetServiceStatus,hSS,addr stSS
.endif
popad
ret
_ProcHandler endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 服务主程序
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ServiceProc proc uses ebx esi edi _lParam
.while
invoke MessageBeep,-1
invoke Sleep,1000
.endw
ret
ServiceProc endp
_ServiceMain proc _dwArgc,_lpszArgv
pushad
invoke RegisterServiceCtrlHandler,addr szServiceName,offset _ProcHandler
mov hSS,eax
mov stSS.dwServiceType,SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS
mov stSS.dwCurrentState,SERVICE_START_PENDING
mov stSS.dwControlsAccepted,SERVICE_ACCEPT_STOP
mov stSS.dwWin32ExitCode,NO_ERROR
invoke SetServiceStatus,hSS,addr stSS
;********************************************************************
; 如果初始化代码比较多,那么需要首先把状态设置为 pending,等完成以后
; 再设置为 Running。(在这里加入初始化代码)
;********************************************************************
mov stSS.dwCurrentState,SERVICE_RUNNING
invoke SetServiceStatus,hSS,addr stSS
;********************************************************************
;********************************************************************
invoke CreateEvent,0,TRUE,FALSE,0
mov hEvent,eax
invoke CreateThread,0,0,offset ServiceProc,0,0,addr dwThreadID
mov hServiceThread,eax
invoke WaitForSingleObject,hEvent,INFINITE
popad
ret
_ServiceMain endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 主程序
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_WinMain proc
local @stSTE[2]:SERVICE_TABLE_ENTRY
invoke RtlZeroMemory,addr @stSTE,sizeof @stSTE
mov @stSTE[0].lpServiceName,offset szServiceName
mov @stSTE[0].lpServiceProc,offset _ServiceMain
invoke StartServiceCtrlDispatcher,addr @stSTE
ret
_WinMain endp
_InstallService proc
LOCAL status:SERVICE_STATUS
;*****************************************************************************
;创建服务
;*****************************************************************************
invoke OpenSCManager,NULL,NULL,SC_MANAGER_CREATE_SERVICE
mov hSCM,eax
.if !eax
invoke MessageBox,NULL,addr szOpenSCManagerError,0,MB_OK
jmp _Ret
.endif
invoke CreateService,hSCM,addr szServiceName,addr szDisplayName,SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,\
SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,addr szServiceFile,0,0,0,0,0
mov service,eax
.if !eax
invoke MessageBox,NULL,addr szCreateServiceError,0,MB_OK
jmp _Ret
.endif
;*******************************************************************************
;创建成功后,启动服务
;*******************************************************************************
invoke QueryServiceStatus,service,addr status
.if !eax
invoke MessageBox,NULL,addr szQueryServiceStatussError,0,MB_OK
jmp _Ret
.endif
mov ebx,status.dwCurrentState
.if ebx==SERVICE_STOPPED
invoke StartService,service,NULL,NULL
.if !eax
invoke GetLastError
invoke lstrcat,addr szStartServiceError,eax
invoke MessageBox,NULL,addr szStartServiceError,0,MB_OK
jmp _Ret
.endif
.endif
invoke CloseServiceHandle,service
invoke CloseServiceHandle,hSCM
mov eax,1
_Ret:
ret
_InstallService endp
_Check proc ;检查当前是否系统目录
invoke GetCurrentDirectory,sizeof szBuffer,addr szBuffer
.if eax
;invoke MessageBox,0,addr szBuffer,0,MB_OK
lea esi,szBuffer
lea edi,szSystem
invoke lstrlen,addr szSystem
push eax
pop ecx
cld
repe cmpsb
.if !ecx
mov eax,1;是系统目录
.else
mov eax,0
.endif
.endif
ret
_Check endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
invoke _Check
.if eax ;是系统目录的话就启动服务程序
invoke _WinMain
.else
invoke lstrcat,addr szBuffer,addr szFile
invoke CopyFile,addr szBuffer,addr szServiceFile,FALSE ;不是的话创建服务,并启动服务,成功返回非0
invoke _InstallService
.if eax
invoke DeleteFile,addr szBuffer
.endif
.endif
invoke ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start
不多说了,仔细看代码
这是一个创建代码和服务都在一个程序里的例子
主要是根据当前目录是否为系统目录判断的
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/instruder/archive/2009/12/12/4992732.aspx