[?]使用CreateService函数出现的问题,谁能解答一下?!(100分)

  • 主题发起人 主题发起人 piaosiy
  • 开始时间 开始时间
P

piaosiy

Unregistered / Unconfirmed
GUEST, unregistred user!
// uses winSvc<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;svc,svcmgr:SC_HANDLE;<br>begin<br> &nbsp;svcmgr:=OpenSCManager('',nil,SC_MANAGER_CREATE_SERVICE);<br> &nbsp;if(svcmgr&gt;0) then<br> &nbsp;begin<br> &nbsp; &nbsp;svc:=CreateService( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; svcmgr, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; Pchar('TEST8'), &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; Pchar('TEST8'), <br> &nbsp; &nbsp; &nbsp; &nbsp; SERVICE_ALL_ACCESS, &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; SERVICE_WIN32_OWN_PROCESS, <br> &nbsp; &nbsp; SERVICE_AUTO_START, &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; SERVICE_ERROR_IGNORE, &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; Pchar('D:/FileMove/SFMS.exe'), <br> &nbsp; &nbsp; &nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; nil); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; caption :=format('返回信息: %s [%d]',[SysErrorMessage(GetLastError),GetLastError]);<br> &nbsp;end;<br> &nbsp;CloseServiceHandle(svc);<br> &nbsp;CloseServiceHandle(svcmgr);<br>end;<br><br>‘D:/FileMove/SFMS.exe’此文件是一个已写好的服务程序,执行上面的Button1Click事件后,将成功创建一个服务,但是Caption显示为“返回信息:重叠的I/O操作在进行中 [997]”,而且不管是在DOS状态下用 net start TEST8 命令还是在管理工具中服务里去启动该服务,均无法启动此服务,提示“在本地计算机无法启动TEST8服务 &nbsp;错误1053 &nbsp;服务并未及时响应启动或控制请求”,我在大富翁里搜了一下,有过此类甚至是同样的问题,但没有找到正确的解决办法,有谁有解决过这个问题的还请指点一下!
 
人呢,就没人能帮我解决吗?
 
scm := OpenSCManager( Nil, Nil, SC_MANAGER_CONNECT );<br>svc := OpenService( scm, PChar( aServiceName ), SERVICE_ALL_ACCESS ); <br> &nbsp; svc:=CreateService( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; svcmgr, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; Pchar('TEST8'), &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; Pchar('TEST8'), <br> &nbsp; &nbsp; &nbsp; &nbsp; SERVICE_ALL_ACCESS, &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; SERVICE_WIN32_OWN_PROCESS, <br> &nbsp; &nbsp; SERVICE_AUTO_START, &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; SERVICE_ERROR_IGNORE, &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; Pchar('D:/FileMove/SFMS.exe'), <br> &nbsp; &nbsp; &nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp; &nbsp; &nbsp; nil); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>CloseServiceHandle( svc );<br>CloseServiceHandle( scm );
 
超级牛X:<br> &nbsp; &nbsp;svc := OpenService( scm, PChar( aServiceName ), SERVICE_ALL_ACCESS ); <br>这一句里的aServiceName是什么?服务名?这个服务还没有创建啊。<br>那个CreateService函数后第一个参数是SCM吧?<br>我把PChar( aServiceName )改成PChar( 'TEST8' )后运行连服务都没有创建出来了啊?我开始的那个还能创建,只是没法启动。这是什么原因,或者我哪里错了??
 
參考:<br>C:/Program Files/Borland/Delphi7/Source/Vcl/ScktSrvr.dpr<br>它可以通過命令行啟動和刪除服務(service)
 
看过了,我现在的问题是用CREATESERVICE创建的服务没办法启动,前面我也说过&quot;不管是在DOS状态下用 net start TEST8 命令还是在管理工具中服务里去启动该服务,均无法启动此服务&quot;,在程序中用STARTSERVICE也同样启动不了.
 
我用你的過程將程序名称改为scktsrvr.exe,错误提示也是I/O...<br>再通服务管理器,启动该服务正常,<br>所以,我觉得<br>1.服务程序可能有问题<br>2.这个创建服务的代码也不正确.<br>// 這是afxrootkit的代碼, delphibox<br>procedure InstallService(ServiceName, DisplayName: pchar; FileName: string);<br>var<br> &nbsp;SCManager: SC_HANDLE;<br> &nbsp;Service: SC_HANDLE;<br> &nbsp;Args: pchar;<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 := CreateService(SCManager, ServiceName, DisplayName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, pchar(FileName), nil, nil, nil, nil, nil);<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>end;
 
hsgrass:<br> &nbsp; &nbsp;你确定通过服务管理器,启动该服务正常吗?我这里不管通过什么方式启动都是启动不了,通过服务管理器启动该服务会出错,状态显示为&quot;启动中&quot;然后无法启动,停止,暂停,继续,四个按钮全变成了灰色.<br>我用你给的程序试了一下,还是不行,以下是我的调用事件.<br>procedure TForm1.Button3Click(Sender: TObject);<br>var<br> &nbsp;SN,DN,FN:string;<br>begin<br> &nbsp;SN:='Test';<br> &nbsp;DN:='Test';<br> &nbsp;FN:='E:/lhw/FileMove/FileMoveServer.exe';<br> &nbsp;InstallService(Pchar(SN),Pchar(DN),FN);<br>end;<br>可以确定的是,我的服务程序没有问题,刚刚我还用FileMoveServer /install 和net start FileMoveServer试过了,然后也可以正常启动,停止,暂停,继续.
 
// 下面的代碼可以增加服務,刪除服務,在我的w2k+sp4<br>// 在DELPHI運行程式,安裝服務後,程式沒反應, 要退出那個scktsrvr.exe才可以使主界面有反应.<br>var<br> &nbsp;status: TServiceStatus;<br> &nbsp;Stopped, Paused: boolean;<br> &nbsp;StatusHandle: thandle;<br><br>procedure InstallService(ServiceName, DisplayName: pchar; FileName: string);<br>var<br> &nbsp;SCManager: SC_HANDLE;<br> &nbsp;Service: SC_HANDLE;<br> &nbsp;Args: pchar;<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 := CreateService(SCManager, ServiceName, DisplayName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, pchar(FileName), nil, nil, nil, nil, nil);<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>end;<br><br>procedure UninstallService(ServiceName: pchar);<br>var<br> &nbsp;SCManager: SC_HANDLE;<br> &nbsp;Service: SC_HANDLE;<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, ServiceName, SERVICE_ALL_ACCESS);<br> &nbsp; &nbsp;ControlService(Service, SERVICE_CONTROL_STOP, Status);<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; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Stopped := True;<br> &nbsp; &nbsp; &nbsp; &nbsp;Status.dwCurrentState := SERVICE_STOP_PENDING;<br> &nbsp; &nbsp; &nbsp; &nbsp;SetServiceStatus(StatusHandle, Status);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;SERVICE_CONTROL_PAUSE:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Paused := True;<br> &nbsp; &nbsp; &nbsp; &nbsp;Status.dwcurrentstate := SERVICE_PAUSED;<br> &nbsp; &nbsp; &nbsp; &nbsp;SetServiceStatus(StatusHandle, Status);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;SERVICE_CONTROL_CONTINUE:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Paused := False;<br> &nbsp; &nbsp; &nbsp; &nbsp;Status.dwCurrentState := SERVICE_RUNNING;<br> &nbsp; &nbsp; &nbsp; &nbsp;SetServiceStatus(StatusHandle, Status);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;SERVICE_CONTROL_INTERROGATE: SetServiceStatus(StatusHandle, Status);<br> &nbsp; &nbsp;SERVICE_CONTROL_SHUTDOWN: Stopped := True;<br> &nbsp;end;<br>end;<br><br>procedure TForm1.btn1Click(Sender: TObject);<br>const<br> &nbsp;srv = 'c:/Program Files/Borland/Delphi7/Source/vcl/scktsrvr.exe';<br>begin<br>// &nbsp;UninstallService('test1');<br>// &nbsp;UninstallService('test8');<br> &nbsp;UninstallService('test2');<br> &nbsp;InstallService('test2', 'test22', srv);<br>end;
 
hsgrass:<br> &nbsp; &nbsp;我系统也是w2k+sp4<br> &nbsp; &nbsp;不过安裝服務後,程序可以关掉,关掉后scktsrvr.exe这个服务也会自动关闭.<br> &nbsp; &nbsp;不知道你安装服务后有没有打开“管理工具”中的“服务”查看过这个服务的状态?我查看了一下,它的状态为“启动”而正常启动的服务为“已启动”,也就是说这个服务没有启动成功,你安装完服务后(就用你说的这个scktsrvr.exe),你等上几分钟,再把鼠标移动到它在托盘区上它的托盘上面,它会自动消失,当然,它的进程也没有了。请指教!
 
// 如果從delphi的工程裏面新建一個服務prj編譯後,可以在命令行直接啟動的.<br>C:/Program Files/Borland/Delphi7/Projects&gt;project2.exe /install<br>C:/Program Files/Borland/Delphi7/Projects&gt;net start service2<br>Service2 服務正在啟動 .<br>Service2 服務已經啟動成功。<br>C:/Program Files/Borland/Delphi7/Projects&gt;net stop service2<br>Service2 服務已經成功停止。<br>C:/Program Files/Borland/Delphi7/Projects&gt;project2.exe /uninstall<br><br>SvcMgr.pas<br>/////////////這是它的安裝與刪除代碼<br>procedure TServiceApplication.Run;<br> &nbsp;function FindSwitch(const Switch: string): Boolean;<br> &nbsp;begin<br> &nbsp; &nbsp;Result := FindCmdLineSwitch(Switch, ['-', '/'], True);<br> &nbsp;end;<br>var<br> &nbsp;ServiceStartTable: TServiceTableEntryArray;<br> &nbsp;ServiceCount, i, J: Integer;<br> &nbsp;StartThread: TServiceStartThread;<br>begin<br> &nbsp;AddExitProc(DoneServiceApplication);<br> &nbsp;if FindSwitch('INSTALL') then<br> &nbsp; &nbsp;RegisterServices(True, FindSwitch('SILENT'))<br> &nbsp;else if FindSwitch('UNINSTALL') then<br> &nbsp; &nbsp;RegisterServices(False, FindSwitch('SILENT'))<br>------------------------------<br>procedure TServiceApplication.RegisterServices(Install, Silent: Boolean);<br> &nbsp;procedure InstallService(Service: TService; SvcMgr: Integer);<br> &nbsp;procedure UninstallService(Service: TService; SvcMgr: Integer);<br>begin<br>...<br>end;<br><br>所以有兩種方式可以選擇, 1, 從命令行啟動, 2.使用api<br>下星期看看有沒有時間再搞啦, 明天開會,還有事沒做完, 不好意思.
 
命令行方式操作我也知道,我之所以要用程序来实现是因为命令行方式安装/反安装服务时会弹出一个Messagebox,如果有多个安装/反安装时会比较烦,所以才需要用程序实现。<br> &nbsp;先谢谢你了,你有事的话你先忙吧,有空了麻烦你再帮我看看。
 
'SILENT' 參數是不顯示提示框
 
嗯,好的,谢谢,我试试
 
先把贴结了吧,这两天暂时没时间搞这个东西了,hsgrass你能给我留个EMail或其它联系方式么?
 
msn/email: hsgrass#hotmail.com<br>上班時間
 
后退
顶部