Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create COM Object In C#

Posted on 2004-08-05
4
Medium Priority
?
1,298 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 750 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

782 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