Solved

Create COM Object In C#

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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