我仿造delphi提供的demo做了一个NT的服务,可以切换普通程序和服务程序的,但是在加入/install运行后,提示 'service installed successfully' ,到服务中没有发现这个服务,再次运行,同样提示'service installed successfully',晕倒,应该提示已经有这个服务才对啊?哪位朋友遇到或解决过这个问题一定要帮俺一把啊~~~ 还有我一直没有搞明白displayname的属性是怎么设置的
工程代码如下
program superspys;uses
SvcMgr,
Forms,
Windows,
SysUtils,
WinSvc,
ScktCnst,
superspy in 'superspy.pas' {mainform},
lookvip in 'lookvip.pas';{$R *.res}function Installing: Boolean;
begin
Result := FindCmdLineSwitch('INSTALL',['-','\','/'], True) or
FindCmdLineSwitch('UNINSTALL',['-','\','/'], True);
end;function StartService: Boolean;
var
Mgr, Svc: Integer;
UserName, ServiceStartName: string;
Config: Pointer;
Size: DWord;
begin
Result := False;
Mgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if Mgr <> 0 then
begin
Svc := OpenService(Mgr, PChar(SServiceName), SERVICE_ALL_ACCESS);
Result := Svc <> 0;
if Result then
begin
QueryServiceConfig(Svc, nil, 0, Size);
Config := AllocMem(Size);
try
QueryServiceConfig(Svc, Config, Size, Size);
ServiceStartName := PQueryServiceConfig(Config)^.lpServiceStartName;
if CompareText(ServiceStartName, 'LocalSystem') = 0 then
ServiceStartName := 'SYSTEM';
finally
Dispose(Config);
end;
CloseServiceHandle(Svc);
end;
CloseServiceHandle(Mgr);
end;
if Result then
begin
Size := 256;
SetLength(UserName, Size);
GetUserName(PChar(UserName), Size);
SetLength(UserName, StrLen(PChar(UserName)));
Result := CompareText(UserName, ServiceStartName) = 0;
end;
end;begin
if not Installing then
begin
CreateMutex(nil, True, 'superspys');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, PChar(SAlreadyRunning), SApplicationName, MB_ICONERROR);
Halt;
end;
end;
if Installing or StartService then
begin
SvcMgr.Application.Initialize;
SvcMgr.Application.CreateForm(Tmainform, mainform);
SvcMgr.Application.Run;
end else
begin
Forms.Application.ShowMainForm := False;
Forms.Application.Initialize;
Forms.Application.CreateForm(Tmainform, mainform);
Forms.Application.Run;
end;
end.
工程代码如下
program superspys;uses
SvcMgr,
Forms,
Windows,
SysUtils,
WinSvc,
ScktCnst,
superspy in 'superspy.pas' {mainform},
lookvip in 'lookvip.pas';{$R *.res}function Installing: Boolean;
begin
Result := FindCmdLineSwitch('INSTALL',['-','\','/'], True) or
FindCmdLineSwitch('UNINSTALL',['-','\','/'], True);
end;function StartService: Boolean;
var
Mgr, Svc: Integer;
UserName, ServiceStartName: string;
Config: Pointer;
Size: DWord;
begin
Result := False;
Mgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if Mgr <> 0 then
begin
Svc := OpenService(Mgr, PChar(SServiceName), SERVICE_ALL_ACCESS);
Result := Svc <> 0;
if Result then
begin
QueryServiceConfig(Svc, nil, 0, Size);
Config := AllocMem(Size);
try
QueryServiceConfig(Svc, Config, Size, Size);
ServiceStartName := PQueryServiceConfig(Config)^.lpServiceStartName;
if CompareText(ServiceStartName, 'LocalSystem') = 0 then
ServiceStartName := 'SYSTEM';
finally
Dispose(Config);
end;
CloseServiceHandle(Svc);
end;
CloseServiceHandle(Mgr);
end;
if Result then
begin
Size := 256;
SetLength(UserName, Size);
GetUserName(PChar(UserName), Size);
SetLength(UserName, StrLen(PChar(UserName)));
Result := CompareText(UserName, ServiceStartName) = 0;
end;
end;begin
if not Installing then
begin
CreateMutex(nil, True, 'superspys');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, PChar(SAlreadyRunning), SApplicationName, MB_ICONERROR);
Halt;
end;
end;
if Installing or StartService then
begin
SvcMgr.Application.Initialize;
SvcMgr.Application.CreateForm(Tmainform, mainform);
SvcMgr.Application.Run;
end else
begin
Forms.Application.ShowMainForm := False;
Forms.Application.Initialize;
Forms.Application.CreateForm(Tmainform, mainform);
Forms.Application.Run;
end;
end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;type
TService1 = class(TService)
procedure ServiceExecute(Sender: TService);
private
{ Private declarations }
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.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;procedure TService1.ServiceExecute(Sender: TService);
begin
Service1.DisplayName := 'ss';
ShowMessage('s');
end;end.
Mgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
StartService(Svc,0,nil)//startservice 为Api
Svc := OpenService(Mgr, PChar(SServiceName), SERVICE_ALL_ACCESS);
如要用DisplayName(大概如此,我没有测试)
CreateService(Svc, //handle
ServiceName, //servicename
DisplayName, //displayname
SERVICE_ALL_ACESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START ,
SERVICE_ERROR_SERVER,
PathName //pathname
nil,
nil,
nil,
ServiceStartName, //servicestartname
nil)我在Source下找不到例子,我用的是D7,可以发一份给我吗[email protected]
SvcMgr.Application.CreateForm(Tmainform, mainform);
SvcMgr.Application.Run;
改为
SvcMgr.Application.Initialize;
SocketService := TSocketService.CreateNew(SvcMgr.Application,0);
//TSocketService应为TService类(见ScktMain),要作Server程序的话必须有TService类 SvcMgr.Application.CreateForm(Tmainform, mainform);
SvcMgr.Application.Run;