The Artima Developer Community
Sponsored Link

.NET Buzz Forum
COM wrappers in C# - the easy way

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eric Gunnerson

Posts: 1006
Nickname: ericgu
Registered: Aug, 2003

Eric Gunnerson is a program manager on the Visual C# team
COM wrappers in C# - the easy way Posted: Oct 1, 2004 9:32 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Eric Gunnerson.
Original Post: COM wrappers in C# - the easy way
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Gunnerson
Latest Posts From Eric Gunnerson's C# Compendium

Advertisement

Mihailik had a good suggestion for how to do COM interop in C# the the comments to a previous post that I'd like to share.

In many cases, TLBIMP produces a wrapper that can be used directly, and that's certainly the easiest way to do things.

But sometimes it doesn't make the right choice in how to express an interface. I ran into two situations yesterday:

In the first one, tlbimp produced an interface where an output string got the type of "ref ushort". In the second one, the designers of the interface decided that they would type a parameter as a byte* to point to lots of different kinds of data, and tlbimp typed that as "ref byte".

So, it meant I needed to tweak the parameters on the interface definitions. The hard way to do this is to write the interface definitions by hand. It's not much fun. Or, you can run ildasm, make the tweak, and then use ilasm. Also not a lot of fun.

The easy way - that Mihailik recommended - was to use Lutz Roeder's excellent .NET reflector, point it at the assembly that TLBIMP generated, and then use the disassembly feature of Reflector to get C# code. Paste the code into a source file, make the necessary tweaks, and Bob's your uncle.

With a few caveats...

First, the full richness of COM cannot be expressed in C#, so it's possible that you'll come across an interface where the C# version doesn't work correctly.

Second, you may come across some little thing's that don't quite mesh with C# syntax. I came across a case where an attribute wasn't legal in C#, and several where a derived interface restated the base interface's members (taken care of by adding a "new" to the derived interface definition).

Read: COM wrappers in C# - the easy way

Topic: Password Scrambler Previous Topic   Next Topic Topic: FlexWiki moves to SourceForge

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use