Welcome to weblogs.com.pk Sign in | Join | Help

Nasir Ali Khan

Believing is easier than thinking. Hence so many more believers than thinkers.
Escape Dll-Hell with Registry Free COM
Intended Readers: Persons having basic knowledge of Component Object Model (COM) and Dll Registration etc
 
Level: Beginners
 
Background
COM is a Microsoft’s binary standard for developing the language independent components it enables the object developed in one COM compliant language such as  C++ to be consumed in any other COM compliant language. COM plays the vital part in the success of Microsoft windows and other development technologies such as Visual Basic, ActiveX. COM was there in the early days of windows 2.x in the form of OLE since then it’s a fundamental architecture on which other Microsoft technologies and operating system built. Here I am not going into the details of COM, if you want more then click here.
 
As technology evolves and grows it uses increases, COM shows its age and shows some weaknesses and complexities such as component registration process, versioning and side by side execution of components, later on Microsoft it self named these issues as Dll Hell.
 
When an application uses COM Component, the component must be registered in the system registry, the typical process of Activating COM object includes
  1. User program directly call CoCreateInstance (…) or Language runtime internally calls this (such as VB).
  2. COM runtime then find the specified CLSID (unique identifier of class) in the system registry.
  3. COM runtime then loads the specified component, and pass this object to the client.
COM activation mechanism relies heavily on the registry, this results problems such as
  • Registration of component required invoking separate program and installers, means no possibility of XCopy installation.
  • Corruption of component registry keys results activation failure of object.
  • Excessive use of registry slows down the system performance.
  • Its very difficult to maintain and upgrade the COM component
  • Side by side execution of different versions of same components is not possible at all

Introduction
Registry free COM is a new activation method for the COM components which introduced in Windows XP, it doesn’t relies on the registry to store the component information just like the .NET assembly. But where the component information stored, the answer is application manifest file. In Registry Free COM component is not responsible for maintaining its information instead client application stores its information in its manifest file.
 
Manifest file is actually a XML document which contains all the component information referenced by the application, application manifest must present in the current folder of application and its name must be same as application with .manifest extension for example if application is MyApp.Exe then its manifest will be MyApp.Exe.Manifest.
 
When an application contains its manifest, windows XP builds an internal table that relates each CLSID (class identifier) to the component file. When application creates COM object, COM runtime looks this internal table instead of system registry and no registry required in activation of COM object. The beauty of all this process is that associated manifest is isolated from other components and applications even other applications uses the same component they won’t effect this application.

Benefits of Registry free COM
  • No need to register the object in the registry, avoid Dll-Hell completely
  • Existing COM objects can be configured for Registry Free Activation without recompiling the code (just add the application manifest in the caller application).
  • Every application has only one manifest which completely independent and isolated from the other manifest and components.
  • If operating system doesn’t find component information is application manifest then it will look into the system registry.
  • Manifest is associated with the client application not with the COM object, means one COM object can be configured for multiple clients independently and component it self is unaware of manifest details.
  • No need to install and uninstall the component, now XCOPY deployment is possible for the COM components even legacy ones.

Structure of Application Manifest
Following is a simple structure for xml manifest file (details omitted for brevity)
<?xml version="1.0" encoding="utf-8"?>
<assembly xsi:schemaLocation="..."
manifestVersion="1.0"
       xmlns:asmv1="..."
       xmlns:asmv2="..."
       xmlns:dsig="..."
       xmlns="..."
       xmlns:xsi="...">
      
       <assemblyIdentity name="MyApp.exe"
version="1.0.0.0"
type="win32" />
      
       <file name="MyComponent.dll" asmv2:size="...">
              <hash xmlns="...">
                     <dsig:Transforms>
                           <dsig:Transform Algorithm="..." />
                     </dsig:Transforms>
                     <dsig:DigestMethod Algorithm="..." />
                     <dsig:DigestValue>...</dsig:DigestValue>
              </hash>
             
              <typelib tlbid="..."  version="1.0" helpdir=""
resourceid="0"  flags="HASDISKIMAGE" />
             
              <comClass clsid="..." threadingModel="..."
                     tlbid=""
                     progid="COMComponent.MyCOMComponent" />
       </file>
</< SPAN>assembly>
 
Here the root element of manifest is assembly attribute which contains some attributes for using the various xml namespaces which is referenced later in the document.
 
The first important element here is assemblyIdentity, this element contains the information about the associated application. Its attributes are
  • name: represents the name of the application
  • version: version of the application
  • type: type of the application such win32/clr
File element contains the information about the one or more component consumed by the current application, name attribute contains the name of the component such as MyComponent.dll, size attribute contain its size.
 
Hash element contains the hashing information about the component such as algorithm used, digest value etc.
 
Typelib element contains the information about the type library of the application, this is where the registry related information of COM object comes. It contains the following attributes
  • tlbid: GUID of the type library
  • version: version of the type library
  • resourceid: resource identifier of the manifest, used when manifest is embedded in the assembly
  • flags: extra information about the type library
 comClass element contains the information about the one or more COM objects in the component, it contains the following attributes
  • clsid: CLSID (class identifier) of the COM object
  • threadingModel: threading model of COM object such as apartment, neutral etc
  • tlbid: type library of the COM object
  • progid: Program identifier of COM object used in CreateObject(…) etc.

Configuring COM object for Registry Free activation
Let’s look at the example of registry free COM activation, due to its simplification I use Visual Basic for developing the object but you can use any other language as well.
 
1.     Create a activeX DLL project in Visual Basic, name the project COMComponent, and the class module as MyComponent along with the following definition 
Public Sub ShowMessage()
    MsgBox "Hello World, From VB 6"
End Sub
 
Compile this project, which results the creation of COMComponent.dll
 
2.     Create another project but this time Standard EXE and add the reference of MyComponent.dll in the project, add button in the form and place the following code in the click event of the button
         Private Sub Command1_Click()
         Dim obj As New COMComponent.MyCOMComponent
   obj.ShowMessage
End Sub
 
     3. COMComponent.dll is already registered in the system registry, so unregistered it with the regsvr32.exe
Regsvr32 /u /COMComponent.dll
 
Now run the application again and click the button, you will see that this time application fails to load the COM component because no information found in the registry. 
 
   4. Now place the COMComponent.dll in the same folder where client application resides and   create the file with the name MyApp.exe.manifest with the following contents
 
<?xml version="1.0" encoding="utf-8"?>
<assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd"
                manifestVersion="1.0"
                xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
                xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
                xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"
                xmlns="urn:schemas-microsoft-com:asm.v1"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      
       <assemblyIdentity name="MyApp.exe" version="1.0.0.0" type="win32" />
 
       <file name="COMComponent.dll" asmv2:size="20480">
              <hash xmlns="urn:schemas-microsoft-com:asm.v2">
                     <dsig:Transforms>
                           <dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
                     </dsig:Transforms>
                     <dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                     <dsig:DigestValue>SrFXR9dKaSDHaLagpsRpQaSpj6k=</dsig:DigestValue>
              </hash>
             
              <typelib tlbid="{08f0d5da-8f00-4410-8e19-ca1fbbfb9387}"
                            version="1.0" helpdir="" resourceid="0"
                            flags="HASDISKIMAGE" />
             
              <comClass clsid="{b473078f-4f9d-48d3-a43a-a05fbee229c9}"
                             threadingModel="Apartment"
                             tlbid="{08f0d5da-8f00-4410-8e19-ca1fbbfb9387}"
                             progid="COMComponent.MyCOMComponent" />
       </file>
</assembly>
 
This time COM object activates successfully without the registry lookup process, you can double check this by renaming the application manifest (which results failure of application again) the evidence of true power of registry free COM.

Registry Free COM Activation in Visual Studio 2005
Registry free COM activation in VS.NET 2005 is very simple, every COM reference  in VS 2005 contains a new property called Isolated. By default this property is false which means that component should be treated like normal. If this property is true , it causes the application manifest to be generated at the build time, once reference is added no registry information is needed for the component.
 
Visual Studio 2005 contains a command line utility called MT.exe which is used to generate the component manifest but this utility requires component to be registered on the machine.

Limitations of Registry Free COM
Registry free COM has its own limitations such as
 
  • Registry Free COM activation requires at least Windows XP.
  • Only In-proc COM servers are supported.
  • It manifest is generated with the tools such as MT.exe, then proper component registration is required
  • Any classes which are not defined in the type library will not be reflected in the application manifest.
Following are some cases where it is not appropriate to use the registry free activation
 
  • If component is the part of operating system (Internet explorer, shell) then it is not feasible to activate it with registry free COM.
  • If component is a part of the large framework which contains multiple components, and one component depends on the other.
  • Components such as Internet explorer plugins , shell extensions are not suitable, because they require special registration in the hosting environment.
  • Component or controls which are intended to add in the toolbox of IDE such as Visual studio or in the insert object list (OLE object).
 
Conclusion
Registry Free COM escapes the legacy COM objects from the Dll-Hell, results better application deployment and maintenance similar to Microsoft.NET architecture it adds all the component registration information in a separate xml file called application manifest. We can create the manifest file either manually or from the tools such as MT.exe which shipped with VS 2005, Visual studio 2005 also supports registry free COM activation by setting the Isolated Property of COM reference to true.
 
Your Comments and feedbacks are always welcome
 
Posted: Thursday, July 07, 2005 2:29 AM by nasir

Comments

No Comments

Anonymous comments are disabled