菏泽教师招聘2016简章:转帖:DELPHI编写服务程序总结二

来源:百度文库 编辑:九乡新闻网 时间:2024/04/30 10:38:38
一、如何限制系统服务和桌面程序只运行一个

如何限制系统服务和桌面程序只运行一个

在工程加入下列代码可以设置系统服务和桌面程序只运行一个。
program FleetReportSvr;

uses
SvcMgr,
Forms,
SysUtils,
Windows,
SvrMain in 'SvrMain.pas' {FleetReportService: TService},
AppMain in 'AppMain.pas' {FmFleetReport};

{$R *.RES}

const
CSMutexName = 'Global\Services_Application_Mutex';
var
OneInstanceMutex: THandle;
SecMem: SECURITY_ATTRIBUTES;
aSD: SECURITY_DESCRIPTOR;
begin
InitializeSecurityDescriptor(@aSD, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@aSD, True, nil, False);
SecMem.nLength := SizeOf(SECURITY_ATTRIBUTES);
SecMem.lpSecurityDescriptor := @aSD;
SecMem.bInheritHandle := False;
OneInstanceMutex := CreateMutex(@SecMem, False, CSMutexName);
if (GetLastError = ERROR_ALREADY_EXISTS)then
begin
  DlgError('Error, Program or service already running!');
  Exit;
end;
if FindCmdLineSwitch('svc', True) or
  FindCmdLineSwitch('install', True) or
  FindCmdLineSwitch('uninstall', True) then
begin
  SvcMgr.Application.Initialize;
  SvcMgr.Application.CreateForm(TSvSvrMain, SvSvrMain);
  SvcMgr.Application.Run;
end
else
begin
  Forms.Application.Initialize;
  Forms.Application.CreateForm(TFmFmMain, FmMain);
  Forms.Application.Run;
end;
end.

二、在系统服务和桌面程序之间共享内存

用于创建内核对象的函数几乎都有一个指向SECURITY_ATTRIBUTES结构的指针作为其参数,在使用CreateFileMapping函数的时候,通常只是为该参数传递NULL,这样就可以创建带有默认安全性的内核对象。  
  默认安全性意味着对象的管理小组的任何成员和对象的创建者都拥有对该对象的全部访问权,而其他所有人均无权访问该对象。可以指定一个ECURITY_ATTRIBUTES结构,对它进行初始化,并为该参数传递该结构的地址。  
  它包含的与安全性有关的成员实际上只有一个,即lpSecurityDescriptor。当你想要获得对相应的一个内核对象的访问权(而不是创建一个新对象)时,必须设定要对该对象执行什么操作。如果想要访问一个现有的文件映射内核对象,以便读取它的数据,那么调用OpenfileMapping函数:通过将FILE_MAP_READ作为第一个参数传递给OpenFileMapping,指明打算在获得对该文件映象的访问权后读取该文件, 该函数在返回一个有效的句柄值之前,首先  
  执行一次安全检查。如果(已登录用户)被允许访问现有的文件映射内核对象,就返回一个有效的句柄。但是,如果被拒绝访问该对象,将返回NULL。

系统服务端核心代码:

constructor TPublicVars.Create(ANew: Boolean);
var
SecMem: SECURITY_ATTRIBUTES;
aSD: SECURITY_DESCRIPTOR;
begin
inherited Create;
{ 创建一个任何用户都可以访问的内核对象访问权 }
InitializeSecurityDescriptor(@aSD, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@aSD, True, nil, False);
SecMem.nLength := SizeOf(SECURITY_ATTRIBUTES);
SecMem.lpSecurityDescriptor := @aSD;
SecMem.bInheritHandle := False;
FMapFile := CreateFileMapping($FFFFFFFF, @SecMem, PAGE_READWRITE, 0, CSharedMemSize, CSharedMemName);
FMapFile := OpenFileMapping(File_Map_All_Access, False, CSharedMemName);
if (FMapFile = 0) then
begin
  raise Exception.Create(SysErrorMessage(GetLastError));
  OutputDebugString(PChar(SysErrorMessage(GetLastError)));
end
else
begin // 成功
  FShareMem := MapViewOfFile(FMapFile, File_Map_All_Access, 0, 0, CSharedMemSize);
  OutputDebugString(PChar(SysErrorMessage(GetLastError) + ',Handle=' + IntToStr(Handle)));
end;
end;

destructor TPublicVars.Destroy;
begin
UnmapViewOfFile(FShareMem);
CloseHandle(FMapFile);
inherited;
end;

桌面程序核心源代码:

constructor TPublicVars.Create(ANew: Boolean);
var
SecMem: SECURITY_ATTRIBUTES;
aSD: SECURITY_DESCRIPTOR;
begin
inherited Create;
{ 创建一个任何用户都可以访问的内核对象访问权 }
InitializeSecurityDescriptor(@aSD, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@aSD, True, nil, False);
SecMem.nLength := SizeOf(SECURITY_ATTRIBUTES);
SecMem.lpSecurityDescriptor := @aSD;
SecMem.bInheritHandle := False;
FMapFile := CreateFileMapping($FFFFFFFF, @SecMem, PAGE_READWRITE, 0, CSharedMemSize, CSharedMemName);
FMapFile := OpenFileMapping(File_Map_All_Access, False, CSharedMemName);
if (FMapFile = 0) then
begin
  raise Exception.Create(SysErrorMessage(GetLastError));
  OutputDebugString(PChar(SysErrorMessage(GetLastError)));
end
else
begin // 成功
  FShareMem := MapViewOfFile(FMapFile, File_Map_All_Access, 0, 0, CSharedMemSize);
  OutputDebugString(PChar(SysErrorMessage(GetLastError) + ',Handle=' + IntToStr(Handle)));
end;
end;

destructor TPublicVars.Destroy;
begin
UnmapViewOfFile(FShareMem);
CloseHandle(FMapFile);
inherited;
end;

详细源代码见报表服务和报表COM中的关于共享内存的源代码。需要注意创建共享内存需要放在:ServiceStart中初始化,不能放在initialization,否则还会出现权限不足的信息,因为initialization是在应用程序初始化之前执行的代码。

三、在服务中使用COM组件

在服务中调用COM组件不能像在桌面程序中直接创建,在每次创建之前先调用CoInitialize(nil),释放的时候调用CoUninitialize。例如:调用ADO组件
var
Qry: TADOQuery;
begin
CoInitialize(nil);
Qry := TADOQuery.Create(nil);
try
  ...
finally
  Qry.Free;
  CoUninitialize;
end;
end;