Skip to content

Support for TypeLibs needed for assembly consumption as VBA references #3740

@robert-io

Description

@robert-io

Steps to reproduce

Create a .Net Core Class Library and set the Target Framework to .NET Core 3.0 and add the following code (setting you own GUIDs) in the PersonRegistration class:

using System;
using System.Runtime.InteropServices;

namespace CoreComTest {
    internal class PersonRegistration {
        public const string IID_IPerson = "########-####-####-####-############";
        public const string IID_IBlagger = "########-####-####-####-############";
        public const string IIE_IPerson = "########-####-####-####-############";
        public const string CLSID_Person = "########-####-####-####-############";
        public const string PROGID_Person = "CoreComTest.Person";
    }

    [ComVisible(true)]
    [Guid(PersonRegistration.IID_IPerson)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IPerson {
        [DispId(100)]
        string Name { get; set; }

        [DispId(200)]
        DateTime BornOn { get; set; }

        [DispId(300)]
        int GetAge();

        [DispId(400)]
        IPerson GetMate();
    }

    [ComVisible(true)]
    [Guid(PersonRegistration.IIE_IPerson)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IPersonEvents {
        [DispId(100)]
        void NameChanged(string original_name, string new_name);
    }

    public delegate void NameChangedDelegate(string original_name, string new_name);

    [ComVisible(true)]
    [Guid(PersonRegistration.IID_IBlagger)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IBlagger {
        [DispId(100)]
        string Blag();
    }

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces(typeof(IPersonEvents))]
    [Guid(PersonRegistration.CLSID_Person)]
    [ProgId(PersonRegistration.PROGID_Person)]
    public class Person : IPerson, IBlagger {
        private string _name = string.Empty;

        public string Name {
            get => _name;
            set {
                if (_name != value) {
                    var old_value = _name;
                    _name = value;
                    OnNameChanged(old_value, _name);
                }
            }
        }
        public DateTime BornOn { get; set; } = DateTime.MinValue;

        public int GetAge() {
            return (int)((DateTime.Now - BornOn).TotalDays / 365);
        }

        public void OnNameChanged(string original_name, string new_name) {
            var callback = NameChanged;
            try {
                callback?.Invoke(original_name, new_name);
            }
            catch {
                /* Todo: Log */
            }
        }

        public string Blag() {
            return $"{Name} is the best";
        }

        public IPerson GetMate() {
            var rnd = new Random(42);
            var years = rnd.Next(20, 50);
            var days = rnd.Next(1, 356);

            return new Person() {
                Name = "John Smith",
                BornOn = DateTime.UtcNow.AddYears(-years).AddDays(-days)
            };
        }

        public event NameChangedDelegate NameChanged;
    }
}

Desired behavior

Using VBA or VBScript create and use the object. For example the following VBS should create the object and show the persons age in a message box:

Dim sam 'As CoreComTest.Person
Set sam = CreateObject("CoreComTest.Person")

rob.Name = "Sam Carter"
rob.BornOn = dotnet/core-setup#29 December 1968#

Call MsgBox(rob.GetAge())

Actual behavior

A Windows Script Host error box with the following error is displayed:
~ ActiveX component can't create object

Environment data

Windows 10
VS 2019 with the .NET Core 3.0 Preview 8 SDK

Additional

I can see from the warnings in my code that ComInterfaceType.InterfaceIsDual & ComSourceInterfaces are marked obsolete.

~ Will Microsoft be adding support for ComInterfaceType.InterfaceIsDual & ComSourceInterfaces?
~ Or is there another way to implement a scriptable (IDispatch) COM object?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions