最近需要一个功能是在VB6和C#中都要共用的,所以选择使用COM来实现。
但是不知道如何让COM返回自定义类型的返回值。
请高手指教!

解决方案 »

  1.   

    自己占沙发补充一下。
    即使是用Object作为返回值,VB6调用时也报错:
    实时错误 '430'
    类不支持自动化或不支持期望的接口。
    请问这是何解呀
      

  2.   

    VB可以使用dll呀。
    Com的话,在C#里是自动生成的引用代码,不让改,因此,你写起来,可能有写东西不符合你的要求。
    你是用VB写dll,在C#里使用dllimport的方式导入,并且最终使用intptr作为引用,应该问题不大。
    或者使用object试试先,这个比较简单的。
      

  3.   

    那你要把自定义的类型也要做到COM里面去!
    这样非托管代码才能使用这个类型。
      

  4.   

    是用VB6调用COM,因为VB6不能直接调用.NET的DLL。
    暂时找到一个方法,如下:
    C#的COM代码:
    Test4VB6类代码
    using System;
    using System.Runtime.InteropServices;namespace Test4VB6
    {
        [ComVisible(true)]
        public interface ITestClass
        {
            string GetDataModel1(ref DataModel1 dataModel1);
        }    [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.AutoDispatch)] 
        public class TestClass : ITestClass
        {
            public string GetDataModel1(ref DataModel1 dataModel1)
            {
                if (dataModel1 == null)
                    dataModel1 = new DataModel1();            dataModel1.Data1 = "Fuck222!";            return dataModel1.Data1;
            }
        }
    }
    DataModel1类代码:
    using System;
    using System.Runtime.InteropServices;namespace Test4VB6
    {
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.AutoDispatch)]
        public class DataModel1
        {
            public int ID
            {
                get;
                set;
            }        public string Data1
            {
                get;
                set;
            }
        }
    }VB6调用代码:
    Dim test4VB61 As New Test4VB6.TestClass
        Dim testStr As String    Dim dataModel1Temp As DataModel1
        testStr = test4VB61.GetDataModel1(dataModel1Temp)
        testStr = dataModel1Temp.Data1
      

  5.   

    对,就这样使用,把定义的类型写到COM里!
      

  6.   

    补充一下,接口public interface ITestClass,好像去掉也没关系。
    大家路过留言吧,问题自己解决了。
    路过的就散分啦!!!!