果洛皆料电子有限公司

VB調(diào)用由C#開發(fā)的com組件

  • 發(fā)布于:2024-02-21
  • 196 人圍觀

1:建立c# 項(xiàng)目 (注意項(xiàng)目必須含有接口,其他類應(yīng)該實(shí)現(xiàn)該接口,才可以在其他語言中用該com組件)

2:將 AssemblyInfo.cs 里的 [assembly: ComVisible(false)] 改為 true

3:項(xiàng)目-屬性-生成里將:為com erop 注冊(cè) 選種

4:生成后就可以在其他語言中引用或該組件。

一般代碼

using System;

using System.Collections.Generic;

using System.Text;

namespace UseMethod

{

public erface IField

{

void SetField(UseMethod.Fields f);

UseMethod.Fields GetField();

UseMethod.Fields GetSetField(UseMethod.Fields f);

Add( i,j);

}

public class FieldAction:IField

{

public FieldAction()

{

//如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行
//InitializeComponent();

}

public void SetField(UseMethod.Fields f)
{

Fields f1 = new Fields();

f1.city = city;

f1.country = country;

f1.myCity = nycity;

f1.myName = myName;

}

public UseMethod.Fields GetField()

{

Fields f1 = new Fields();

f1.city = city;

f1.country = country;

f1.myCity = nycity;

f1.myName = myName;

return f1;

}

public UseMethod.Fields GetSetField(UseMethod.Fields f)

{

return f;
}

publicAdd( i,j)

{

return i + j;

}

}

public class Fields

{

public string city;

public string country;

public string myName;

public string myCity;

}

} 當(dāng)然可以有其他復(fù)雜的類,但是在操作時(shí),最好用強(qiáng)類型,以便于識(shí)別!

將生成的UseMethod.tlb 即.tlb文件引用到vb的項(xiàng)目中,然后

在vb6中調(diào)用:

Private Sub cmdObject_Click()

Dim meth As UseMethod.IField

Dim met As UseMethod.FieldAction

Dim result As UseMethod.Fields

Dim fi As UseMethod.Fields

Set fi = New UseMethod.Fields

Set met = New UseMethod.FieldAction

With fi

.city = city

.country = country

.myName = myName

.myCity = MYcITR

End With

Set meth = met

txtStr.Text = meth.Add(8, 9)

Set result = meth.GetSetField(fi)

txtInOut.Text = result.city result.country //注意這里的屬性應(yīng)和c#里定義的一樣

MsgBox soapClient3.AddressName, vbDefaultButton1, 提示

End Sub

萬企互聯(lián)
標(biāo)簽: