9
9908006
Unregistered / Unconfirmed
GUEST, unregistred user!
近日学习delphi sdk编程,在弄一个服务程序时发现很难给服务添加描述,在网上弄了个服务程序的源码,改了下后能注册成功,但服务描述那一栏老是空的,查了些资料后被告之可改注册表项,也可使用 ChangeServiceConfig2这个api函数,但是这个函数我使用时老是失败,请各位帮帮忙,解答下,源程序贴在下面,请直接在上面改,也可以说下用法,先答对者给80分,补充者也有分,谢了先!<br><br>program SS;<br><br>uses<br> Windows,<br> WinSvc,<br> winsock;<br><br>var<br> szServiceName: pchar = 'ss';<br> szFileNamechar;<br> ServiceTable: array [0..1] of TServiceTableEntry;<br> Status: SERVICE_STATUS;<br> StatusHandle: SERVICE_STATUS_HANDLE;<br> Stopped: boolean;<br> Paused: boolean;<br><br>//获取系统目录<br>function GetDirectory(dInt: Integer): string;<br>var<br> s: array[0..255] of Char;<br>begin<br> case dInt of<br> 0: GetWindowsDirectory(@s, 256); //Windows安装文件夾所存在的路径<br> 1: GetSystemDirectory(@s, 256); //系统文件夾所存在的路径<br> 2: GetTempPath(256,@s); //Temp文件夾所存在的路径<br> 3: GetCurrentDirectory(256,@s);<br> end;<br> if dInt=2 then<br> result :=string(s)<br> else<br> result := string(s) + '/';<br>end;<br><br>function InstallService(ServiceName, DisplayName, FileName: string): boolean;<br>var<br> SCManager,Service: THandle;<br> Args: pchar;<br>begin<br> Result := False;<br> SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);<br> if SCManager = 0 then Exit;<br> try<br> Service := CreateService(SCManager, //句柄<br> PChar(ServiceName), //服务名称<br> PChar(DisplayName), //显示服务名<br> SERVICE_ALL_ACCESS, //服务访问类型<br> SERVICE_WIN32_OWN_PROCESS, //服务类型 or SERVICE_INTERACTIVE_PROCESS<br> SERVICE_AUTO_START, //自动启动服务<br> SERVICE_ERROR_IGNORE, //忽略错误<br> PChar(FileName), //启动的文件名<br> nil, //name of load ordering group (载入组名) 'LocalSystem'<br> nil, //标签标识符<br> nil, //相关性数组名<br> nil, //帐户(当前)<br> nil); //密码(当前)<br><br> Args := nil;<br> StartService(Service, 0, Args);<br> CloseServiceHandle(Service);<br> finally<br> CloseServiceHandle(SCManager);<br> end;<br> Result := True;<br>end;<br><br>procedure UninstallService(ServiceName: string);<br>var<br> SCManager,Service: THandle;<br> ServiceStatus: SERVICE_STATUS;<br>begin<br> SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);<br> if SCManager = 0 then Exit;<br> try<br> Service := OpenService(SCManager, PChar(ServiceName), SERVICE_ALL_ACCESS);<br> ControlService(Service, SERVICE_CONTROL_STOP, ServiceStatus);<br> DeleteService(Service);<br> CloseServiceHandle(Service);<br> finally<br> CloseServiceHandle(SCManager);<br> end;<br>end;<br><br>procedure ServiceCtrlHandler(Control: dword); stdcall;<br>begin<br> case Control of<br> SERVICE_CONTROL_STOP:<br> begin<br> Stopped := True;<br> Status.dwCurrentState := SERVICE_STOPPED;<br> end;<br> SERVICE_CONTROL_PAUSE:<br> begin<br> Paused := True;<br> Status.dwcurrentstate := SERVICE_PAUSED;<br> end;<br> SERVICE_CONTROL_CONTINUE:<br> begin<br> Paused := False;<br> Status.dwCurrentState := SERVICE_RUNNING;<br> end;<br> SERVICE_CONTROL_INTERROGATE: ;<br> SERVICE_CONTROL_SHUTDOWN: Stopped := True;<br> end;<br> SetServiceStatus(StatusHandle, Status);<br>end;<br><br>procedure ServiceMain;<br>begin<br> repeat<br> if not Paused then<br> begin<br> Sleep(2000);<br> end;<br> until Stopped;<br> ExitProcess(0);<br>end;<br><br>procedure ServiceCtrlDispatcher(dwArgc: dword; var lpszArgv: pchar); stdcall;<br>begin<br> StatusHandle := RegisterServiceCtrlHandler(szServiceName, @ServiceCtrlHandler);<br> if StatusHandle <> 0 then<br> begin<br> ZeroMemory(@Status, SizeOf(Status));<br> Status.dwServiceType := SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS;<br> Status.dwCurrentState:= SERVICE_START_PENDING;<br> Status.dwControlsAccepted := SERVICE_ACCEPT_STOP or SERVICE_ACCEPT_PAUSE_CONTINUE;<br> Status.dwWaitHint := 1000;<br> SetServiceStatus(StatusHandle, Status);<br> Stopped := False;<br> Paused := False;<br> Status.dwCurrentState := SERVICE_RUNNING;<br> SetServiceStatus(StatusHandle, Status);<br> ServiceMain;<br> end;<br>end;<br><br>procedure Main;<br>var cBuf:array[0..MAX_PATH]of Char;<br>begin<br> GetModuleFileNameA(0,cBuf,256);<br> szFileName :=cBuf;<br> ServiceTable[0].lpServiceName := szServiceName;<br> ServiceTable[0].lpServiceProc := @ServiceCtrlDispatcher;<br> ServiceTable[1].lpServiceName := nil;<br> ServiceTable[1].lpServiceProc := nil;<br> StartServiceCtrlDispatcher(ServiceTable[0]);<br> InstallService(szServiceName, szServiceName, szFileName);<br><br>end;<br><br>begin<br> Main;<br>end.