📄 server.asm
字号:
.586
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\comctl32.inc
include \masm32\include\gdi32.inc
include \masm32\include\shell32.inc
include \masm32\include\advapi32.inc
include \masm32\include\ws2_32.inc
include \masm32\include\masm32.inc
include \masm32\include\winmm.inc
include \masm32\include\comdlg32.inc
include \masm32\include\shlwapi.inc
include \masm32\include\odbc32.inc
include LJYLIB.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\comctl32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\shell32.lib
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\ws2_32.lib
includelib \masm32\lib\winmm.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\comdlg32.lib
includelib \masm32\lib\shlwapi.lib
includelib \masm32\lib\odbc32.lib
includelib LJYLIB.lib
_WinMain PROTO
_ServiceMain PROTO :DWORD,:DWORD
_ProcHandler PROTO :DWORD
TimeToStr PROTO :DWORD,:DWORD
LC_Set_Server PROTO
LC_Create_Server PROTO :DWORD
LC_Start_Service PROTO :DWORD
LC_Delete_Server PROTO
LC_Stop_Server PROTO
CallBywgThreadProc PROTO :DWORD
SendStatusToSCM PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
ServerDialogProc PROTO :DWORD,:DWORD,:DWORD,:DWORD
ConfigDialogProc PROTO :DWORD,:DWORD,:DWORD,:DWORD
MemFill PROTO :DWORD,:DWORD
DLG_SERVER equ 100
DLG_CONFIG equ 101
IDI_TRAY equ 102
WM_SHELLNOTIFY equ WM_USER+100
STIF_DEFAULT equ 0
STIF_SUPPORT_HEX equ 1
szText MACRO Name, Text:VARARG
LOCAL lbl
jmp lbl
Name db Text,0
lbl:
ENDM
STEXT MACRO Text
local szText
LOCAL lbl
jmp lbl
szText db Text,0
lbl:
exitm <offset szText>
ENDM
m2m MACRO M1, M2
push M2
pop M1
ENDM
.data?
hInstance dd ?
hImageList dd ?
stSS SERVICE_STATUS <> ;服务的状态
hSS dd ? ;服务的状态句柄
hBywgThreadHand dd ?
hBywgThreadId dd ?
hTrayIcon dd ?
.data
wsad WSADATA <>
include WIN.ASM
include CHECK.ASM
include CALL.ASM
include MONITOR.ASM
include SQL.ASM
.code
start:
;invoke WSAStartup,101h,addr wsad
;invoke gethostbyname,STEXT('www.bywg.com')
;mov eax,[eax+12]
;mov eax,[eax]
;mov eax,[eax]
;invoke inet_ntoa,eax
;invoke MessageBox,0,eax,0,MB_OK
;invoke WSACleanup
invoke _WinMain
invoke ExitProcess,NULL
ret
_WinMain proc
LOCAL @stSTE[2]:SERVICE_TABLE_ENTRY
LOCAL TmpBuff[MAX_PATH]:BYTE
invoke GetCL,1,addr TmpBuff
invoke lstrcmpi,addr TmpBuff,STEXT('/DELETE')
.if eax == 0
invoke LC_Delete_Server
.else
invoke lstrcmpi,addr TmpBuff,STEXT('/STOP')
.if eax == 0
invoke LC_Stop_Server
.else
invoke LC_Set_Server
.if eax != 0
invoke RtlZeroMemory,addr @stSTE,sizeof @stSTE
mov @stSTE[0].lpServiceName,STEXT('BywgService')
mov @stSTE[0].lpServiceProc,offset _ServiceMain
invoke StartServiceCtrlDispatcher,addr @stSTE
.endif
.endif
.endif
ret
_WinMain endp
_ServiceMain proc _dwArgc,_lpszArgv
pushad
invoke RegisterServiceCtrlHandler,STEXT('BywgService'),offset _ProcHandler
mov hSS,eax
invoke SendStatusToSCM,SERVICE_START_PENDING,NO_ERROR,0,1,5000
invoke SendStatusToSCM,SERVICE_START_PENDING,NO_ERROR,0,2,1000
invoke SendStatusToSCM,SERVICE_START_PENDING,NO_ERROR,0,3,5000
invoke CreateThread,0,0,offset CallBywgThreadProc,0,0,addr hBywgThreadId
mov hBywgThreadHand,eax
invoke SendStatusToSCM,SERVICE_RUNNING,NO_ERROR,0,0,0
invoke WaitForInputIdle,hBywgThreadHand,INFINITE
invoke WaitForSingleObject,hBywgThreadHand,INFINITE
invoke CloseHandle,SERVICE_CONTROL_CONTINUE
invoke CloseHandle,hBywgThreadHand
invoke SendStatusToSCM,SERVICE_STOPPED,NO_ERROR,0,0,0
popad
ret
_ServiceMain endp
_ProcHandler proc _dwControl
pushad
mov eax,_dwControl
.if eax == SERVICE_CONTROL_STOP || eax == SERVICE_CONTROL_SHUTDOWN
invoke SendStatusToSCM,SERVICE_STOP_PENDING,NO_ERROR,0,1,5000
invoke SendMessage,hWnd,WM_CLOSE,0,0
;invoke TerminateThread,hBywgThreadHand,hBywgThreadId
.elseif eax == SERVICE_CONTROL_INTERROGATE
invoke SendStatusToSCM,SERVICE_RUNNING,NO_ERROR,0,0,0
.endif
popad
ret
_ProcHandler endp
SendStatusToSCM proc dwCurrentState:DWORD,dwWin32ExitCode:DWORD,dwServiceSpecificExitCode:DWORD,dwCheckPoint:DWORD,dwWaitHint:DWORD
LOCAL serviceStatus:SERVICE_STATUS
mov serviceStatus.dwServiceType,SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS
m2m serviceStatus.dwCurrentState,dwCurrentState
.if dwCurrentState == SERVICE_START_PENDING
mov serviceStatus.dwControlsAccepted,0
.else
mov serviceStatus.dwControlsAccepted,SERVICE_ACCEPT_STOP
.endif
.if dwServiceSpecificExitCode == 0
m2m serviceStatus.dwWin32ExitCode,dwWin32ExitCode
.else
mov serviceStatus.dwWin32ExitCode,ERROR_SERVICE_SPECIFIC_ERROR
.endif
m2m serviceStatus.dwServiceSpecificExitCode,dwServiceSpecificExitCode
m2m serviceStatus.dwCheckPoint,dwCheckPoint
m2m serviceStatus.dwWaitHint,dwWaitHint
invoke SetServiceStatus,hSS,addr serviceStatus
ret
SendStatusToSCM endp
LC_Set_Server proc
LOCAL hSCM:DWORD
LOCAL @hService:DWORD
LOCAL @stStatus:SERVICE_STATUS
LOCAL hRet:DWORD
mov hRet,0
invoke OpenSCManager,0,0,SC_MANAGER_ALL_ACCESS
.if eax != 0
mov hSCM,eax
invoke OpenService,hSCM,STEXT('BywgService'),SERVICE_ALL_ACCESS
.if eax == 0
invoke LC_Create_Server,hSCM
.else
mov @hService,eax
invoke QueryServiceStatus,@hService,addr @stStatus
.if @stStatus.dwCurrentState == SERVICE_STOPPED
invoke StartService,@hService,0,NULL
.elseif @stStatus.dwCurrentState == SERVICE_RUNNING
invoke ControlService,@hService,SERVICE_CONTROL_STOP,addr @stStatus
.repeat
invoke QueryServiceStatus,@hService,addr @stStatus
.until @stStatus.dwCurrentState == SERVICE_STOPPED
invoke StartService,@hService,0,NULL
xor eax,eax
.elseif @stStatus.dwCurrentState == SERVICE_START_PENDING
mov hRet,1
.endif
invoke CloseServiceHandle,@hService
.endif
invoke CloseServiceHandle,hSCM
.endif
mov eax,hRet
ret
LC_Set_Server endp
; 创建服务
LC_Create_Server proc hSCM:DWORD
LOCAL TmpBuff[MAX_PATH]:BYTE
invoke GetModuleFileName,0,addr TmpBuff,sizeof TmpBuff
invoke CreateService,hSCM,STEXT('BywgService'),STEXT('BY辅助工具系统服务'),SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_IGNORE,addr TmpBuff,0,0,0,0,0
.if eax != 0
invoke CloseServiceHandle,eax
invoke LC_Start_Service,hSCM
.endif
ret
LC_Create_Server endp
; 启动服务
LC_Start_Service proc hSCM:DWORD
LOCAL @hService:DWORD
invoke OpenService,hSCM,STEXT('BywgService'),SERVICE_START
.if eax != 0
mov @hService,eax
invoke StartService,@hService,0,NULL
.if eax != 0
mov eax,TRUE
.else
xor eax,eax
.endif
push eax
invoke CloseServiceHandle,@hService
pop eax
.else
xor eax,eax
.endif
ret
LC_Start_Service endp
; 删除服务
LC_Delete_Server proc
LOCAL @hService:DWORD,hSCM:DWORD
LOCAL @stStatus:SERVICE_STATUS
invoke OpenSCManager,0,0,SC_MANAGER_ALL_ACCESS
.if eax != 0
mov hSCM,eax
invoke OpenService,hSCM,STEXT('BywgService'),SERVICE_ALL_ACCESS
.if eax != 0
mov @hService,eax
invoke QueryServiceStatus,@hService,addr @stStatus
invoke ControlService,@hService,SERVICE_CONTROL_STOP,addr @stStatus
invoke DeleteService,@hService
invoke CloseServiceHandle,@hService
.endif
invoke CloseServiceHandle,hSCM
.endif
ret
LC_Delete_Server endp
; 停止服务
LC_Stop_Server proc
LOCAL @hService:DWORD,hSCM:DWORD
LOCAL @stStatus:SERVICE_STATUS
invoke OpenSCManager,0,0,SC_MANAGER_ALL_ACCESS
.if eax != 0
mov hSCM,eax
invoke OpenService,hSCM,STEXT('BywgService'),SERVICE_ALL_ACCESS
.if eax != 0
mov @hService,eax
invoke QueryServiceStatus,@hService,addr @stStatus
invoke ControlService,@hService,SERVICE_CONTROL_STOP,addr @stStatus
invoke CloseServiceHandle,@hService
.endif
invoke CloseServiceHandle,hSCM
.endif
ret
LC_Stop_Server endp
CallBywgThreadProc proc lParam:DWORD
LOCAL hwinstaCurrent:DWORD,hdeskCurrent:DWORD,hwinsta:DWORD,hdesk:DWORD
invoke GetProcessWindowStation
mov hwinstaCurrent,eax
invoke GetCurrentThreadId
invoke GetThreadDesktop,eax
mov hdeskCurrent,eax
invoke OpenWindowStation,STEXT('winsta0'),FALSE,WINSTA_ACCESSCLIPBOARD or WINSTA_ACCESSGLOBALATOMS or WINSTA_CREATEDESKTOP or WINSTA_ENUMDESKTOPS or WINSTA_ENUMERATE or WINSTA_EXITWINDOWS or WINSTA_READATTRIBUTES or WINSTA_READSCREEN or WINSTA_WRITEATTRIBUTES
mov hwinsta,eax
invoke SetProcessWindowStation,hwinsta
invoke OpenDesktop,STEXT('default'),0,FALSE,DESKTOP_CREATEMENU or DESKTOP_CREATEWINDOW or DESKTOP_ENUMERATE or DESKTOP_HOOKCONTROL or DESKTOP_JOURNALPLAYBACK or DESKTOP_JOURNALRECORD or DESKTOP_READOBJECTS or DESKTOP_SWITCHDESKTOP or DESKTOP_WRITEOBJECTS
mov hdesk,eax
invoke SetThreadDesktop,hdesk
invoke InitCommonControls
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,DLG_SERVER,NULL,offset ServerDialogProc,DLG_SERVER
invoke SetProcessWindowStation,hwinstaCurrent
invoke SetThreadDesktop,hdeskCurrent
invoke CloseWindowStation,hwinsta
invoke CloseDesktop,hdesk
ret
CallBywgThreadProc endp
CallIsStartSqlServer proc uses ebx
LOCAL @hService:DWORD,hSCM:DWORD
LOCAL @stStatus:SERVICE_STATUS
or ebx,-1
invoke OpenSCManager,0,0,SC_MANAGER_ALL_ACCESS
.if eax != 0
mov hSCM,eax
invoke OpenService,hSCM,STEXT('MSSQLSERVER'),SERVICE_ALL_ACCESS
.if eax != 0
mov @hService,eax
invoke QueryServiceStatus,@hService,addr @stStatus
.if @stStatus.dwCurrentState == SERVICE_RUNNING
xor ebx,ebx
.endif
invoke CloseServiceHandle,@hService
.endif
invoke CloseServiceHandle,hSCM
.endif
mov eax,ebx
ret
CallIsStartSqlServer endp
end start
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -