简体   繁体   中英

Wrap C# code as ActiveX

I write a C# class library and want to use it in vbscript. Here is my code:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.PointOfService;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Reflection;
using System.ComponentModel;

namespace IndigoDynamic
{
    #region class implements IAsyncResult
    [ProgId("IndigoDynamic.VirtualManager")]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComVisible(true)]
    public class AsyncResult : IAsyncResult
    {
        object _state;
        private bool m_completed;
        private System.Threading.ManualResetEvent m_handle;
        private Exception m_exception;

        public bool IsCompleted
        {
            get { return m_completed; }
            set { m_completed = value; }
        }

        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get { return m_handle; }
            set { m_handle = (System.Threading.ManualResetEvent)value; }
        }

        public object AsyncState
        {
            get
            {
                if (Exception != null)
                {
                    throw Exception;
                }
                return _state;
            }
            internal set
            {
                _state = value;
            }
        }

        public bool CompletedSynchronously { get { return IsCompleted; } }

        internal Exception Exception
        {
            get { return m_exception; }
            set { m_exception = value; }
        }
    }
    #endregion

    #region extends CashDrawer
    [ProgId("IndigoDynamic.VirtualManager")]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComVisible(true)]
    public class MyCashDrawer
    {
        private CashDrawer me;
        public delegate void status_callback(int newstatus);

        private status_callback myF;

        public MyCashDrawer(CashDrawer param)
        {
            me = param;
            me.StatusUpdateEvent += new StatusUpdateEventHandler(this.StatusUpdate);
        }

        [ComVisible(true)]
        public void Claim(int timeout) { me.Claim(timeout); }

        [ComVisible(true)]
        public void Close() { me.Close(); }

        [ComVisible(true)]
        public void Open() { me.Open(); }

        [ComVisible(true)]
        public void OpenDrawer() { me.OpenDrawer(); }

        [ComVisible(true)]
        public void Release() { me.Release(); }

        [ComVisible(true)]
        public void Release(int timeout, int freq, int duration, int delay)
        {
            me.WaitForDrawerClose(timeout, freq, duration, delay);
        }

        [ComVisible(true)]
        public int StatusClosed() { return CashDrawer.StatusClosed; }

        [ComVisible(true)]
        public int StatusOpen() { return CashDrawer.StatusOpen; }

        [ComVisible(true)]
        public bool Claimed() { return me.Claimed; }

        [ComVisible(true)]
        public bool DeviceEnabled() { return me.DeviceEnabled; }

        [ComVisible(true)]
        public bool DrawerOpened() { return me.DrawerOpened; }

        [ComVisible(true)]
        public ControlState State() { return me.State; }

        [ComVisible(true)]
        public void addStatusCallback(status_callback f)
        {
            myF = f;
        }

        [ComVisible(true)]
        public void removeStatusCallback(status_callback f)
        {
            if (myF == f)
                myF = null;
        }

        [ComVisible(true)]
        private void StatusUpdate(object sender, StatusUpdateEventArgs arg)
        {
            if (myF != null)
                myF(arg.Status);
        }
    }
    #endregion

    [ProgId("IndigoDynamic.VirtualManager")]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComVisible(true)]
    class VirtualManager : ISynchronizeInvoke
    {
        private readonly object _sync;

        // Constructor
        public VirtualManager()
        {
            _sync = new object();
        }

        #region implements methods of ISynchronizeInvoke
        public IAsyncResult BeginInvoke(Delegate method, object[] args) {
            AsyncResult result = new AsyncResult();

            System.Threading.ThreadPool.QueueUserWorkItem(delegate {
                result.AsyncWaitHandle = new System.Threading.ManualResetEvent(false);
                try {
                    result.AsyncState = Invoke(method, args);
                } catch (Exception exception) {
                    result.Exception = exception;
                }
                result.IsCompleted = true;
            });

            return result;
        }

        public object EndInvoke(IAsyncResult result) {
            if (!result.IsCompleted) {
                result.AsyncWaitHandle.WaitOne();
            }

            return result.AsyncState;
        }


        public object Invoke(Delegate method, object[] args) {
            lock (_sync) {
                return method.DynamicInvoke(args);
            }
        }

        public bool InvokeRequired {
            get { return true; }
        }
        #endregion

        [ComVisible(true)] 
        public MyCashDrawer getCashDrawer()
        {
            PosExplorer posExplorer = new PosExplorer(this);
            DeviceInfo deviceInfo = posExplorer.GetDevice(DeviceType.CashDrawer);
            if (deviceInfo == null)
            {
                //<report failure >
                return null;
            }
            else
            {
                CashDrawer cd = posExplorer.CreateInstance(deviceInfo) as CashDrawer;

                return new MyCashDrawer(cd);
            }
        }

        [ComVisible(true)]
        public MyCashDrawer getCashDrawer(String name)
        {
            PosExplorer posExplorer = new PosExplorer(this);
            DeviceInfo deviceInfo = posExplorer.GetDevice(DeviceType.CashDrawer, name);
            if (deviceInfo == null)
            {
                //<report failure >
                return null;
            }
            else
            {
                CashDrawer cd = posExplorer.CreateInstance(deviceInfo) as CashDrawer;
                return new MyCashDrawer(cd);
            }
        }

        [ComRegisterFunction()]
        public static void RegisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            RegistryKey ctrl = k.CreateSubKey("Control");
            ctrl.Close();

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
            inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
            inprocServer32.Close();

            k.Close();
        }

        [ComUnregisterFunction()]
        public static void UnregisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            if (k == null)
            {
                return;
            }
            k.DeleteSubKey("Control", false);

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
            inprocServer32.DeleteSubKey("CodeBase", false);
            inprocServer32.Close();
            k.Close(); 
        }
    }
}

After building, I use RegAsm but it threw Warning No types are registered. Then I write a sample code in vbs but it says ActiveX can not create Object.

Sub main
    set objTest = CreateObject("IndigoDynamic.VirtualManager")
end sub

call main

Somebody said that I have to check AssemblyInfo.cs and make sure I have

[assembly: ComVisible(true)]

Of course I have, but problem is still not solved. Can anybody tell me a solution?


I change my code like that. More simple, no threading, no interface, all is public. But it still doesn't work. Please, I really need help.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.PointOfService;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Reflection;
using System.ComponentModel;

namespace IndigoDynamic
{            
    [ProgId("IndigoDynamic.VirtualManager")]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComVisible(true)]
    public class VirtualManager
    {   
        public VirtualManager()
        {
        }

        [ComVisible(true)] 
        public CashDrawer getCashDrawer()
        {
            PosExplorer posExplorer = new PosExplorer();
            DeviceInfo deviceInfo = posExplorer.GetDevice(DeviceType.CashDrawer);
            if (deviceInfo == null)
            {
                //<report failure >
                return null;
            }
            else
            {
                CashDrawer cd = posExplorer.CreateInstance(deviceInfo) as CashDrawer;
                return cd;
            }
        }

        [ComVisible(true)]
        public CashDrawer getCashDrawer(String name)
        {
            PosExplorer posExplorer = new PosExplorer();
            DeviceInfo deviceInfo = posExplorer.GetDevice(DeviceType.CashDrawer, name);
            if (deviceInfo == null)
            {
                //<report failure >
                return null;
            }
            else
            {
                CashDrawer cd = posExplorer.CreateInstance(deviceInfo) as CashDrawer;
                return cd;
            }
        }

        [ComRegisterFunction()]
        public static void RegisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            RegistryKey ctrl = k.CreateSubKey("Control");
            ctrl.Close();

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
            inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
            inprocServer32.Close();

            k.Close();
        }

        [ComUnregisterFunction()]
        public static void UnregisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            if (k == null)
            {
                return;
            }
            k.DeleteSubKey("Control", false);

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
            inprocServer32.DeleteSubKey("CodeBase", false);
            inprocServer32.Close();
            k.Close(); 
        }
    }
}

You are unfortunately very far removed from a workable solution. The warning is accurate, none of the classes you made [ComVisible] at creatable by a COM client. MyCashDrawer is missing the required default constructor, a COM client app cannot pass arguments to a constructor. VirtualManager isn't public and derives from an interface that's not [ComVisible]

The code is also missing the required implementations for interfaces that makes an ActiveX component work on a ActiveX host window, like IOleObject, IOleInPlaceObject, IOleInplaceActiveObject, IOleWindow, IViewObject, etcetera. Furthermore, you are exposing implementation details that an ActiveX object can't take care of, the threading model in COM is very different from the one in .NET.

You are going to need a seriously different approach. Consider deriving the visible object from System.Windows.Forms.Control, it takes care of the minimum ActiveX interface implementation requirements. And make threading your problem, don't leave it up to the client to sort it out.

The regasm warning isn't the problem. Maybe it's beacause VirtualManager class isn't public. Try to expose your class as public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM