Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: C# dll usedin COM environment  (Read 4004 times)

0 Members and 1 Guest are viewing this topic.

dbran

    Topic Starter


    Rookie
    • Experience: Experienced
    • OS: Windows 7
    C# dll usedin COM environment
    « on: April 12, 2012, 11:53:53 AM »
    I know, I know...  The web is full of posts similar to this.  But none of them really give me an answer to my question so I figured one more wouldn't hurt...

    I have a dll that I have written in C# using the 2.0 .NET framework under VS2010.  Inside the project properties I have set Register for COM Interop to true (Checked the checkbox). I created a class and an interface to define the functionality.

    The interface:
    Code: [Select]

    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Text;

    namespace MyClassLibrary
    {
        [Guid("29C614FE-6DA1-4817-A111-B483D710D661")]
        [ComVisible(true)]
        interface IMyClassLibrary
        {
            string goodByeStr
            {
                get;
            }

            string SayHello();
        }
    }


    The class:
    Code: [Select]

    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Text;

    namespace MyClassLibrary
    {
        [Guid("2C87C0CE-47E9-4C89-A553-E10E1F4FC8B8")]
        [ProgId("MyClassLibrary.MyClassLibrary")]
        [ClassInterface(ClassInterfaceType.None)]
        [ComVisible(true)]
        public class MyClassLibrary : IMyClassLibrary
        {
            // Default Constructor.
            public MyClassLibrary()
            {

            }

            // Implement interface property.
            public string goodByeStr
            {
                get { return "Goodbye!"; }
            }

            // Implement interface method.
            public string SayHello()
            {
                return "Hello!";
            }
        }
    }


    And the Assembly file:
    Code: [Select]

    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    // General Information about an assembly is controlled through the following
    // set of attributes. Change these attribute values to modify the information
    // associated with an assembly.
    [assembly: AssemblyTitle("MyClassLibrary")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("")]
    [assembly: AssemblyProduct("MyClassLibrary")]
    [assembly: AssemblyCopyright("Copyright ©  2012")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]

    // Setting ComVisible to false makes the types in this assembly not visible
    // to COM components.  If you need to access a type in this assembly from
    // COM, set the ComVisible attribute to true on that type.
    [assembly: ComVisible(false)]

    // The following GUID is for the ID of the typelib if this project is exposed to COM
    [assembly: Guid("6191a078-1d98-4a49-9c0f-7027ab71824c")]

    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]


    This is pretty basic stuff as far as creating a COM Interop goes.  When I build this, I get a .tlb file that I can access inside a VB6 form application.  However, when I use the object viewer in VB6 to look at the methods and properties, the only ones visible are the ones inherited from mscorlib.dll.  The two public objects from the class are not visible.  I am still not sure why VS2010 does not recognize them as types that can be registered for COM interop.  The resulting TLB is shown below...

    Code: [Select]

    // Generated .IDL file (by the OLE/COM Object Viewer)
    //
    // typelib filename: MyClassLibrary.tlb

    [
      uuid(6191A078-1D98-4A49-9C0F-7027AB71824C),
      version(1.0)
    ]
    library MyClassLibrary
    {
        // TLib :     // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
        importlib("mscorlib.tlb");

        // Forward declare all types defined in this typelib

        [
          uuid(2C87C0CE-47E9-4C89-A553-E10E1F4FC8B8),
          version(1.0),
            custom({0F21F359-AB84-41E8-9A78-36D110E6D2F9}, "MyClassLibrary.MyClassLibrary")
        ]
        coclass MyClassLibrary {
            [default] interface _Object;
        };
    };


    Any thoughts on this?

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: C# dll usedin COM environment
    « Reply #1 on: April 12, 2012, 02:27:44 PM »
    well, ComVisible is set to false in the Assembly, and that would make the types in the assembly invisible.
    I was trying to dereference Null Pointers before it was cool.