tags:

views:

33

answers:

2

I am new to COM and need to add a Server COM object to my c# application so I can call its methods and implement events. The documentation I have says this requires a COM-aware language such as Visual C++. My app is written in C# so I'm not sure how this is going to work. Any direction would be appreciated.

I am writing an app that communicates with a serial hypercom terminal. The device came with a DLL (the com server interface) that I will need to figure out how to use in my c# application.

I added a reference to the DLL to my project, which shows up as a type library. In object explorer I get interfaces, and some classes etc.

Thanks, Matt

+1  A: 

You can add the COM object as a reference. .NET will create an interop assembly to work with the COM object, just like it was a .NET type.

Reed Copsey
Thanks, I added a reference to the DLL to my project, which shows up as a type library. In object explorer I get interfaces, and some classes etc. Now I just don't know how to use them really since the only sample code I have is in C++ and uses a class that inherits from CComObjectRoot.
@pkan522: You should have some objects you can instantiate in that library (which implement the interface(s) you need). Just construct them like any other .NET object.
Reed Copsey
Thanks Reed. I am heading in the right direction now.
A: 
  1. CComObjectRoot can be used as a base class for all COM objects implemented with ATL. As such you don't have to worry to implement something similar in C#, the required methods (AddRef, Release, QueryInterface) will be generated by tlbexp for classes that are tagged with ClassInterface.
  2. STDMETHODIMP is a define which serves to declare the most common methods that can be called from COM (#define STDMETHODIMP HRESULT STDMETHODCALLTYPE). Again if your class is tagged with ClassInterface you will not have to worry about.
  3. Such construction is required in C++ when your class implements several interfaces. I think this is not required if you tell C# compiler that your C# object implement IDispatch, IFPESOlementationEvents. The appropriate code will be written automatically by the compiler.

Probably everything will not make much sense if you are new to COM and C#, I'll suggest to take a look at the various introduction that you may find on the web, like this.

Ismael