0

我编写了一个 C# 类库并想在 vbscript 中使用它。这是我的代码:

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(); 
        }
    }
}

构建后,我使用 RegAsm 但它抛出警告没有类型被注册。然后我在 vbs 中编写了一个示例代码,但它说 ActiveX 无法创建对象。

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

call main

有人说我必须检查 AssemblyInfo.cs 并确保我有

[assembly: ComVisible(true)]

我当然有,但问题仍然没有解决。谁能告诉我一个解决方案?


我像那样更改我的代码。更简单,没有线程,没有接口,都是公开的。但它仍然不起作用。拜托,我真的需要帮助。

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(); 
        }
    }
}
4

2 回答 2

2

不幸的是,您可行的解决方案相去甚远。警告是准确的,您在 [ComVisible] 上创建的任何类都不能由 COM 客户端创建。MyCashDrawer 缺少所需的默认构造函数,COM 客户端应用程序无法将参数传递给构造函数。VirtualManager 不是公共的,并且派生自一个不是 [ComVisible] 的接口

该代码还缺少使 ActiveX 组件在 ActiveX 主机窗口上工作的接口所需的实现,例如 IOleObject、IOleInPlaceObject、IOleInplaceActiveObject、IOleWindow、IViewObject 等。此外,您正在公开 ActiveX 对象无法处理的实现细节,COM 中的线程模型与 .NET 中的非常不同。

您将需要一种截然不同的方法。考虑从 System.Windows.Forms.Control 派生可见对象,它负责最低 ActiveX 接口实现要求。并让你的问题线程化,不要让客户来解决它。

于 2012-06-09T15:16:36.137 回答
0

regasm 警告不是问题。也许是因为 VirtualManager 类不是公开的。尝试将您的课程公开。

于 2012-06-09T14:34:00.837 回答