VB6调用C#COM,字符串及对象数组的ref传值传址

值类型可以直接传值。 复杂类型只能ref传址或者用Variant包装,C#用object接收再解包。

传址就要注意内存分配位置了。 

VB6调用C#对象,那就不能预定义收接对象的长度。

VB6传值就容易一些, 用Variant包装 

VB6Project下载

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace TestEntityClass {

    [ComVisible(true)]
    [Guid("825B0AC6-E22C-476A-955F-758FDD0D49BE")]
    public interface IClassTest {
        /// <summary>
        /// 第三方调用测试
        /// </summary>
        /// <param name="msg">回传字符串</param>
        /// <returns>true成功</returns>
        bool Hello(ref string msg);

        /// <summary>
        /// 第三方调用测试
        /// </summary>
        /// <param name="msg">回传字符串</param>
        /// <returns>true成功</returns>
        bool VBSendArray(string[] strs, ref string msg);

        bool VBGetArray(ref string[] strs);

        bool TestClass(ref TestValue[] tv);

        bool VBSendObj(ref object obj, ref string msg);

        bool VBGetObj(ref object obj);

        TestValue CTestValue(object obj);
        TestValueList CTestValueList(object obj);
    }



    [ComVisible(true)]
    [Guid("1A17E11B-39CC-43B5-A780-874F0DE0DD48")]
    public class ClassTest : IClassTest {

        /// <summary>
        ///Private Sub Command1_Click()
        ///    Dim s As String
        ///     s = "Hi"
        ///    Call vbt.Hello(s)
        ///    MsgBox(s)
        ///End Sub
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Hello(ref string msg) {
            string s = msg;
            msg = $"VB Input:\t\t{s}\r\nC# Output:\tHello,{s}!";
            return true;
        }

        /// <summary>
        ///Private Sub Command2_Click()
        ///    Dim s As String
        ///    Dim ss(4) As String
        ///    ss(0) = "bar0"
        ///    ss(1) = "bar1"
        ///    ss(2) = "bar2"
        ///    ss(3) = "bar3"
        ///    ss(4) = "bar4"
        ///     s = "Hi"
        ///    Call vbt.VBSendArray(ss, s)
        ///    MsgBox(s)
        ///End Sub
        /// </summary>
        /// <param name="strs"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool VBSendArray(string[] strs, ref string msg) {
            StringBuilder sb = new StringBuilder();
            foreach (string s in strs) sb.Append($"{s},");
            msg = sb.ToString();
            return true;
        }

        /// <summary>
        ///Private Sub Command4VBRefStringArray_Click() 
        ///    Dim s As String
        ///    Dim ss() As String
        ///    Call vbt.VBGetArray(ss)
        ///    MsgBox(ss(1))
        ///End Sub
        /// </summary>
        /// <param name="strs"></param>
        /// <returns></returns>
        public bool VBGetArray(ref string[] strs) {
            strs = new string[] { "test0", "test1", "test2", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), };
            return true;
        }

        /// <summary>
        ///Private Sub Command3_Click()
        ///    Dim s As String
        ///    Dim ss() As String
        ///    ss = vbt.VBGetArray2
        ///    MsgBox(ss(0))
        ///End Sub
        /// </summary>
        /// <returns></returns>
        public string[] VBGetArray2() {
            return new string[] {
                "test20", "test21", "test22", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
            };
        }


        /// <summary>
        ///Private Sub Command5_Click()
        ///    Dim by0(1) As Byte
        ///        by0(0) = 12
        ///        by0(1) = 13
        ///    Dim by1(1) As Byte
        ///        by1(0) = 22
        ///        by1(1) = 23
        ///    Dim tl0 As New TestValueList
        ///        With tl0
        ///            .cGUID = "TestValueListcGUID0"
        ///            .Value5 = by0
        ///        End With
        ///    Dim tl1 As New TestValueList
        ///        With tl1
        ///            .cGUID = "TestValueListcGUID1"
        ///            .Value5 = by1
        ///        End With
        ///    Dim tl(2) As Variant
        ///    Set tl(0) = tl0
        ///    Set tl(1) = tl1
        ///    Dim tv As New TestValue
        ///        With tv
        ///            .Barcode = "BarTestTV"
        ///            .testValues = tl
        ///        End With
        ///    Dim ret As String
        ///    Call vbt.VBSendObj(tv, ret)
        ///   MsgBox (ret)
        ///End Sub
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool VBSendObj(ref object obj, ref string msg) {
            TestValue tv = (TestValue)obj;
            TestValueList tvl0 = (TestValueList)tv.testValues[0];
            msg = tvl0.cGUID;
            return true;
        }

        /// <summary>
        /// Private Sub Command7VBRefObj_Click()
        ///Dim tv As New TestValue
        ///        Call vbt.VBGetObj(tv)
        ///'        MsgBox (tv.cGUID)
        ///        Dim tl() As Variant
        ///        tl = tv.testValues
        ///        Set tl1 = vbt.CTestValueList(tl(0))
        ///        MsgBox(tl1.cGUID)
        ///End Sub
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool VBGetObj(ref object obj) {
            TestValue tv = new TestValue() {
                Barcode = "123", cGUID = "adsfasfdasfdas",
                testValues = new object[] {
                    new TestValueList() { cGUID = "00bbbbb", Remark = "okRemark" }
                ,new TestValueList() { cGUID = "11bbbbb", Remark = "ngRemark" }
                }
            };
            obj = tv;
            return true;
        }

        /// <summary>
        ///Private Sub Command4VBGetClass_Click()
        ///        Dim tv As TestValue
        ///        Set tv = vbt.VBGetClass()
        ///        Dim tl() As Variant
        ///        tl = tv.testValues
        ///        MsgBox(tl(0).cGUID)
        ///End Sub
        /// </summary>
        /// <returns></returns>
        public TestValue VBGetClass() {
            TestValue tv = new TestValue() {
                Barcode = "123", cGUID = "adsfasfdasfdas",
                testValues = new object[] {
                    new TestValueList() { cGUID = "00bbbbb", Remark = "okRemark" }
                ,new TestValueList() { cGUID = "11bbbbb", Remark = "ngRemark" }
                }
            };
            return tv;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tv"></param>
        /// <returns></returns>
        public bool TestClass(ref TestValue[] tv) {
            tv[0].Barcode = "C#Mod";
            return true;
        }

        /// <summary>
        /// 等效VBGetObj
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public TestValue CTestValue(object obj) {
            return (TestValue)obj;
        }

        /// <summary>
        /// 等效VBGetObj
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public TestValueList CTestValueList(object obj) {
            return (TestValueList)obj;
        }
    }
}

 

 
posted @ 2022-08-03 11:37  enif  阅读(245)  评论(0编辑  收藏  举报
豫ICP备2021034901号