You created a .NET assembly named XYZAssembly that must be used by an existing standard COM client application. The COM client will run on a separate client computer, and it must be able to instantiate components from XYZAssembly as if they were COM components. How should you prepare and deploy the assembly so that the COM client can create and use these types?

Difficulty: Easy

Correct Answer: Use the Assembly Registration tool (Regasm.exe) to generate a registry entry and optional type library for the assembly, and then register it on the client computer.

Explanation:


Introduction / Context:
COM interop enables existing COM applications to use managed .NET components and allows .NET applications to use existing COM components. When you write a .NET assembly that must be consumed by a legacy COM client, you must expose your managed types as COM-visible classes and create the correct registry entries and type library information on the client computer. This question tests your understanding of which tool is appropriate for registering a .NET assembly for COM clients.



Given Data / Assumptions:

  • XYZAssembly is a .NET assembly built with Visual Studio .NET.
  • An existing COM client application needs to create objects from XYZAssembly as COM components.
  • The assembly will be deployed on client computers where the COM client runs.
  • You must provide the necessary COM metadata and registry entries.



Concept / Approach:
To make a .NET assembly available to COM clients, you must register it with the system so that CLSIDs and ProgIDs are mapped to the managed types. The Assembly Registration tool, Regasm.exe, reads the metadata in the assembly and creates COM registration entries in the Windows registry. It can also generate a type library (.tlb) file that describes the exposed interfaces and classes to COM. Tlbimp.exe works in the opposite direction, importing a COM type library into a .NET assembly. Strong naming and placing an assembly in the global assembly cache are not sufficient by themselves to make the assembly visible to COM clients.



Step-by-Step Solution:
1. Design your managed classes with appropriate attributes, such as [ComVisible(true)] and GUID attributes, so they can be exposed to COM. 2. Build XYZAssembly.dll using Visual Studio .NET. 3. On the client computer, run Regasm.exe with XYZAssembly.dll as input. Optionally use the /tlb switch to generate a type library file for COM clients that need it. 4. Regasm writes the required CLSID and ProgID entries into the registry, mapping COM identifiers to your managed types. 5. The COM client can now create instances of these types using standard COM creation mechanisms, and the .NET runtime will host and manage the actual managed objects.



Verification / Alternative check:
After registration, you can use tools like OLE/COM Object Viewer or simply attempt to instantiate the COM-visible class from the COM client. If registration succeeded, the COM client will find the CLSID or ProgID and be able to create the object. If Regasm was not run or failed, object creation attempts will typically result in errors such as "ActiveX component cannot create object".



Why Other Options Are Wrong:
Option A, creating a strong name with Sn.exe, is useful for versioning and placing assemblies in the global assembly cache, but it does not by itself register the assembly for COM. Option B uses Tlbimp.exe, which is specifically for importing a COM type library into a .NET interop assembly, the opposite of what is needed here. Option D, copying the assembly to the global assembly cache and referencing it directly from the COM application, is not sufficient because COM relies on registry-based activation and type libraries, both of which are provided by Regasm.



Common Pitfalls:
A common mistake is confusing Regasm and Tlbimp, or assuming that strong naming and the global assembly cache are enough for COM visibility. Developers sometimes forget to set ComVisible attributes or to provide fixed GUIDs for interfaces and classes, which can cause versioning and compatibility issues. Another pitfall is registering the assembly on a development machine but forgetting to repeat registration on every client where the COM application runs. Following the correct Regasm-based process ensures a reliable COM interop layer.



Final Answer:
You should run the Assembly Registration tool (Regasm.exe) for XYZAssembly on the client computer to create the necessary COM registry entries and optional type library so that the COM client can instantiate the managed types.

More Questions from Microsoft Certification

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion