Click here to Skip to main content
15,868,096 members
Articles / Programming Languages / C#
Article

Accessing COM+ component using C#

Rate me:
Please Sign up or sign in to vote.
4.75/5 (13 votes)
30 Oct 20013 min read 160.9K   1.5K   31   18
This article provides a step by step explanation on how to access COM+ applications using C#.

Summary

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...

Early Binding

Tools Required

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.

Let's Start

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.

Image 1

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

Image 2

This generated a wrapper dll CompAddRcw.dll. we can view this DLL using IlDasm.Exe

Image 3

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.

Image 4

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.

Late Binding

For Late binding we have to use 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..
  • We have Get IDispatch Interface using Type.GetTypeFromProgID("Project1.Class1")
  • We have to create instance using the type ID Activator.CreateInstance(objAddType)
  • We have to make array of arguments (if required)
  • Invoke the Method using objAddType.InvokeMember function.

 

Image 5

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.

Conclusion

This article gives very basic idea of using the old COM Application, for the purpose of simplicity and to focus on the purpose of the article no error handling has been done. In my next article we will see how can we use .NET component with VB.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalvalue cannot be null. Pin
jimkqui25-Sep-08 19:54
jimkqui25-Sep-08 19:54 
GeneralRe: value cannot be null. Pin
jjjkkk25-Sep-08 23:47
jjjkkk25-Sep-08 23:47 
GeneralCOM Exception Pin
androjdaz27-May-08 20:35
androjdaz27-May-08 20:35 
GeneralRe: COM Exception Pin
Kipetcoff6-Aug-08 6:38
Kipetcoff6-Aug-08 6:38 
GeneralRe: COM Exception Pin
jimkqui25-Sep-08 19:54
jimkqui25-Sep-08 19:54 
QuestionWhat about calling COM+ on another server? Pin
Dan Vallejo10-Oct-07 6:38
Dan Vallejo10-Oct-07 6:38 
AnswerRe: What about calling COM+ on another server? Pin
jjjkkk26-Sep-08 12:40
jjjkkk26-Sep-08 12:40 
GeneralRe: What about calling COM+ on another server? Pin
ejojart20-Mar-09 5:07
ejojart20-Mar-09 5:07 
GeneralThank you Pin
Hez26-Apr-06 0:50
Hez26-Apr-06 0:50 
Thank you so much for writing this article, I am sure the information is available elsewhere but not in such a concise and easily readable manner. At the end of the day, I applaud anyone on Code Project for sharing the information they have already learned and making someone else's life a little easier.

Ignore the ignorant people who appear to know everything! Some people like the sound of their own messages.

Wink | ;)
GeneralNeed help! Pin
lily0lin11-Oct-05 6:51
lily0lin11-Oct-05 6:51 
GeneralRe: Need help! Pin
jimkqui25-Sep-08 19:56
jimkqui25-Sep-08 19:56 
GeneralSimple and Concise Pin
mcintyak7-Oct-05 17:36
mcintyak7-Oct-05 17:36 
QuestionWhy was this dumb article written ? Pin
19-Feb-02 21:02
suss19-Feb-02 21:02 
AnswerFor people to learn I guess :-) Pin
2-Jul-02 0:54
suss2-Jul-02 0:54 
AnswerRe: Why was this dumb article written ? Pin
Jamie Nordmeyer16-Dec-02 9:45
Jamie Nordmeyer16-Dec-02 9:45 
GeneralThis is a dumb article Pin
thread_moniter10-Jun-03 23:12
thread_moniter10-Jun-03 23:12 
GeneralRe: This is a dumb article Pin
normanzb19-Sep-07 22:20
normanzb19-Sep-07 22:20 
AnswerRe: Why was this dumb article written ? Pin
xavitomorera16-Jun-04 14:11
xavitomorera16-Jun-04 14:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.