1.用C#编写一个类库(DEMO.DLL)
using System; using System.Collections.Generic; using System.Text; namespace CSHARPDEMO { public class DEMO { public Demo_Response DoRequest(Demo_Request request) { Demo_Response response = new Demo_Response(); response.result = "0000" ..... return response; } } public class Demo_Request { public string name; public string price; } public class Demo_Response { public string result; public string message; public Demo_Object[] demo; public int total; public Demo_Object getDemoInfo(int index) { if (demo != null && demo.Length > 0 && index >= 0 && index < demo.Length) return demo[index]; else return null; } } public class Demo_Object { public string name; public string price; public string source; } }
将该类型上的 ComVisible 属性设置为 true, 使其对 COM 组件可见。
// 将 ComVisible 设置为 false 使此程序集中的类型 // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, // 则将该类型上的 ComVisible 属性设置为 true。 [assembly: ComVisible(true)]
2.注册DEMO.DLL
可以使用命令行注册
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe DEMO.DLL /tlb:DEMO.tlb /codebase
也可以制作安装包,指定DEMO.DLL需要注册为COM
3.在VB中使用
Dim demo As BKDI_WEB Private Sub Command1_Click() '初始化访问对象 Set demo = New DEMO '定义查询条件对象 Dim request As Demo_Request '初始化查询条件对象 Set request = New Demo_Request '设置查询条件 request.name = t_bkiid.Text request.price = t_tocid.Text '定义返回对象 Dim response As Demo_Response '方法调用 Set response = demo.DoRequest(request) '解析结果 If response.result = "0000" Then MsgBox CStr(response.total) + " demo" If response.total > 0 Then Dim book As Demo_Object '遍历列表 For Index = 0 To response.total - 1 Set book = response.getDemoInfo(Index) MsgBox book.name Next Index End If Else MsgBox "结果码:" & response.result & vbCrLf & response.message End If End Sub