3

I want to create a VS2010 c# class that can be used in a Vb6 project. I've done simple C# class below and ticked the "register for com interop" in the build properties. In the Vb6 project I can see a reference for ComTestC but when I run the vb6 code I get:

run-time error '429' ActiveX component can't create object

I'm I missing an obvious step to getting this Com object working?

Vb6 Code

Private Sub Command1_Click()

Dim foo As Object
Set foo = CreateObject("ComTestC.Numbers")

End Sub

C# Code

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

namespace ComTestC
{
    public class ComTestC
    {

        [Guid("8b8d1e17-fc8e-4316-afb7-394a5da56801")]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface _Numbers
        {
            [DispId(1)]
            int GetDay();

        }

        [Guid("68d6c981-66dd-4731-93a0-2c39bd86495f")]
        [ClassInterface(ClassInterfaceType.None)]
        [ProgId("ComTestC.Numbers")]
        public class Numbers : _Numbers
        {
            public Numbers() { }

            public int GetDay()
            {
                return (DateTime.Today.Day);
            }

        }


    }
}
paul rockerdale
  • 377
  • 6
  • 21
  • Did you run regsvr32? – Mike Cheel Dec 26 '13 at 15:03
  • 2
    I have never done this before, but am sure that you need to set `ComVisible(true)` to expose the class to COM – Sriram Sakthivel Dec 26 '13 at 15:03
  • 2
    Project + Properties, Application tab, Assembly Information button, tick the "Make assembly COM-Visible" option. You're likely to get a compile error next, you have to run VS elevated so the class can be registered. – Hans Passant Dec 26 '13 at 15:22
  • You code workes fine for me. As Hans Passant said, "Make assembly COM-Visible" option is neccessary ... – Daniel Dušek Dec 26 '13 at 16:28
  • To clarify @SriramSakthivel's comment... You only need `ComVisible(true)` on the assembly and not on the class. Classes are visible to COM by default, as long as the assembly is properly marked. See http://stackoverflow.com/questions/15688395/whats-the-deal-with-comvisible-default-and-public-classes-com-exposure. – Holistic Developer Dec 26 '13 at 20:17
  • 1
    Thanks guys by default "Make assembly COM-Visible" was false I changes this to true and all is fine thanks – paul rockerdale Dec 26 '13 at 21:59

1 Answers1

1

Exposing .NET Components to COM

Mehdi Esmaeili
  • 828
  • 1
  • 11
  • 15