Groups | Blog | Home
all groups > dotnet interop > december 2007 >

dotnet interop : Adding COM support to my C# library


zorrothefox@rediffmail.com
12/11/2007 4:59:27 AM
Hi,
I have written a library in C# for doing some power
calculations. I wanted to add COM support to this library, so that
other applications/scripts could call this functionality.

I have seen a wizard for adding a new ATL/COM project in Visual Studio
6.0. However, this type of project seems to be missing in Visual
Studio 2005. Even otherwise, I need the project to be a C# project.

1) Is it possible to implement a COM library in C# ?
2) Is there any wizard which I can use to do the grunt work of
implementing COM in C#
3) Are there any good links on how to achieve this step-by-step?
4) If any books could be suggested on this topic, it'd be a great
help.

In case this is an inappropriate newsgroup for posting, please point
me to the right one.

Thanks in advance,
Paul.
Tony Gravagno
12/12/2007 11:13:27 AM
In addition to ticking that checkbox there are other considerations.
After reading about COM Shims, it seemed to me that was a good way to
go. Microsoft provides a Wizard that creates a bit of C++ COM code
along with some C# code, to proxy calls to your managed libraries.
See my blog for details of how to create one, and lots of references
to supporting information and code, especially by Andrew Whitechapel.
(Ignore the Excel details, that just happens to be my COM client.)
Unfortunately I haven't been able to get my code to deploy, but I'm
probably not doing something right with type libraries or the
registry, etc. If you do get this to deploy I'd be very interested in
details. Unfortunately no one in any forum or blog has responded to
any of my inquiries on this - it's quite disturbing.

Good luck
Tony Gravagno
Nebula Research and Development
remove.pleaseNebula-RnD.com/blog/tech/2007/11/excel-tools5.html

[quoted text, click to view]
Jigar Mehta
12/12/2007 11:53:26 AM
[quoted text, click to view]

You need to do following things,

1. make your assembly ComVisible
2. set the project property to "Register for COM Interop" OR manually
use regasm tool to register your assembly for COM interop.

if you face issues doing that, let me know, I have a sample showing a
Phill W.
12/12/2007 12:55:17 PM
[quoted text, click to view]

"Register for COM Interop" is the magic option you're looking for.
Tick the box and C# will generate you the required Type Library
automatically.

However...
I would think twice before /directly/ exposing your library to COM.
COM doesn't use Framework data types, so as soon as you start thinking
"expose this to COM", you have to start thinking "translate values
to/from COM data types" as you move arguments and results back and forth.

Personally, I would create a new dll that references and uses the "core"
library and expose /that/ to COM and have /that/ dll worry about any
translations that it may need to do. That way, your "core" library can
concentrate on those "power calculations" that /it/ does so well and the
"adapter" layer just has to worry about its /own/ translation efforts.

HTH,
AddThis Social Bookmark Button