怎样与Delphi开发的NT服务程序通信?(200)(200分)

  • 主题发起人 主题发起人 delphiinfomail
  • 开始时间 开始时间
D

delphiinfomail

Unregistered / Unconfirmed
GUEST, unregistred user!
用Delphi开发了一个NT的服务程序,但是怎样与此NT服务程序通信呢?有例子吗?
急啊!
 
你要实现怎样的通信,具体点说。
 
用COM吧,简单问题
 
是要控制这个服务程序的启动,停止么? 如果是, 参考...详细的发邮件tseug@263.net

unit Control;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, WinSvc;

const
ServiceName='TomDemoService';

type
TfrmMain = class(TForm)
cmdStart: TButton;
cmdStop: TButton;
stsMessage: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure cmdStopClick(Sender: TObject);
procedure cmdStartClick(Sender: TObject);
private
{ Private declarations }
schService:SC_HANDLE;
schSCManager:SC_HANDLE;
ssStatus:TServiceStatus;
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.DFM}


procedure TfrmMain.FormCreate(Sender: TObject);
begin
schSCManager:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if (schSCManager<=0) then
begin
MessageDlg('Tom Lee Demo Service is not installed.',mtError,[mbOk],0);
Application.Terminate;
end;

schService:=OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);
if (schService<=0) then
begin
MessageDlg('Tom Lee Demo Service can not open.',mtError,[mbOk],0);
Application.Terminate;
end;
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
end;

procedure TfrmMain.cmdStopClick(Sender: TObject);
begin
if ControlService(schService,SERVICE_CONTROL_STOP,ssStatus) then
begin
stsMessage.SimpleText:='Stopping Service Now ';
Sleep(1000);
while (QueryServiceStatus(schService,ssStatus)) do
begin
Application.ProcessMessages;
if ssStatus.dwCurrentState=SERVICE_STOP_PENDING then
begin
stsMessage.SimpleText:=stsMessage.SimpleText+('.');
Sleep(1000);
end
else
break;
end;

if ssStatus.dwCurrentState=SERVICE_STOPPED then
stsMessage.SimpleText:='Service Stop Ok'
else
stsMessage.SimpleText:='Service Stop Fail';
end
else
stsMessage.SimpleText:='Service Stop Fail';
end;

procedure TfrmMain.cmdStartClick(Sender: TObject);
var
Argv:PChar;
begin
if StartService(schService,0,Argv) then
begin
stsMessage.SimpleText:='Start Service Now ';
Sleep(1000);
while (QueryServiceStatus(schService,ssStatus)) do
begin
Application.ProcessMessages;
if ssStatus.dwCurrentState=SERVICE_START_PENDING then
begin
stsMessage.SimpleText:=stsMessage.SimpleText+('.');
Sleep(1000);
end
else
break;
end;

if ssStatus.dwCurrentState=SERVICE_RUNNING then
stsMessage.SimpleText:='Service Start Ok'
else
stsMessage.SimpleText:='Service Start Fail';
end
else
stsMessage.SimpleText:='Service Start Fail';
end;

end.
 
具体来讲就是A(可能是一般应用也可能是NT服务)程序向B(NT服务形式)程序之间进行
数据交换,不仅仅是服务的启动和停止。(不使用SOCKET的方式)有什么好办法吗?
 
还有就是命名管道, 内存映射文件, COM+等等, 我觉得内存映射文件比较好...
 
可以在server上放个indy的socketserver控建,前端用socket客户端发就是了
 
to:tseug能给过例子学习吗?谢谢!
 
邮箱, 或者给我发邮件tseug@263.net
 
delphiinfomail@163.net谢谢或QQ:36625379
 
接受答案了.
 
后退
顶部