重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
vb6-legacy by bobmatnyc/claude-mpm-skills
npx skills add https://github.com/bobmatnyc/claude-mpm-skills --skill vb6-legacy维护 VB6 代码的模式以及通过 COM 互操作迁移到 VB.NET 的策略。
' VB6 - Variant 类型
Dim data As Variant
data = 123
data = "Hello"
' VB.NET - 启用 Option Strict 的强类型
Option Strict On
Dim data As Object ' 尽可能避免使用
Dim number As Integer = 123
Dim text As String = "Hello"
' VB6 - 默认属性
Text1.Text = "Hello"
Text1 = "Hello" ' 使用默认的 Text 属性
' VB.NET - 需要显式指定属性
TextBox1.Text = "Hello" ' 必须显式指定
' VB6 - 默认 ByRef
Sub ProcessData(data As String) ' 默认 ByRef
' VB.NET - 默认 ByVal
Sub ProcessData(data As String) ' 默认 ByVal
Sub ProcessData(ByRef data As String) ' 显式 ByRef
' VB6 - On Error
On Error Resume Next
On Error GoTo ErrorHandler
' VB.NET - Try-Catch
Try
' 代码
Catch ex As Exception
' 处理错误
End Try
' VB6 - 定长字符串
Dim name As String * 50
' VB.NET - 使用常规字符串和 PadRight
Dim name As String = "John".PadRight(50)
' VB6 - Currency 类型
Dim amount As Currency
' VB.NET - 使用 Decimal
Dim amount As Decimal
' VB6 - 控件数组
Dim TextBox(5) As TextBox
' VB.NET - 使用集合
Dim textBoxes As New List(Of TextBox)()
' VB6 - Let/Set 关键字
Let x = 5
Set obj = New MyClass
' VB.NET - 无需关键字的赋值
Dim x As Integer = 5
Dim obj As New MyClass()
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
' 在项目中添加 COM 引用
' 工具 -> 添加引用 -> COM -> Excel 对象库
Imports Excel = Microsoft.Office.Interop.Excel
Public Sub ExportToExcel(data As DataTable)
Dim excelApp As Excel.Application = Nothing
Dim workbook As Excel.Workbook = Nothing
Dim worksheet As Excel.Worksheet = Nothing
Try
excelApp = New Excel.Application()
workbook = excelApp.Workbooks.Add()
worksheet = CType(workbook.Worksheets(1), Excel.Worksheet)
' 写入表头
For col = 0 To data.Columns.Count - 1
worksheet.Cells(1, col + 1) = data.Columns(col).ColumnName
Next
' 写入数据
For row = 0 To data.Rows.Count - 1
For col = 0 To data.Columns.Count - 1
worksheet.Cells(row + 2, col + 1) = data.Rows(row)(col)
Next
Next
excelApp.Visible = True
Catch ex As Exception
MessageBox.Show($"Excel 导出失败: {ex.Message}")
Finally
' 释放 COM 对象
If worksheet IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet)
End If
If workbook IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
End If
If excelApp IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
End If
End Try
End Sub
Imports System.Reflection
Public Sub CreateExcelLateBound()
Dim excelType As Type = Type.GetTypeFromProgID("Excel.Application")
If excelType Is Nothing Then
MessageBox.Show("Excel 未安装")
Return
End If
Dim excelApp As Object = Nothing
Try
' 创建 COM 对象
excelApp = Activator.CreateInstance(excelType)
' 通过反射调用方法
excelType.InvokeMember("Visible",
BindingFlags.SetProperty,
Nothing,
excelApp,
New Object() {True})
Dim workbooks As Object = excelType.InvokeMember("Workbooks",
BindingFlags.GetProperty,
Nothing,
excelApp,
Nothing)
' 添加工作簿
workbooks.GetType().InvokeMember("Add",
BindingFlags.InvokeMethod,
Nothing,
workbooks,
Nothing)
Finally
If excelApp IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
End If
End Try
End Sub
' ✅ 良好:正确的 COM 对象清理
Public Sub UseComObject()
Dim excelApp As Excel.Application = Nothing
Dim workbook As Excel.Workbook = Nothing
Try
excelApp = New Excel.Application()
workbook = excelApp.Workbooks.Add()
' 使用对象
Finally
' 按创建顺序反向释放
If workbook IsNot Nothing Then
workbook.Close(False)
Marshal.ReleaseComObject(workbook)
workbook = Nothing
End If
If excelApp IsNot Nothing Then
excelApp.Quit()
Marshal.ReleaseComObject(excelApp)
excelApp = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
' ❌ 不良:未释放 COM 对象(内存泄漏)
Dim excelApp = New Excel.Application()
excelApp.Workbooks.Add()
' 没有清理 - Excel 进程会保留在内存中!
Imports System.Runtime.InteropServices
<ComVisible(True)>
<Guid("12345678-1234-1234-1234-123456789012")>
<ClassInterface(ClassInterfaceType.None)>
<ProgId("MyCompany.Calculator")>
Public Class Calculator
Implements ICalculator
Public Function Add(a As Integer, b As Integer) As Integer Implements ICalculator.Add
Return a + b
End Function
Public Function Subtract(a As Integer, b As Integer) As Integer Implements ICalculator.Subtract
Return a - b
End Function
End Class
' 用于 COM 的接口
<ComVisible(True)>
<Guid("87654321-4321-4321-4321-210987654321")>
<InterfaceType(ComInterfaceType.InterfaceIsDual)>
Public Interface ICalculator
Function Add(a As Integer, b As Integer) As Integer
Function Subtract(a As Integer, b As Integer) As Integer
End Interface
# 为 COM 注册程序集
regasm MyAssembly.dll /tlb /codebase
# 取消注册
regasm MyAssembly.dll /u
# 生成类型库
tlbexp MyAssembly.dll
' VB6 风格(在新代码中避免使用)
Public Function GetCustomer(id As Integer) As Customer
On Error GoTo ErrorHandler
' 此处代码
Exit Function
ErrorHandler:
MsgBox "错误: " & Err.Description
Resume Next
End Function
' VB.NET 现代风格
Public Function GetCustomer(id As Integer) As Customer
Try
' 此处代码
Catch ex As ArgumentException
MessageBox.Show($"无效参数: {ex.Message}")
Return Nothing
Catch ex As Exception
MessageBox.Show($"错误: {ex.Message}")
Throw
End Try
End Function
' VB6 风格(避免使用)
Dim fileNum As Integer = FreeFile()
FileOpen(fileNum, "data.txt", OpenMode.Output)
PrintLine(fileNum, "Hello World")
FileClose(fileNum)
' VB.NET 现代风格
Using writer = New StreamWriter("data.txt")
writer.WriteLine("Hello World")
End Using
' 或异步方式
Await File.WriteAllTextAsync("data.txt", "Hello World")
' VB6 集合(在新代码中避免使用)
Dim customers As New Collection()
customers.Add(customer, "key1")
Dim item = customers("key1")
' VB.NET 泛型集合
Dim customers = New Dictionary(Of String, Customer)()
customers.Add("key1", customer)
Dim item = customers("key1")
' 或 List(Of T)
Dim customerList = New List(Of Customer)()
customerList.Add(customer)
' 1. 从 COM 互操作包装器开始
' 在 VB.NET 中包装 VB6 COM 组件
Public Class VB6Wrapper
Private vb6Component As Object
Public Sub New()
vb6Component = CreateObject("VB6Project.Component")
End Sub
Public Function ProcessData(data As String) As String
Return vb6Component.ProcessData(data).ToString()
End Function
End Class
' 2. 逐步替换为原生 VB.NET
Public Class ModernComponent
Public Function ProcessData(data As String) As String
' 新的 VB.NET 实现
Return data.ToUpper()
End Function
End Class
' 在新代码中使用现代 VB.NET 特性
Option Strict On
Option Explicit On
' 显式释放 COM 对象
Marshal.ReleaseComObject(comObject)
GC.Collect()
' 使用 Try-Catch 替代 On Error
Try
' 代码
Catch ex As Exception
' 处理
End Try
' 使用泛型替代集合
Dim items = New List(Of Customer)()
' 对 I/O 使用异步
Await File.WriteAllTextAsync("file.txt", content)
' 不要使用 VB6 兼容性模块
Imports Microsoft.VisualBasic.Compatibility.VB6 ' 仅限遗留代码!
' 不要使用 Option Strict Off
Option Strict Off ' 避免!
' 不要在新代码中使用 On Error
On Error Resume Next ' VB6 风格
' 不要忘记释放 COM 对象
Dim excelApp = New Excel.Application()
' ... (没有清理 - 内存泄漏!)
' 当有早期绑定时不要使用后期绑定
Dim obj = CreateObject("Excel.Application") ' 应使用类型化引用
每周安装次数
38
仓库
GitHub 星标数
18
首次出现
2026年2月14日
安全审计
安装于
codex35
gemini-cli33
opencode33
cursor33
github-copilot32
amp31
Patterns for maintaining VB6 code and strategies for migrating to VB.NET with COM interop.
' VB6 - Variant types
Dim data As Variant
data = 123
data = "Hello"
' VB.NET - Strong typing with Option Strict On
Option Strict On
Dim data As Object ' Still avoid when possible
Dim number As Integer = 123
Dim text As String = "Hello"
' VB6 - Default properties
Text1.Text = "Hello"
Text1 = "Hello" ' Uses default Text property
' VB.NET - Explicit properties required
TextBox1.Text = "Hello" ' Must be explicit
' VB6 - ByRef default
Sub ProcessData(data As String) ' ByRef by default
' VB.NET - ByVal default
Sub ProcessData(data As String) ' ByVal by default
Sub ProcessData(ByRef data As String) ' Explicit ByRef
' VB6 - On Error
On Error Resume Next
On Error GoTo ErrorHandler
' VB.NET - Try-Catch
Try
' Code
Catch ex As Exception
' Handle error
End Try
' VB6 - Fixed-length strings
Dim name As String * 50
' VB.NET - Use regular string and PadRight
Dim name As String = "John".PadRight(50)
' VB6 - Currency type
Dim amount As Currency
' VB.NET - Use Decimal
Dim amount As Decimal
' VB6 - Control arrays
Dim TextBox(5) As TextBox
' VB.NET - Use collection
Dim textBoxes As New List(Of TextBox)()
' VB6 - Let/Set keywords
Let x = 5
Set obj = New MyClass
' VB.NET - Assignment without keywords
Dim x As Integer = 5
Dim obj As New MyClass()
' Add COM reference in project
' Tools -> Add Reference -> COM -> Excel Object Library
Imports Excel = Microsoft.Office.Interop.Excel
Public Sub ExportToExcel(data As DataTable)
Dim excelApp As Excel.Application = Nothing
Dim workbook As Excel.Workbook = Nothing
Dim worksheet As Excel.Worksheet = Nothing
Try
excelApp = New Excel.Application()
workbook = excelApp.Workbooks.Add()
worksheet = CType(workbook.Worksheets(1), Excel.Worksheet)
' Write headers
For col = 0 To data.Columns.Count - 1
worksheet.Cells(1, col + 1) = data.Columns(col).ColumnName
Next
' Write data
For row = 0 To data.Rows.Count - 1
For col = 0 To data.Columns.Count - 1
worksheet.Cells(row + 2, col + 1) = data.Rows(row)(col)
Next
Next
excelApp.Visible = True
Catch ex As Exception
MessageBox.Show($"Excel export failed: {ex.Message}")
Finally
' Release COM objects
If worksheet IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet)
End If
If workbook IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
End If
If excelApp IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
End If
End Try
End Sub
Imports System.Reflection
Public Sub CreateExcelLateBound()
Dim excelType As Type = Type.GetTypeFromProgID("Excel.Application")
If excelType Is Nothing Then
MessageBox.Show("Excel not installed")
Return
End If
Dim excelApp As Object = Nothing
Try
' Create COM object
excelApp = Activator.CreateInstance(excelType)
' Call methods via reflection
excelType.InvokeMember("Visible",
BindingFlags.SetProperty,
Nothing,
excelApp,
New Object() {True})
Dim workbooks As Object = excelType.InvokeMember("Workbooks",
BindingFlags.GetProperty,
Nothing,
excelApp,
Nothing)
' Add workbook
workbooks.GetType().InvokeMember("Add",
BindingFlags.InvokeMethod,
Nothing,
workbooks,
Nothing)
Finally
If excelApp IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
End If
End Try
End Sub
' ✅ Good: Proper COM object cleanup
Public Sub UseComObject()
Dim excelApp As Excel.Application = Nothing
Dim workbook As Excel.Workbook = Nothing
Try
excelApp = New Excel.Application()
workbook = excelApp.Workbooks.Add()
' Use objects
Finally
' Release in reverse order of creation
If workbook IsNot Nothing Then
workbook.Close(False)
Marshal.ReleaseComObject(workbook)
workbook = Nothing
End If
If excelApp IsNot Nothing Then
excelApp.Quit()
Marshal.ReleaseComObject(excelApp)
excelApp = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
' ❌ Bad: Not releasing COM objects (memory leak)
Dim excelApp = New Excel.Application()
excelApp.Workbooks.Add()
' No cleanup - Excel process remains in memory!
Imports System.Runtime.InteropServices
<ComVisible(True)>
<Guid("12345678-1234-1234-1234-123456789012")>
<ClassInterface(ClassInterfaceType.None)>
<ProgId("MyCompany.Calculator")>
Public Class Calculator
Implements ICalculator
Public Function Add(a As Integer, b As Integer) As Integer Implements ICalculator.Add
Return a + b
End Function
Public Function Subtract(a As Integer, b As Integer) As Integer Implements ICalculator.Subtract
Return a - b
End Function
End Class
' Interface for COM
<ComVisible(True)>
<Guid("87654321-4321-4321-4321-210987654321")>
<InterfaceType(ComInterfaceType.InterfaceIsDual)>
Public Interface ICalculator
Function Add(a As Integer, b As Integer) As Integer
Function Subtract(a As Integer, b As Integer) As Integer
End Interface
# Register assembly for COM
regasm MyAssembly.dll /tlb /codebase
# Unregister
regasm MyAssembly.dll /u
# Generate type library
tlbexp MyAssembly.dll
' VB6 style (avoid in new code)
Public Function GetCustomer(id As Integer) As Customer
On Error GoTo ErrorHandler
' Code here
Exit Function
ErrorHandler:
MsgBox "Error: " & Err.Description
Resume Next
End Function
' VB.NET modern style
Public Function GetCustomer(id As Integer) As Customer
Try
' Code here
Catch ex As ArgumentException
MessageBox.Show($"Invalid argument: {ex.Message}")
Return Nothing
Catch ex As Exception
MessageBox.Show($"Error: {ex.Message}")
Throw
End Try
End Function
' VB6 style (avoid)
Dim fileNum As Integer = FreeFile()
FileOpen(fileNum, "data.txt", OpenMode.Output)
PrintLine(fileNum, "Hello World")
FileClose(fileNum)
' VB.NET modern style
Using writer = New StreamWriter("data.txt")
writer.WriteLine("Hello World")
End Using
' Or async
Await File.WriteAllTextAsync("data.txt", "Hello World")
' VB6 Collection (avoid in new code)
Dim customers As New Collection()
customers.Add(customer, "key1")
Dim item = customers("key1")
' VB.NET generic collections
Dim customers = New Dictionary(Of String, Customer)()
customers.Add("key1", customer)
Dim item = customers("key1")
' Or List(Of T)
Dim customerList = New List(Of Customer)()
customerList.Add(customer)
' 1. Start with COM interop wrapper
' Wrap VB6 COM component in VB.NET
Public Class VB6Wrapper
Private vb6Component As Object
Public Sub New()
vb6Component = CreateObject("VB6Project.Component")
End Sub
Public Function ProcessData(data As String) As String
Return vb6Component.ProcessData(data).ToString()
End Function
End Class
' 2. Gradually replace with native VB.NET
Public Class ModernComponent
Public Function ProcessData(data As String) As String
' New VB.NET implementation
Return data.ToUpper()
End Function
End Class
' Use modern VB.NET features in new code
Option Strict On
Option Explicit On
' Release COM objects explicitly
Marshal.ReleaseComObject(comObject)
GC.Collect()
' Use Try-Catch instead of On Error
Try
' Code
Catch ex As Exception
' Handle
End Try
' Use generics instead of Collections
Dim items = New List(Of Customer)()
' Use async for I/O
Await File.WriteAllTextAsync("file.txt", content)
' Don't use VB6 compatibility module
Imports Microsoft.VisualBasic.Compatibility.VB6 ' Legacy only!
' Don't use Option Strict Off
Option Strict Off ' Avoid!
' Don't use On Error in new code
On Error Resume Next ' VB6 style
' Don't forget to release COM objects
Dim excelApp = New Excel.Application()
' ... (no cleanup - memory leak!)
' Don't use late binding when early binding available
Dim obj = CreateObject("Excel.Application") ' Use typed reference instead
Weekly Installs
38
Repository
GitHub Stars
18
First Seen
Feb 14, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
codex35
gemini-cli33
opencode33
cursor33
github-copilot32
amp31
Electron Chromium 升级指南:分阶段同步、构建与补丁管理
291 周安装