关于程序控制另一台计算机后台服务的问题(100分)

  • 主题发起人 主题发起人 catbrother
  • 开始时间 开始时间
C

catbrother

Unregistered / Unconfirmed
GUEST, unregistred user!
在一台XP系统的计算机上执行一下代码<br>function TForm_Main.ReStartServer(HostName , ServiceName : string) : Boolean;<br>var<br> &nbsp;SCM, SCH: SC_Handle;<br> &nbsp;spTemp : PChar;<br> &nbsp;ServiceStatus: TServiceStatus;<br>begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := False;<br> &nbsp; &nbsp; &nbsp; &nbsp;SCM := OpenSCManager(PChar(HostName), nil, SC_MANAGER_ALL_ACCESS);<br> &nbsp; &nbsp; &nbsp; &nbsp;if SCM &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SCH := OpenService(SCM, PChar(ServiceName), SC_MANAGER_ALL_ACCESS);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if SCH &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;QueryServiceStatus(SCH, ServiceStatus);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Not (ServiceStatus.dwCurrentState = SERVICE_STOPPED ) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := ControlService(SCH, SERVICE_CONTROL_STOP, ServiceStatus);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Result then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;repeat<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;QueryServiceStatus(SCH, ServiceStatus);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Sleep(1000);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;until (ServiceStatus.dwCurrentState = SERVICE_STOPPED);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;spTemp := nil;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := StartService(SCH, 0, spTemp );<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseServiceHandle(SCH);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseServiceHandle(SCM);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>end;<br><br>可以控制本地服务重新启动,但是无法控制其他计算机后台服务重启。<br>曾经有一段时间可以控制一台Win2000的服务器,奇怪的就是现在又不行了!<br><br>哪位老大能帮着看看,还要做什么工作啊?
 
哪能随便就控制另一台计算机的服务?<br>先要建立IPC$连接,连接的帐户要有管理员权限。
 
说具体一点沙<br><br>怎样建立IPC$连接?应该怎么设置呢
 
NetSource.dwType := RESOURCETYPE_ANY;<br> &nbsp; &nbsp;NetSource.lpLocalName := '';<br> &nbsp; &nbsp;NetSource.lpRemoteName := pchar('//' + server + '/ipc$');<br> &nbsp; &nbsp;NetSource.lpProvider := '';<br> &nbsp; &nbsp;if WnetAddConnection2(NetSource, 'password', 'administrator', 0) = 0 then<br>或者winexec('net use //server/ipc$...
 
试验中。。。。
 
没有用啊<br> WnetAddConnection2的返回值不为0<br>程序在SCM := OpenSCManager(PChar(HostName), nil, SC_MANAGER_ALL_ACCESS);<br>返回值为0
 
在远程的被控制端,是否要添加或者打开某种服务?
 
不为0说明没有连接成功,你看看对方电脑是否打开了IPC连接、密码是否正确
 
就是不知道在哪里打开IPC连接啊!
 
嘻嘻..俺是初学者.俺不懂.来搭搭台面的!
 
哪位老大能解释具体一点啊
 

Similar threads

I
回复
0
查看
541
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部