Solved

Create COM Object In C#

Posted on 2004-08-05
4
1,244 Views
Last Modified: 2007-12-19
Hi!
I'm new in C#.
I need to create a com object or some type of library that I can use in both VB 6.0 an MS Dot Net.
How can I do this?

I'll thank any help.

Jorge
0
Comment
Question by:jorgearias
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 250 total points
ID: 11727127
Hi Jorge, this is fairly easy and you can get some good information on this on the MSDN site and in the book "Inside C#" by Tom Archer and Andrew Whitechapel.  Here is the short of it.

1) Create a project that will compile into a dll.

2) Create an interface that has all the function that you need to be available through COM.  i.e.

    /// <summary>
    /// Interface for use with com tlb...
    /// </summary>
    [GuidAttribute("5E8D6DFC-6A2F-3AA0-A7B9-25CDBE07CEEA"),
     InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ImfoAdapter : IDisposable {
        bool PostStatus(MFOAdapterStatus status, string strStatus);
        void Setup( string Machine, string AppName, bool createInputQ );
        new void Dispose();
        string LastOutput { get; }
        string AppPath { get; set; }
        string AppSystem { get; set; }
        int AppFrequency { get; set; }
        int AppTolerance { get; set; }
    }

2) Add the attribute [InterfaceType(ComInterfaceType.InterfaceIdIDispatch)] to the interface.  Note: there are other types but this is the one I used.

3) To help avoid DLL HELL add the attribute [GuidAttribute("5E8D6DFC-6A2F-3AA0-A7B9-25CDBE07CEEA")], of course replace the guid with one that you create.

4) Create a class that implements your interface. i.e.

    [GuidAttribute("2ED2E475-BC29-3CB6-ABD4-FADD80B88590"),
     ClassInterface(ClassInterfaceType.None)]
    public class AdapterBase : ImfoAdapter {
        // Implement Interface and what ever other code you want.
    }

5) Add the attribute [ClassInterface(ClassInterface.None)] to make it so the class will NOT be visible through COM.  I do this as otherwise I ran into issues and this seems the easiest.

6) Again, to help avoid DLL HELL add the attribute [GuidAttribute("2ED2E475-BC29-3CB6-ABD4-FADD80B88590")],  of course replace the guid with one that you create.

7) Compile your class into a dll.

8) run regasm on the dll.  i.e.

   'Unregister prior install
   regasm MFO_Adapters.dll /u
   'Register new type library
   regasm MFO_Adapters.dll /tlb /codebase

you can do regasm /? to see what else is possible.

9) There you go... you can now start using your DotNET dll in VB6 as a COM object.

Hope this helps.
nipnfriar_tuck
0
 
LVL 5

Expert Comment

by:jjacksn
ID: 11730397
ANY COM object can be used in .NET through interop.  and, I believe any COM object can be used in VB6 as well.  So, to answer your question, you want to create a COM object.  Whether you do that in .NET (in C#) or C++ or VB, it will still work with everything so long as it is a COM object.
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11730862
True, most any COM object can be used in VB6 or C#... However, how you get there is different.  For instance in C# to use a COM object you have to run TLBIMP to import the COM tlb into a dll that DotNET can use.  Once this is done the COM object can be used.

So, if writing new classes that need to be used from both VB6 and DotNET why not use C# to write them?  Create an interface, implement the interface, use the attributes that come with DotNET, run regasm and you have a class that can be used in DotNET as normal or as a COM object.

Just a thought,
nipnfriar_tuck
0
 
LVL 5

Expert Comment

by:jjacksn
ID: 11731809
I agree, I was just clarifying :)

If you don't know c#, it isn't neccessarily worth learning just for this.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now