在VB.NET中写个例子实现所有VB6 X86数据类型的对应:string,long,Single等
以下是一个完整的 VB.NET COM DLL 示例,实现了所有 VB6 X86 数据类型的对应:
完整的 COM 类实现
vbnet
Imports System.Runtime.InteropServices
<ComClass(VB6Compatible.ClassId, VB6Compatible.InterfaceId, VB6Compatible.EventsId)>
<ComVisible(True)>
Public Class VB6Compatible
#Region "COM GUIDs"
' 请替换为实际的GUID(使用Tools -> Create GUID生成)
Public Const ClassId As String = "12345678-1234-1234-1234-123456789ABC"
Public Const InterfaceId As String = "12345678-1234-1234-1234-123456789ABD"
Public Const EventsId As String = "12345678-1234-1234-1234-123456789ABE"
#End Region
#Region "构造函数"
Public Sub New()
MyBase.New()
End Sub
#End Region
#Region "字符串类型 (String)"
' VB6: Function ProcessString(input As String) As String
Public Function ProcessString(ByVal input As String) As String
Return "Processed: " & input
End Function
' VB6: Sub ModifyString(ByRef text As String)
Public Sub ModifyString(ByRef text As String)
text = "Modified: " & text
End Sub
' VB6: Function ConcatenateStrings(a As String, b As String) As String
Public Function ConcatenateStrings(ByVal a As String, ByVal b As String) As String
Return a & " " & b
End Function
#End Region
#Region "整数类型"
' VB6 Integer (16位) - 对应 VB.NET Short
' VB6: Function AddIntegers(a As Integer, b As Integer) As Integer
Public Function AddIntegers(ByVal a As Short, ByVal b As Short) As Short
Return CShort(a + b)
End Function
' VB6 Long (32位) - 对应 VB.NET Integer
' VB6: Function AddLongs(a As Long, b As Long) As Long
Public Function AddLongs(ByVal a As Integer, ByVal b As Integer) As Integer
Return a + b
End Function
' 明确使用 MarshalAs 指定
Public Function AddIntegersExplicit(
<MarshalAs(UnmanagedType.I2)> ByVal a As Integer,
<MarshalAs(UnmanagedType.I2)> ByVal b As Integer) _
As <MarshalAs(UnmanagedType.I2)> Integer
Return a + b
End Function
#End Region
#Region "浮点类型"
' VB6 Single - 对应 VB.NET Single
' VB6: Function AddSingles(a As Single, b As Single) As Single
Public Function AddSingles(ByVal a As Single, ByVal b As Single) As Single
Return a + b
End Function
' VB6 Double - 对应 VB.NET Double
' VB6: Function AddDoubles(a As Double, b As Double) As Double
Public Function AddDoubles(ByVal a As Double, ByVal b As Double) As Double
Return a + b
End Function
#End Region
#Region "货币类型 (Currency)"
' VB6 Currency - 对应 VB.NET Decimal
' VB6: Function AddCurrency(a As Currency, b As Currency) As Currency
Public Function AddCurrency(ByVal a As Decimal, ByVal b As Decimal) As Decimal
Return a + b
End Function
' VB6: Function CalculateTotal(price As Currency, quantity As Long) As Currency
Public Function CalculateTotal(ByVal price As Decimal, ByVal quantity As Integer) As Decimal
Return price * quantity
End Function
#End Region
#Region "布尔类型"
' VB6 Boolean - 对应 VB.NET Boolean
' VB6: Function IsPositive(value As Long) As Boolean
Public Function IsPositive(ByVal value As Integer) As Boolean
Return value > 0
End Function
' VB6: Sub ToggleFlag(ByRef flag As Boolean)
Public Sub ToggleFlag(ByRef flag As Boolean)
flag = Not flag
End Sub
#End Region
#Region "日期类型"
' VB6 Date - 对应 VB.NET Date
' VB6: Function AddDays(startDate As Date, days As Long) As Date
Public Function AddDays(ByVal startDate As Date, ByVal days As Integer) As Date
Return startDate.AddDays(days)
End Function
' VB6: Function GetCurrentDate() As Date
Public Function GetCurrentDate() As Date
Return Date.Now
End Function
#End Region
#Region "变体类型 (Variant)"
' VB6 Variant - 对应 VB.NET Object
' VB6: Function ProcessVariant(input As Variant) As Variant
Public Function ProcessVariant(ByVal input As Object) As Object
If input Is Nothing Then
Return "Null input"
ElseIf TypeOf input Is String Then
Return "String: " & input.ToString()
ElseIf TypeOf input Is Integer Then
Return CInt(input) * 2
ElseIf TypeOf input Is Double Then
Return CDbl(input) * 1.5
Else
Return "Unknown type: " & input.GetType().Name
End If
End Function
#End Region
#Region "字节类型 (Byte)"
' VB6 Byte - 对应 VB.NET Byte
' VB6: Function AddBytes(a As Byte, b As Byte) As Byte
Public Function AddBytes(ByVal a As Byte, ByVal b As Byte) As Byte
Return CByte((a + b) Mod 256)
End Function
#End Region
#Region "数组类型"
' VB6 数组 - 需要明确指定 MarshalAs
' VB6: Function SumIntegerArray(arr() As Integer) As Long
Public Function SumIntegerArray(
<MarshalAs(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_I2)>
ByVal arr() As Short) As Integer
Dim sum As Integer = 0
For Each item As Short In arr
sum += item
Next
Return sum
End Function
' VB6: Function ProcessStringArray(arr() As String) As String
Public Function ProcessStringArray(
<MarshalAs(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_BSTR)>
ByVal arr() As String) As String
Return String.Join(", ", arr)
End Function
#End Region
#Region "可选参数和默认值"
' VB6: Function CalculateWithOptional(amount As Double, Optional taxRate As Double = 0.1) As Double
Public Function CalculateWithOptional(ByVal amount As Double,
Optional ByVal taxRate As Double = 0.1) As Double
Return amount * (1 + taxRate)
End Function
' VB6: Sub MethodWithMultipleOptionals( _
' ByVal required As String, _
' Optional ByVal optional1 As Long = 10, _
' Optional ByVal optional2 As String = "default")
Public Sub MethodWithMultipleOptionals(ByVal required As String,
Optional ByVal optional1 As Integer = 10,
Optional ByVal optional2 As String = "default")
' 方法实现
End Sub
#End Region
#Region "ByRef 参数示例"
' VB6: Sub IncrementByRef(ByRef value As Long)
Public Sub IncrementByRef(ByRef value As Integer)
value += 1
End Sub
' VB6: Sub SwapDoubles(ByRef a As Double, ByRef b As Double)
Public Sub SwapDoubles(ByRef a As Double, ByRef b As Double)
Dim temp As Double = a
a = b
b = temp
End Sub
#End Region
#Region "复杂示例 - 综合使用多种类型"
' VB6: Function CalculateOrderTotal( _
' ByVal unitPrice As Currency, _
' ByVal quantity As Long, _
' ByVal discountRate As Double, _
' ByVal taxExempt As Boolean) As Currency
Public Function CalculateOrderTotal(ByVal unitPrice As Decimal,
ByVal quantity As Integer,
ByVal discountRate As Double,
ByVal taxExempt As Boolean) As Decimal
Dim subtotal As Decimal = unitPrice * quantity
Dim discounted As Decimal = subtotal * CDec(1 - discountRate)
If Not taxExempt Then
Return discounted * 1.08D ' 8% 税
Else
Return discounted
End If
End Function
#End Region
End Class
项目设置要求
在 AssemblyInfo.vb 中添加:
vbnet
Imports System.Runtime.InteropServices
<Assembly: ComVisible(True)>
<Assembly: Guid("你的项目GUID")>
<Assembly: AssemblyVersion("1.0.0.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")>
编译设置
-
项目属性 → 应用程序 → 程序集信息 → 勾选"使程序集 COM 可见"
-
项目属性 → 编译 → 勾选"注册 COM 互操作"
-
项目属性 → 签名 → 勾选"为程序集签名",选择或创建强名称密钥文件
在 VB6 中的调用示例
vbnet
Dim obj As New YourDLL.VB6Compatible
Dim result As Variant
' 字符串操作
Dim text As String
text = obj.ProcessString("Hello")
obj.ModifyString text
' 数值计算
Dim sumInt As Integer
sumInt = obj.AddIntegers(10, 20)
Dim sumLong As Long
sumLong = obj.AddLongs(1000, 2000)
Dim total As Currency
total = obj.CalculateTotal(19.99, 5)
' 布尔和日期
Dim isPos As Boolean
isPos = obj.IsPositive(-5)
Dim newDate As Date
newDate = obj.AddDays(Date, 7)
' 变体类型
Dim varResult As Variant
varResult = obj.ProcessVariant("Test")
varResult = obj.ProcessVariant(100)
这个示例涵盖了 VB6 X86 中所有主要数据类型的对应关系,并提供了完整的实现和调用示例。