在.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
<RunInstaller(True)> Public Class Installer1
Inherits System.Configuration.Install.Installer
组件设计器生成的代码
Private m_SystemPath As String = ""
Private m_ProgramPath As String = ""
Public Overrides 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")
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
m_ProgramPath = (New FileInfo(Asm.Location)).DirectoryName & Path.DirectorySeparatorChar & "控件"Imports System.Configuration.Install
Imports System.IO
<RunInstaller(True)> Public Class Installer1
Inherits System.Configuration.Install.Installer
组件设计器生成的代码
Private m_SystemPath As String = ""
Private m_ProgramPath As String = ""
Public Overrides 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")
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
在打包的同时,也把Mscomm.srg, Mscomm32.ocx,Mscomm32.dep 打包到“控件”文件夹里,这样在安装时,就可以同时起到了注册的过程,我的程序就是这样做的,还很好用。