100分求教各位高手(100分)

  • 主题发起人 主题发起人 9908006
  • 开始时间 开始时间
9

9908006

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

Similar threads

I
回复
0
查看
747
import
I
I
回复
0
查看
671
import
I
I
回复
0
查看
806
import
I
I
回复
0
查看
836
import
I
I
回复
0
查看
678
import
I
后退
顶部