Solved

Create COM Object In C#

Posted on 2004-08-05
4
1,245 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

15 Experts available now in Live!

Get 1:1 Help Now