在.net 2.0以前的版本,如果需要对串口进行操作的话就需要用到MSComm控件,当我们安装VC++6.0/VB6.0时,如果选择了ACtiveX控件项(自定义安装),MSComm控件就会自动安装在计算机上了,并在系统文件夹下多了3个文件:Mscomm.srg, Mscomm32.ocx,Mscomm32.dep 。如果在不安装vb++6.0和vb6.0的情况下,我们可以进行手工注册。
第一步:将Mscomm.srg, Mscomm32.ocx,Mscomm32.dep三个文件复制到系统文件夹中。要注意的是,MSComm控件是要授权的,所以必须将其使用“执照”Licence 在注册表中登记注册,下一步就是注册方法。至于为什么要这样做,可以看看下面的网页: http://support.microsoft.com/support/kb/articles/q151/7/71.asp
第二步:用Windows下的注册工具regsvr32注册该OCX控件,点击“开始”->"运行",再在中填入(假设操作安装在C盘,WIN2000):
Regsvr32 C:\winnt\system32\Mscomm32.ocx
第三步:在注册表中手工新建一个主键项:先在点击“开始”->"运行",再在中填入regedit命令打开注册表,找到HKEY_CLASSES_ROOT\Licenses,在其中添加主键
4250E830-6AC2-11cf-8ADB-00AA00C00905 并将内容设置为:
kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun
以上是进行的手工注册。
下面介绍如何写成代码,在安装程序的时候同时注册
我们可以新建一个安装类,代码如下
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.IO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<RunInstaller(True)> Public Class Installer1Class Installer1
Inherits System.Configuration.Install.Installer
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
组件设计器生成的代码#Region " 组件设计器生成的代码 "
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New()
MyBase.New()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'该调用是组件设计器所必需的。
InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'在 InitializeComponent() 调用之后添加任何初始化
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'Installer 重写 dispose 以清理组件列表。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'组件设计器所必需的
Private components As System.ComponentModel.IContainer
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'注意: 以下过程是组件设计器所必需的
'可以使用组件设计器来修改此过程。
'不要使用代码编辑器来修改它。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#End Region
Private m_SystemPath As String = ""
Private m_ProgramPath As String = ""
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Overrides Sub Install()Sub Install(ByVal stateSaver As System.Collections.IDictionary)
Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
m_SystemPath = Environment.SystemDirectory
m_ProgramPath = (New FileInfo(Asm.Location)).DirectoryName & Path.DirectorySeparatorChar & "控件"
If File.Exists(m_SystemPath & "\MSCOMM32.DEP") Then
File.Delete(m_SystemPath & "\MSCOMM32.DEP")
End If
File.Copy(m_ProgramPath & "\MSCOMM32.DEP", m_SystemPath & "\MSCOMM32.DEP")
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
If File.Exists(m_SystemPath & "\MSCOMM32.oca") Then
File.Delete(m_SystemPath & "\MSCOMM32.oca")
End If
File.Copy(m_ProgramPath & "\MSCOMM32.oca", m_SystemPath & "\MSCOMM32.oca")
If File.Exists(m_SystemPath & "\MSCOMM32.OCX") Then
File.Delete(m_SystemPath & "\MSCOMM32.OCX")
End If
File.Copy(m_ProgramPath & "\MSCOMM32.OCX", m_SystemPath & "\MSCOMM32.OCX")
Dim regVersion As Microsoft.Win32.RegistryKey
regVersion = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("Licenses\\4250E830-6AC2-11cf-8ADB-00AA00C00905")
Dim newVersion As String = "kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun"
If (Not regVersion Is Nothing) Then
regVersion.SetValue("", newVersion)
regVersion.Close()
End If
Shell("Regsvr32 /s " & m_SystemPath & "\MSCOMM32.OCX", AppWinStyle.Hide)
End Sub
End Class
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
m_ProgramPath = (New FileInfo(Asm.Location)).DirectoryName & Path.DirectorySeparatorChar & "控件"
在打包的同时,也把Mscomm.srg, Mscomm32.ocx,Mscomm32.dep 打包到“控件”文件夹里,这样在安装时,就可以同时起到了注册的过程,我的程序就是这样做的,还很好用。