使用 COM+ 管理目录的介绍性示例
以编程方式使用 COM+ 管理员目录时,通常执行以下常规步骤(此处未按严格顺序提供):
- 在本地计算机上打开与 COM+ 目录的会话。 (可选)连接到远程计算机上的 COM+ 目录。
- 执行诸如启动或停止服务之类的操作,这些操作与特定的 COM+ 应用程序无关。
- 执行一些操作,如安装或导出 COM+ 应用程序,或将组件安装到应用程序中,这些操作与读取或写入文件有关。
- 向集合中添加新项,例如通过向“Applications”集合中添加一个新项来创建新的 COM+ 应用程序。
- 设置或获取集合中项的属性。
- 保存或放弃对目录的挂起更改。
- 处理可能发生的任何错误。
为了显示使用 COMAdmin 对象时这些步骤的外观,下面提供了 Microsoft Visual Basic 示例。 其中简要说明了上面描述的一些典型步骤,例如查找集合、枚举集合以检索项以及设置该项的属性。
在下面的示例中,将执行以下操作:
- 创建新的 COM+ 应用程序“MyHomeZoo”。
- 在应用程序中安装一些组件 Cat 和 Dog。 这两个组件都包含在一个必须已经存在的 DLL 中:MyZoo.DLL。
- 通过定义两个角色为应用程序配置基于角色的安全性:ZooKeeper 和 AllergicToCats。
- 为整个应用程序分配 ZooKeeper 角色访问权限。
- 将 AllergicToCats 角色访问权限仅分配给 Dog 组件。
- 启用安全属性,以便为应用程序强制实施角色检查。
- 将 MyHomeZoo 应用程序导出到文件,以便可以将其安装在其他计算机上。
若要在 Visual Basic 中使用此示例,请添加对 COM+ 管理类型库的引用。
Function SetupMyZoo() As Boolean ' Return False if any errors occur.
' Initialize error handling for this function.
SetupMyZoo = False
On Error GoTo My_Error_Handler
' Open a session with the catalog.
' Instantiate a COMAdminCatalog object.
Dim objCatalog As COMAdminCatalog
Set objCatalog = CreateObject("COMAdmin.COMAdminCatalog")
' Create a new COM+ application.
' First get the "Applications" collection from the catalog.
Dim objApplicationsColl As COMAdminCatalogCollection
Set objApplicationsColl = objCatalog.GetCollection("Applications")
' Add a new item to this collection.
Dim objZooApp As COMAdminCatalogObject
Set objZooApp = objApplicationsColl.Add
' The "Applications" collection determines the available properties.
' Set the "Name" property of the new application item.
objZooApp.Value("Name") = "MyHomeZoo"
' Set the "Description" property of the new application item.
objZooApp.Value("Description") = "My pets at home"
' Save changes made to the "Applications" collection.
objApplicationsColl.SaveChanges
' Install components into the application.
' Use the InstallComponent method on COMAdminCatalog.
' In this case, the last two parameters are passed as empty strings.
objCatalog.InstallComponent "MyHomeZoo","MyZoo.DLL","",""
' Define the roles ZooKeeper and AllergicToCats
' by adding them to the "Roles" collection related to MyHomeZoo.
' First get the "Roles" collection related to MyHomeZoo.
' Use the GetCollection method on COMAdminCatalogCollection,
' passing in the name of the desired collection, "Roles",
' and the Key property value of objZooApp.
' The Key property uniquely identifies the object, serving
' here to distinguish the "Roles" collection related
' to MyHomeZoo from that of any other application.
Dim objRolesColl As COMAdminCatalogCollection
Set objRolesColl = objApplicationsColl.GetCollection("Roles", objZooApp.Key)
' Add new items to this "Roles" collection.
Dim objZooKeeperRole As COMAdminCatalogObject
Dim objAllergicToCatsRole As COMAdminCatalogObject
Set objZooKeeperRole = objRolesColl.Add
Set objAllergicToCatsRole = objRolesColl.Add
' Set the "Name" for the new items.
objZooKeeperRole.Value("Name") = "ZooKeeper"
objAllergicToCatsRole.Value("Name") = "AllergicToCats"
' Save changes made to any items in this "Roles" collection.
objRolesColl.SaveChanges
' Assign the AllergicToCats role to the Dog component to
' restrict its scope of access. (The ZooKeeper role, if assigned
' only at the application level, can access the whole application.)
' First get the "Components" collection related to MyHomeZoo.
' Use the GetCollection method on COMAdminCatalogCollection,
' passing in the name of the desired collection, "Components", and
' the Key property value of objZooApp.
Dim objZooComponentsColl As COMAdminCatalogCollection
Set objZooComponentsColl = objApplicationsColl.GetCollection("Components", objZooApp.Key)
' Find the Dog component item in this "Components" collection.
' First Populate the collection to read in data for all its items.
objZooComponentsColl.Populate
' Enumerate through the "Components" collection
' until the Dog component item is located.
Dim objDog As COMAdminCatalogObject
For Each objDog in objZooComponentsColl
If objDog.Name = "Dog" Then
Exit For
End If
Next
' Set the role checking property at the component level for Dog.
objDog.Value("ComponentAccessChecksEnabled") = True
' Save these changes.
objZooComponentsColl.SaveChanges
' Get the "RolesForComponent" collection related to the
' Dog component, using the Key property of objDog.
Dim objRolesForDogColl As COMAdminCatalogCollection
Set objRolesForDogColl = objZooComponentsColl.GetCollection("RolesForComponent", objDog.Key)
' Add a new item to this "RolesForComponent" collection.
Dim objCatSneezerRole As COMAdminCatalogObject
Set objCatSneezerRole = objRolesForDogColl.Add
' Set the "Name" of the new item to be "AllergicToCats".
objCatSneezerRole.Value("Name") = "AllergicToCats"
' Save changes made to this "RolesForComponent" collection.
objRolesForDogColl.SaveChanges
' Now set properties to enforce role-based security.
' First set role-based security at the application level.
objZooApp.Value("ApplicationAccessChecksEnabled") = True
objZooApp.Value("AccessChecksLevel") = COMAdminAccessChecksApplicationComponentLevel
' Save these changes.
objApplicationsColl.SaveChanges
' Finally, export the new configured MyHomeZoo application to an
' MSI file, used to install the application on other machines.
' Use the ExportApplication method on COMAdminCatalogObject.
objCatalog.ExportApplication "MyHomeZoo", "c:\Program Files\COM applications\MyHomeZoo.MSI", 4
' Exit the function gracefully.
SetupMyZoo = True
My_Error_Handler:
If Not SetupMyZoo Then
MsgBox "Error # " & Err.Number & " (Hex: " & Hex(Err.Number) & ")" & vbNewLine & Err.Description
End If
objCatSneezerRole = Nothing
objRolesForDogColl = Nothing
objDog = Nothing
objZooComponentsColl = Nothing
objAllergicToCatsRole = Nothing
objZooKeeperRole = Nothing
objRolesColl = Nothing
objZooApp = Nothing
objApplicationsColl = Nothing
objCatalog = Nothing
Exit Function
相关主题