需要用 C# 写一个 DLL 文件传一个 event 给 VB6.
DLL要怎么写?(我知道一定能做到,就是不知道要怎么写)

解决方案 »

  1.   

    C# 编写的 DLL 不是 Regular DLL,因此无法被 VB6 直接调用。有以下两种解决办法:
    1、将 C# 编写的组件实现为 COM 组件(使用 ComVisible 属性),VB6 调用该 COM 组件;
    2、升级你的 VB6 到 VB.NET,然后直接使用 C# 组件。
      

  2.   


    之前是用的[ComVisible(true)],VB6就调用的COM组件
    event 我不是很懂,只知道个大概,要写出来就不行,非常希望做个非常简单的DLL传event到VB6的例子看看!
      

  3.   

    首先应声明一个托管接口:[ComVisible(true)] 
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] // 托管接口必须是 IDispatch
    public interface IMathEvents 
    {
        // 声明支持的事件
        void OnOperationCompleted(string message);
    }
    然后定义一个实现该接口的类:[ClassInterface(ClassInterfaceType.None)] 
    [ComSourceInterfaces(typeof(IMathEvents))]
    public class Math : ServicedComponent , IMath 
    {
        public int Add(int x, int y) 
        {
            if (null != this.OnOperationCompleted) 
            {
                // 引发事件
                this.OnOperationCompleted("Operation completed");
            } 
            return x + y;
        }    [ComVisible(false)]
        public delegate void OperationCompletedDel(string message); 
        public event OperationCompletedDel OnOperationCompleted;
    }