RunXin Shirley
RunXin Shirley

Reputation: 31

How to implement using COM interface in Communication between NET and C++

I have a NET Core DLL as below.

using System.Runtime.InteropServices;

namespace ProxyClient
{
    [ComVisible(true)]
    [Guid("B611D722-8C7E-4F2A-8FBD-36617A49CE90")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IProxyComClient
    {
        bool BindDetectorAccess(IntPtr bAccess);
        bool StartConnection(string url);
        bool StopConnection();
        bool SendMessage(byte[] message);
        uint GetHubConnectionState();
    }
    [ComVisible(true)]
    [Guid("0017C04E-0B72-49DE-B07B-0BCAD50851DE")]
    [ComDefaultInterface(typeof(IProxyComClient))]
    public class ProxyComClient : IProxyComClient, IDisposable
    {
        private bool _disposed = false;
        private ProxyClient? _proxyClient;
        ~ProxyComClient()
        {
            Dispose(false);
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    StopConnection();
                }
                _disposed = true;
            }
        }
        public bool BindDetectorAccess(IntPtr detectorAccess)
        {
            if (detectorAccess == IntPtr.Zero)
            {
                return false;
            }

            if (_proxyClient != null)
            {
                return true;
            }
            _proxyClient = new ProxyClient();
            return true;
        }
        public bool StartConnection(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return false;
            }

            if (_proxyClient != null)
            {
                return true;
            }

            return _proxyClient.HubConnection_StartAsync(url).GetAwaiter().GetResult();
        }
        public bool StopConnection()
        {
            if (_proxyClient == null)
            {
                return false;
            }
            return _proxyClient.HubConnection_StopAsync().GetAwaiter().GetResult();
        }
        public bool SendMessage(byte[] message)
        {
            if (message.Length == 0)
            {
                return false;
            }

            if (_proxyClient == null)
            {
                return false;
            }
            return _proxyClient.SendMessageToServer(message).GetAwaiter().GetResult();
        }
        public uint GetHubConnectionState()
        {
            if (_proxyClient == null)
            {
                return (uint)HubConnectionState.Disconnected;
            }
            return (uint)_proxyClient.GetHubConnectionState();
        }
    }
}

And a C++ ATL application with COM Interface

[
    object,
    uuid(a297a0b3-2655-47dc-9295-7e82af09f2c7),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface IClient : IDispatch
{
    [id(1)] HRESULT Send([in] SAFEARRAY(BYTE) message);
};

My scenario is using ATL application to host NET DLL, ATL application can run multiple instances simultaneously on the same machine, I want to communication between ATL application and NET DLLs through the above COM interfaces.

My question is how to call the COM interface of ATL in NET DLL to make sure the NET DLL return the message to its host's ATL application.

I have defined a BindDetectorAccess function with an IntPtr parameter. Do I need to create a C++ COM object in ATL and pass it to NET? If so, how to convert this IntPtr to COM type of ATL in .NET?

Upvotes: 0

Views: 56

Answers (0)

Related Questions