![]() |
Languages »
C# »
General
Intermediate
Accessing COM+ component using C#By Imtiaz AlamThis article provides a step by step explanation on how to access COM+ applications using C#. |
C#, Windows, .NET 1.0, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This article explains step by step of accessing COM+ application using C#. The code is compiled using beta2. Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]. It can be used with Beta1 with some minor modification.
If we want need to access the existing COM+ application with any .net supported language, we don't need to modify the single line of existing COM+ application, despite of the fact their execution model is completely different. Here is the step-by-step easy example of doing the same. We can access it in two ways, Early Binding and Late binding (remembered the old VB concepts). We will first look at Early binding example...
TlbImp.exe: (Type Library
Importer) Imports
(converts, generates wrapper DLL) the type definition found in COM component into equivalent .net
definitions (or metadata), understandable by
Common Language Runtime (CLR). The metadata generated, by TlbImp can be viewed
by Ildasm.exe. If you are using the Visual Studio development environment, you only need to add
a reference to the COM type library and the conversion is done automatically.
To use existing COM+ application we need create Runtime Callable Wrapper (RCW) using TlbImp.Exe, with VS.Net it is created automatically,
when we reference the existing COM Component. The difference is that, VS.Net create the RCW with the same name as the original DLL, which may
be confusing, and with TlbImp.Exe, we can specify the different name using /out: parameter.

Let us assume that we have a COM Component with a method Add, which takes two parameter A and B and returns the Sum.
Note: We have to register COM DLL first before using it...'CompAdd.Dll (class1) Public Function Add(A As Long, B As Long) As Long Add = A + B End Function
Now we will run TlbImp.Exe in our existing DLL i.e. CompAdd.Dll
This generated a wrapper dll CompAddRcw.dll. we can view this DLL using IlDasm.Exe
The discussion in IlDasm.Exe is beyond the scope of this article. Now
we will simply write code to use the Wrapper DLL, which
in turn will be calling the actual DLL. Now let us look into the C# code
for doing it.
As we can see that we are getting the same kind of
tooltip as we used to get with VB when we do early binding. Compile the program with /r:
switch e.g.csc TestClient.cs /r:CompAddRcw.dll. Execute it and
we just called COM component with .net.
System.Reflection namespace, which allows us
to programmatically access the types contained in any assembly. We will see how
we can do late bindings in four easy steps..
IDispatch Interface using Type.GetTypeFromProgID("Project1.Class1")
Activator.CreateInstance(objAddType)
objAddType.InvokeMember function.
The method Type.GetTypeFromProgID is used to load the type information of the COM
object. The call to Activator.CreateInstance returns an instance of the COM object. And Finally InvokeMember function is
used to call the Method of COM object.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 30 Oct 2001 Editor: Chris Maunder |
Copyright 2001 by Imtiaz Alam Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |