TheSwamp

Code Red => .NET => Topic started by: Draftek on September 09, 2005, 09:15:32 AM

Title: C# and Com Interop
Post by: Draftek on September 09, 2005, 09:15:32 AM
I have written a small xml config class to manipulate xml files similar to the old ini file usage.

I decided to try and expose this thru com interop so I could use it in some old vb or unmanaged c++ apps if needed.

After doing the following:
1. created a strong name using the sn utility.
2. added a Guid attribute to the public class
3. Changed the compile for com interop to 'true' in the visual studio configuration dialog

I'm able to add the class as a reference and use in a vb project correctly but - I can't see any of the methods or properties in intellisense or the object browser in vb. Almost like late binding is occuring or something.

Did I forget to do something?
Title: Re: C# and Com Interop
Post by: Glenn R on September 09, 2005, 10:04:56 PM
Just had a quick look thru a book I have here and all public types and type members will be visible to COM.

You can control this even further with the [ComVisible(false)] attribute.

Cheers,
Glenn.
Title: Re: C# and Com Interop
Post by: Draftek on September 12, 2005, 08:12:11 AM
Thanks Glenn, I read that too, and all the methods work, I just can't see them in the IDE.

Odd.
Title: Re: C# and Com Interop
Post by: MickD on September 12, 2005, 05:37:04 PM
This is only a guess but your assembly holds all your information about your application and I think your exported function names should appear in there so the clr can make them available/visible to other apps or COM (similar to a dll's exports). Do you have any export type attributes for all of your public functions?
Title: Re: C# and Com Interop
Post by: Glenn R on September 13, 2005, 07:50:54 AM
This should explain a few things:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp
Title: Re: C# and Com Interop
Post by: Glenn R on September 13, 2005, 07:55:20 AM
BTW Mick, in this case, the assembly does NOT hold all the info as it's for COM...it needs the .tlb registered as such...and a few more things as well.
COM is not for the faint hearted...glad it's dying and getting a LOT easier with .net, however there are still a few...steps shall we say...to go through.

Hope this helps.

Cheers,
Glenn.
Title: Re: C# and Com Interop
Post by: Draftek on September 13, 2005, 07:58:38 AM
oh geesh!, is that all I have to do. :wink:

Time to create an interface, I guess.

Thanks, I think that will take care of it.

I'll let you know.
Title: Re: C# and Com Interop
Post by: Glenn R on September 13, 2005, 08:15:35 AM
Forgot to mention, that if you can get your hands on the course material for last years AU, there are some examples of doing just this.

The course codes you're after are the CPxxxxx ones.

Cheers,
Glenn.
Title: Re: C# and Com Interop
Post by: Draftek on September 13, 2005, 08:22:53 AM
Worked Perfectly!

Just have to remember to use the interface in vb..

Thanks.
Title: Re: C# and Com Interop
Post by: MickD on September 13, 2005, 06:41:08 PM
BTW Mick, in this case, the assembly does NOT hold all the info as it's for COM...it needs the .tlb registered as such...and a few more things as well.
COM is not for the faint hearted...glad it's dying and getting a LOT easier with .net, however there are still a few...steps shall we say...to go through.

Hope this helps.

Cheers,
Glenn.

Thanks Glenn for the clarification and that link, I wasn't really sure and I forgot all about interfaces :( doh! I knew you had to expose it somewhere though :)
Title: Re: C# and Com Interop
Post by: Glenn R on September 14, 2005, 06:58:21 AM
Draftek,

Everything in COM is an interface and I mean everything...and VB uses COM, but it hides EVERYTHING about interfaces (paradoxical me thinks), as a lot of VBers are finding moving to .NET and C# in particular.
Glad I could help and it worked out.

No wuckers Mick  :-D

Cheers,
Glenn.