asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

COM+ Automation
By Prasanna Pattam
Rating: 4.4 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Everyone knows it's a hassle to register the COM component in COM+ applications when the COM component is changed. The steps typically involve shutting down the COM+ application, deleting the existing components (refresh does not work if the binary compatibility changes), overwriting the existing DLL with the new DLL, and finally registering the DLL in the COM+ application. This means that every developer must be a part-time administrator. Since administration is far less exciting than writing the actual code for your components, this article shows you how to automate this process.

    COMAdmin Library

    COM+ Automation is possible because of a set of system-supplied components known as the Component Services Administration (COMAdmin) Library. This article will show how to create a COM component, which does the trick, and show how to call it from an ASP page.

    To program using COMAdmin components from Visual Basic set the project reference to "Com+ 1.0 Admin Type Library." If you look at the COMAdmin type library using the object browser, you will see that it contains only three objects, the COMAdminCatalog, the COMAdminCatalogCollection, and the COMAdminCatalogObject. The COMAdminCatalog component represents the COM+ Catalog Manager, while the other two components represent every object in the hierarchy.

    The COMAdmin object model contains quite few logical collections, such as Applications, Components, and Roles. However, the object model is a bit tricky when you first start programming because all of these collections map to the COMAdminCatalogCollection component. Similarly, there are several logical object types such as Application, Component, Role, and User that all map to the COMAdminCatalogObject component.

    COM+ Administration

    Let's see how we can we use this object to program. First, assume that the COM+ application is already created.

    You must begin any administrative session by establishing a connection to a Catalog manager on the local computer.

    
    Dim objCatalog As COMAdminCatalog
    Set objCatalog = New COMAdminCatalog
    
    
    If you want to administer a remote computer, then you can establish a connection on the remote computer using
    
    objCatalog.Connect "RemoteServer"
    
    
    After creating the Catalog Manager object, our first task is to shut down the application, which can be achieved by
    
         	objCatalog.ShutDownApplication "COM+ Application Name"
    
    
    Once the application is shut down, we have to remove all the components in that application. Removing the components is tricky. For that we have to use the COMAdminCatalogCollection object. Remember, this object is used to store the Application collection as well as the Component collection.

    In order to get the current Application object we have to get all the COM+ applications in the server into the COMAdminCatalogCollection object and iterate through them to the application we want. Obtaining all the applications is achieved by

    
         	Dim objCatalog As COMAdminCatalog
         	Dim objCatalogApplicationCollection As COMAdminCatalogCollection
    
    Set objCatalog = New COMAdminCatalog
         	'Getting all the Applications in the server
         	Set objCatalogApplicationCollection = 
    objCatalog.GetCollection("Applications")
         	objCatalogApplicationCollection.Populate
    
    
    The Populate method must be used to actually retrieve all the application objects from RegDB. Once we have all the Application objects we have to iterate through each object to the application in which we have to remove all the components. (There is no other way to get the Application object directly from Visual Basic.)

    Securing the Current Application Object. (Assume AppName contains the name of the current application.)

    
         	For iCounter = 0 To objCatalogApplicationCollection.Count - 1
    If objCatalogApplicationCollection.Item(iCounter).Name = AppName Then
    ' application is found
    'remove components code goes here
    		End If
    	Next
    
    
    After finding the current application, we have to use the same logic to get all the components in that application and we have to delete them.
    
    Set objCatalogComponentCollection = 
    objCatalogApplicationCollection.GetCollection _
    ("Components",  objCatalogApplicationCollection.Item(iCounter).Key)
                 objCatalogComponentCollection.Populate
    
    Once the Component collection is populated, we then iterate though 
    the collection and delete them.
                     For iComponentCounter = 0 To 
    objCatalogComponentCollection.Count - 1
                         'remove the component, which is always the top one
                         objCatalogComponentCollection.Remove 0
                     Next
    
    When all the components are deleted, the collection should be saved. 
    This can be done using
                 objCatalogComponentCollection.SaveChanges
    
    
    The complete code for removing the Components in an application is below:
    
    Public Function RemoveComponent(ByVal AppName As String) As Boolean
         'removes the component installed in the Application
         On Error GoTo Err_RemoveComponent
    
         Dim objCatalog As COMAdminCatalog
         Dim objCatalogApplicationCollection As COMAdminCatalogCollection
         Dim objCatalogComponentCollection As COMAdminCatalogCollection
    
         Dim iCounter As Integer
         Dim iComponentCounter As Integer
         Dim bRemoved As Boolean
    
         Set objCatalog = New COMAdminCatalog
         'getting all the Applications in the server
         Set objCatalogApplicationCollection = 
    objCatalog.GetCollection("Applications")
         objCatalogApplicationCollection.Populate
    
         'looping through all the Applications to get the current Application
         bRemoved = False
         For iCounter = 0 To objCatalogApplicationCollection.Count - 1
             'Debug.Print objCatalogApplicationCollection.Item(iCounter).Name
             If objCatalogApplicationCollection.Item(iCounter).Name = 
    AppName Then  ' if application is found
                 'Shutdown the application first
                 objCatalog.ShutDownApplication AppName
    
                 'getting the current collection
                 Set objCatalogComponentCollection = 
    objCatalogApplicationCollection.GetCollection _
                                              ("Components", 
    objCatalogApplicationCollection.Item(iCounter).Key)
                 objCatalogComponentCollection.Populate
                 'check if the remove option is enabled
                 If objCatalogComponentCollection.RemoveEnabled Then
                     For iComponentCounter = 0 To 
    objCatalogComponentCollection.Count - 1
                         'remove the component, which is always the top one
                         objCatalogComponentCollection.Remove 0
                     Next
                 Else
                     'raise an error
                     Err.Raise vbObjectError, "Method:RemoveComponent", _
     
    "RemoveEnabled property returned false"
                 End If
    
                 'saves changes
                 objCatalogComponentCollection.SaveChanges
    
                 bRemoved = True
                 Exit For
             End If
         Next
             RemoveComponent = bRemoved
         Exit Function
    Err_RemoveComponent:
         RemoveComponent = False
         Err.Raise Err.Number, Err.Source, Err.Description
    End Function
    
    
    Once all the components are removed we have to install the components. This is easy and straightforward. The code that does the installation is:
    
         objCatalog.InstallComponent "COM+ Application Name", DLLPath, "", ""
    
    

    Putting It All Together

    Since we've seen the code that does the COM+ administration, let's look at the user interface. First, create an Active X DLL called COMPlusAdmin, and it needs a class in it called Admin where I place all the administration code. I quickly create a Web interface (see figure below).

    I used ASPUpload (http://www.aspupload.com) to upload the DLL from the developer machine to the server. I created another class in the COMPlusAdmin DLL project called Register that has only one method, RegisterCOMPlus. Below demonstrates how this method works.

    
    Public Function RegisterCOMPlus(ByVal AppName As String, ByVal 
    DLLPath As String,  _
                                               ByVal CurrentPath As 
    String, ByVal User As String) As Boolean
         'calls the admin functions in a sequence
    
         On Error GoTo Err_RegisterCOMPlus
         Dim sMessage As String
         'creating the ObjectContext object and Response Object
         Dim objCxt As ObjectContext
         Dim objResponse As Response
    
         Set objCxt = GetObjectContext
         Set objResponse = objCxt.Item("Response")
    
         'creating a reference to the Admin class
         Dim objCOMAdmin As ComPlusAdmin.Admin
         Set objCOMAdmin = New ComPlusAdmin.Admin
    
    
         'checking if the Application exists
         If objCOMAdmin.IsApplicationExists(AppName) Then
    
             'send a message before replacing
             objResponse.Write "Sending out a replacing message to all the 
    connected users" & "<br>"
             objResponse.Flush
             sMessage = "Replacing the application '" & AppName & "' -- " & User
    
             SendNetMessage sMessage
             'shut down the application
             objResponse.Write "Shutting down the application 
    ""<b>" & AppName & "</b>""<br>"
             objResponse.Flush
             objCOMAdmin.ShutDownApplication AppName
    
             objResponse.Write "Removing the components in the application 
    ""<b>" & _
                                                 AppName & 
    "</b>""<br>"
             objResponse.Flush
             'remove the components in the current application
             objCOMAdmin.RemoveComponent AppName
    
             'copying the DLL from the Current Path to its path
             'if file exists delete the file
             objResponse.Write "Overwriting the DLL with the uploaded 
    DLL<br>"
             objResponse.Flush
             If Dir(DLLPath) <> "" Then
                 Kill DLLPath
             End If
    
             FileCopy CurrentPath, DLLPath
    
             'install the component
             objResponse.Write "Installing the components in to the 
    application ""<b>" & _
                                                  AppName & 
    "</b>""<br>"
             objResponse.Flush
             objCOMAdmin.InstallComponent AppName, DLLPath
    
             'sending the completing message
             objResponse.Write "Sending out a Completion message to all 
    the connected users" & "<br>"
             objResponse.Flush
             sMessage = "Successfully registered the application '" & 
    AppName & "' -- " & User
             SendNetMessage sMessage
    
         Else
             Err.Raise vbObjectError, "Method:RegisterCOMPlus", 
    "Application does not exist"
         End If
    
         RegisterCOMPlus = True
         Exit Function
    Err_RegisterCOMPlus:
         RegisterCOMPlus = False
         Err.Raise Err.Number, Err.Source, Err.Description
    End Function
    
    
    This method takes four input parameters, as follows: AppName - Name of the COM+ application
    DLLPath - The physical path where the DLL resides and registers into COM+
    CurrentPath - The current path of the DLL to be registered. This is important because you can only rewrite the DLL after the application is shut down.
    User - Name of the user who is replacing the COM+ application, which is used to send messages

    I also used ASP objects in this method to send status to the user.

    This is an outline of how the method works. This method checks whether the application exists by the isApplicationExists method in the Admin object. If the application exists, then Shut Down the application and remove all the Components in that application. Overwrite the existing DLL, and finally, install the DLL. This method is called from the ASP page, which does the registration work.

    Conclusion

    This article has covered the basic and day-to-day COM+ administration. The attached code contains some other COM+ administration modules like creating and deleting an application, which are not discussed in this article. Further, you can extend the functionality of this COM+ administration by adding Roles, Attributes, and Events, etc. For further reference see http://msdn.microsoft.com/library/periodic/period00/instincts0900.htm.

    About the Author

    Prasanna Pattam is a senior architect at Qwest Communications International, Inc., and has been working in the ASP technologies for the last four years. He can be reached at Prasanna.Pattam@qwest.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Apr 27, 2004 - Develop and Customize Web Parts with Custom Tool Parts
    Tool Parts provide an interface for Web Part properties well beyond the capabilities of the default property pane. In this article Gayan Peiris shows how to customize Web Parts with custom Tool Parts.
    [Read This Article]  [Top]
    Apr 7, 2004 - Reusable Components in ASP.NET 2.0, Object Binding and Precompilation
    This article demonstrates how to create a reusable component in ASP.NET 2.0 and then consume it from an ASP.NET page. Also learn how the ObjectDataSource control can be used to directly bind the output of an object to the controls in an ASP.NET page and how precompilation can be used to increase the performance of the Web application and catch compilation errors.
    [Read This Article]  [Top]
    Mar 31, 2004 - Build a Managed BHO and Plug into the Browser
    Browser Helper Objects (BHOs) are COM components that communicate with Internet Explorer to enrich the browsing experience. Michele Leroux Bustamante returns to the world of COM to show you how to build a managed BHO with the help of the .NET Framework's COM interoperability features.
    [Read This Article]  [Top]
    Feb 18, 2004 - Customizing SharePoint Web Parts with Custom Properties
    In addition to creating custom Web Parts for SharePoint Portal Server, developers can actually create their own custom properties to further enhance Web Part appearance and behavior. Gayan Peiris explains the process and provides all the necessary code.
    [Read This Article]  [Top]
    Sep 26, 2003 - Accessing Shared Resources Using ASP.NET
    Accessing shared resources is a challenge for many ASP.NET developers. Tony Arslan explains how a simple serviced component can solve this infamous problem.
    [Read This Article]  [Top]
    Oct 2, 2002 - Function Pointers and COM
    Using callbacks and function pointers in VB can be risky and complicated. Ben Garcia explains his work-around for the function pointer issue he encountered while creating the VB version of his SNMP component.
    [Read This Article]  [Top]
    Sep 4, 2002 - Creating an SNMP Component - Part 2
    In part two of this intriguing article series, Ben Garcia shows how to build an updated and improved SNMP component in VC++ AND VB, and he briefly explains why limitations in VB make VC++ a better language for developing this type of application.
    [Read This Article]  [Top]
    Jul 23, 2002 - Creating an SNMP Component
    Ben Garcia sheds some light on the Simple Network Management Protocol (SNMP). First he provides a history of SNMP, then he dives right into its architecture. Finally, he shows how to build a COM component that communicates with SNMP-enabled devices.
    [Read This Article]  [Top]
    Jun 26, 2002 - Accessing Caller ID from the Web - Part 1
    Paul Apostolos begins his series on using Web services and the MSComm32.OCX component to access caller id information from a Web page. In part 1, learn how to write the Visual Basic program that runs on the server and updates a database with incoming callers.
    [Read This Article]  [Top]
    Nov 20, 2001 - Creating a Server Component with VB - Redesigned - Part 2
    Doug Dean explains different methods of retrieving and manipulating data from a database in a VB DLL so that it is ready to be rendered in a browser.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers