//Windows服务的代码:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;const
WM_SERVICE_TEST = WM_USER + 299;type
TService1 = class(TService)
private
{ Private declarations }
protected
function DoCustomControl(CtrlCode: DWord): Boolean; override;
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;var
Service1: TService1;implementation{$R *.DFM}procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service1.Controller(CtrlCode);
end;function TService1.DoCustomControl(CtrlCode: DWord): Boolean;
begin
case CtrlCode of
WM_SERVICE_TEST:
begin
MessageBox(0, '测试服务自定义消息!', '测试', MB_OK+MB_ICONINFORMATION);
end;
end;
Result := True;
end;function TService1.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;end.
//测试应用程序:unit Unit1Exe;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;const
WM_SERVICE_TEST = WM_USER + 299;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses SvcMgr, WinSvc;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
MHnd, SHnd: SC_HANDLE;
SerStatus: _SERVICE_STATUS;
begin
MHnd := OpenSCManager('', nil, SC_MANAGER_ALL_ACCESS);
if MHnd <> 0 then
try
SHnd := OpenService(MHnd, PChar('Service1'), SERVICE_ALL_ACCESS);
if SHnd <> 0 then
try
if QueryServiceStatus(SHnd, SerStatus) then
if SerStatus.dwCurrentState = SERVICE_RUNNING then
if not ControlService(SHnd, WM_SERVICE_TEST, SerStatus) then
ShowMessage('失败!');
finally
CloseServiceHandle(SHnd);
end;
finally
CloseServiceHandle(MHnd);
end;
end;end.//为什么 ControlService 不能成功,老是提示失败!
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;const
WM_SERVICE_TEST = WM_USER + 299;type
TService1 = class(TService)
private
{ Private declarations }
protected
function DoCustomControl(CtrlCode: DWord): Boolean; override;
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;var
Service1: TService1;implementation{$R *.DFM}procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service1.Controller(CtrlCode);
end;function TService1.DoCustomControl(CtrlCode: DWord): Boolean;
begin
case CtrlCode of
WM_SERVICE_TEST:
begin
MessageBox(0, '测试服务自定义消息!', '测试', MB_OK+MB_ICONINFORMATION);
end;
end;
Result := True;
end;function TService1.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;end.
//测试应用程序:unit Unit1Exe;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;const
WM_SERVICE_TEST = WM_USER + 299;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses SvcMgr, WinSvc;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
MHnd, SHnd: SC_HANDLE;
SerStatus: _SERVICE_STATUS;
begin
MHnd := OpenSCManager('', nil, SC_MANAGER_ALL_ACCESS);
if MHnd <> 0 then
try
SHnd := OpenService(MHnd, PChar('Service1'), SERVICE_ALL_ACCESS);
if SHnd <> 0 then
try
if QueryServiceStatus(SHnd, SerStatus) then
if SerStatus.dwCurrentState = SERVICE_RUNNING then
if not ControlService(SHnd, WM_SERVICE_TEST, SerStatus) then
ShowMessage('失败!');
finally
CloseServiceHandle(SHnd);
end;
finally
CloseServiceHandle(MHnd);
end;
end;end.//为什么 ControlService 不能成功,老是提示失败!
解决方案 »
- 各位再帮忙看看呀!!!!
- Delphl7有补丁么??
- 急,动态创建4个Form,用FindCompont查找这些Form怎么找不到呢?
- 菜鸟问题!
- 点击PageControl中不同的TabSheet,改变焦点的问题
- 菜鸟问题
- 用midas开发三层结构,发布时midas是不是要许可?哪里能买到。
- 100分求对delphi x控件熟的人一个问题
- ide
- 大家给我推荐一本关于SQLSERVER2000的好书!
- 谁知道在delphi中有无可用的RTP/RTCP协议通讯控件?
- SetWindowLong(Handle, GWL_STYLE, GetWindowLong(Handle,GWL_STYLE) and not WS_CAPTION);这条语句是什么意思?
你的分数加上了吗?我在结贴时 CSDN 报错,再次结贴又不让结了,
最近 CSDN 怎么老是出这个问题!