Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create COM Object In C#

Posted on 2004-08-05
4
Medium Priority
?
1,286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

722 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