uses ComObj
//安装COM+应用程序
function
InstallCOMApplication:Boolean;
const
SComApplicationName = ' 你的应用程序名 ' ;
SFullDllFileName = ' C:\a.dll ' ; // 你的DLL,这里只注册一个DLL
var
COMAdminCatalog : OleVariant;
CatalogCollection : OleVariant;
CatalogObject : OleVariant;
i : Integer;
begin
try
COMAdminCatalog : = CreateOleObject( ' COMAdmin.COMAdminCatalog ' );
CatalogCollection : =
COMAdminCatalog.GetCollection( ' Applications ' );
CatalogCollection.Populate;
for i : = 0 to CatalogCollection.Count - 1 do
if CatalogCollection.Item[i].Name = SComApplicationName then Break;
if i = CatalogCollection.Count then
begin
CatalogObject : = CatalogCollection.Add;
CatalogObject.Value[ ' Name ' ] : = SComApplicationName;
CatalogCollection.SaveChanges;
end
else
CatalogObject : = CatalogCollection.Item[i];
COMAdminCatalog.InstallComponent(
CatalogObject.Name,
SFullDllFileName, '' , '' );
Result : = True;
except
Result : = False;
end ;
end ;
const
SComApplicationName = ' 你的应用程序名 ' ;
SFullDllFileName = ' C:\a.dll ' ; // 你的DLL,这里只注册一个DLL
var
COMAdminCatalog : OleVariant;
CatalogCollection : OleVariant;
CatalogObject : OleVariant;
i : Integer;
begin
try
COMAdminCatalog : = CreateOleObject( ' COMAdmin.COMAdminCatalog ' );
CatalogCollection : =
COMAdminCatalog.GetCollection( ' Applications ' );
CatalogCollection.Populate;
for i : = 0 to CatalogCollection.Count - 1 do
if CatalogCollection.Item[i].Name = SComApplicationName then Break;
if i = CatalogCollection.Count then
begin
CatalogObject : = CatalogCollection.Add;
CatalogObject.Value[ ' Name ' ] : = SComApplicationName;
CatalogCollection.SaveChanges;
end
else
CatalogObject : = CatalogCollection.Item[i];
COMAdminCatalog.InstallComponent(
CatalogObject.Name,
SFullDllFileName, '' , '' );
Result : = True;
except
Result : = False;
end ;
end ;
//卸载COM+应用程序
function
UnInstallCOMApplication:Boolean;stdcall;
const
SComApplicationName = ' 你的应用程序名 ' ;
var
COMAdminCatalog : OleVariant;
CatalogCollection : OleVariant;
i : Integer;
begin
try
COMAdminCatalog : = CreateOleObject( ' COMAdmin.COMAdminCatalog ' );
CatalogCollection : = COMAdminCatalog.GetCollection( ' Applications ' );
CatalogCollection.Populate;
for i : = 0 to CatalogCollection.Count - 1 do
if CatalogCollection.Item[i].Name = SComApplicationName then
begin
CatalogCollection.Remove(i);
CatalogCollection.SaveChanges;
Break;
end ;
Result : = True;
except
Result : = False;
end ;
end ;
const
SComApplicationName = ' 你的应用程序名 ' ;
var
COMAdminCatalog : OleVariant;
CatalogCollection : OleVariant;
i : Integer;
begin
try
COMAdminCatalog : = CreateOleObject( ' COMAdmin.COMAdminCatalog ' );
CatalogCollection : = COMAdminCatalog.GetCollection( ' Applications ' );
CatalogCollection.Populate;
for i : = 0 to CatalogCollection.Count - 1 do
if CatalogCollection.Item[i].Name = SComApplicationName then
begin
CatalogCollection.Remove(i);
CatalogCollection.SaveChanges;
Break;
end ;
Result : = True;
except
Result : = False;
end ;
end ;
//启动/关闭COM+服务
function
StartCOMService(
const
AStart : Boolean):Boolean;
const
SComApplicationName = ' 你的应用程序名 ' ;
var
COMAdminCatalog : OleVariant;
CatalogCollection : OleVariant;
i : Integer;
begin
Result : = False;
try
COMAdminCatalog : = CreateOleObject( ' COMAdmin.COMAdminCatalog ' );
CatalogCollection : = COMAdminCatalog.GetCollection( ' Applications ' );
CatalogCollection.Populate;
for i : = 0 to CatalogCollection.Count - 1 do
if CatalogCollection.Item[i].Name = SComApplicationName then
begin
if AStart then
COMAdminCatalog.StartApplication(SComApplicationName)
else
COMAdminCatalog.ShutdownApplication (SComApplicationName);
Result : = True;
Break;
end ;
except
end ;
end ;
end ;
const
SComApplicationName = ' 你的应用程序名 ' ;
var
COMAdminCatalog : OleVariant;
CatalogCollection : OleVariant;
i : Integer;
begin
Result : = False;
try
COMAdminCatalog : = CreateOleObject( ' COMAdmin.COMAdminCatalog ' );
CatalogCollection : = COMAdminCatalog.GetCollection( ' Applications ' );
CatalogCollection.Populate;
for i : = 0 to CatalogCollection.Count - 1 do
if CatalogCollection.Item[i].Name = SComApplicationName then
begin
if AStart then
COMAdminCatalog.StartApplication(SComApplicationName)
else
COMAdminCatalog.ShutdownApplication (SComApplicationName);
Result : = True;
Break;
end ;
except
end ;
end ;
end ;
//客户端连接计数 TRMDServer是你的COM类定义
procedure
TRMDServer.RemoteDataModuleCreate(Sender: TObject);
begin
Inc(Querycount);
end ;
procedure TRMDServer.RemoteDataModuleDestroy(Sender: TObject);
begin
Dec(Querycount);
end ;
begin
Inc(Querycount);
end ;
procedure TRMDServer.RemoteDataModuleDestroy(Sender: TObject);
begin
Dec(Querycount);
end ;