作者:土人
编程思路:当光驱里有光盘,立即检测此光盘是否已经注册,如不是,则弹出光驱,从而达到保护光驱的作用。
实现方法:
一.注册光盘
利用INI配置文件记录光盘的卷标号和序列号,比如一张卷标号为Sys、序列号为38972126的光盘,可在INI文件中在[CDRom]下按如下格式记录:ys=38972126。
二.检测光盘是否已经注册
用一个Timer控件监视光驱里是否有光盘,若有,则激活另一个Timer控件,由它来检测光驱里的光盘是否已经注册,然后进行相关操作。
三.获取光盘卷标和序列号
用GetDriveType判断光驱盘符、用GetVolumeInformation读取光盘的卷标和序列号。
四.弹出光驱
用mciSendString可对光驱的开、关进行操作,格式如下:
Call mciSendString("set CDAudio door open", returnstring, 127, 0)
具体步骤:
一.新建标准EXE工程,给窗体绘制如下控件:
控件 Name Caption
Timer tmrCheck
Timer tmrCd
命令按钮 cmdAdd 注册光盘
命令按钮 cmdUnlock 解除保护
二、缺省添加一个标准模块
三、编写代码如下——
'******* 模块代码:******
Option Explicit
'获取磁盘类型的API
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long
'获取磁盘信息的API
Public Declare Function GetVolumeInformation Lib "kernel32" Alias _
"GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal _
lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, _
lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _
lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As _
String, ByVal nFileSystemNameSize As Long) As Long
'用于操作光驱的API
Public Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As String, ByVal uReturnLength As Long, _
ByVal hwndCallback As Long) As Long
'读写INI的API
Public Declare Function WritePrivateProfileString Lib _
"kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName _
As Any, ByVal lpString As Any, ByVal lpFileName As _
String) As Long
Public Declare Function GetPrivateProfileString Lib _
"kernel32" Alias "GetPrivateProfileStringA" (ByVal _
lpApplicationName As String, ByVal lpKeyName As Any, _
ByVal lpDefault As String, ByVal lpReturnedString As _
String, ByVal nSize As Long, ByVal lpFileName As String) _
As Long
Public Const DRIVE_CDROM = 5 '磁盘类型常量--光驱为5
'写INI函数
Public Function WriteIni(ByVal section As String, ByVal key As String, _
ByVal value As String) As Boolean
Dim x As Long, Buff As String * 128, I As Integer
Buff = value + Chr(0)
x = WritePrivateProfileString(section, key, Buff, App.Path + "\cd.ini")
WriteIni = x
End Function
'读INI函数
Public Function ReadIni(ByVal section As String, ByVal key As String) As String
Dim x As Long, Buff As String * 128, I As Integer
x = GetPrivateProfileString(section, key, "", Buff, 128, App.Path + "\cd.ini")
I = InStr(Buff, Chr(0))
ReadIni = Trim(Left(Buff, I - 1))
End Function
'****** 窗体代码:******
Option Explicit
Dim cdName As String '光驱盘符
Dim volName As String '光盘卷标
Dim Serial As String '光盘序列号
Private Sub cmdAdd_Click()
'添加光盘
Dim sR As String
On Error GoTo ErrHandle
sR = Dir(cdName & "*.*")
Readcd '读取光盘信息
Call WriteIni("CDRom", volName, Serial)
Exit Sub
ErrHandle:
Exit Sub
End Sub
Private Sub cmdUnlock_Click()
'保护/解除保护
Select Case cmdUnlock.Caption
Case "解除保护"
tmrCheck.Enabled = False
cmdUnlock.Caption = "保护模式"
Case "保护模式"
tmrCheck.Enabled = True
cmdUnlock.Caption = "解除保护"
End Select
End Sub
Private Sub Form_Load()
Dim DrvN As Integer '驱动器的ASCII码
Dim DrvType As Integer '驱动器的类别
Dim n As Integer
tmrCheck.Enabled = True
tmrCheck.Interval = 1000
tmrCd.Enabled = False
tmrCd.Interval = 1
'获取光驱盘符
DrvN = Asc("c")
For n = 0 To 10
DrvN = DrvN + 1
DrvType = GetDriveType(Chr(DrvN) & ":\")
If DrvType = 5 Then
cdName = Chr(DrvN) & ":\"
End If
Next
If cdName = "" Then '无光驱则退出
MsgBox "该计算机没有光驱,即将退出。"
End
End If
End Sub
Private Sub Readcd() '读取cd信息
Dim Vol As String * 256 '卷标
Dim FatType As String * 256 'fat格式
Dim GetVal As Long '序列号
Dim TempLon1 As Long
Dim TempLon2 As Long
Call GetVolumeInformation(cdName, Vol, 256, _
GetVal, TempLon1, TempLon2, FatType, 256)
volName = Vol: Serial = GetVal '给卷标、序列号赋值
End Sub
Private Sub tmrCheck_Timer()
Dim sR As String
On Error GoTo ErrHandle
'用Dir函数检测光驱里是否有光盘
sR = Dir(cdName & "*.*") '若有光盘
tmrCd.Enabled = True '则tmrCd有效
Exit Sub
ErrHandle: '若无则tmrCd无效
tmrCd.Enabled = False
End Sub
Private Sub tmrCd_Timer()
Dim MyStr As String, ReStr As Long
Readcd
MyStr = ReadIni("CDRom", volName)
If Serial <> MyStr Then Call mciSendString("set CDAudio door open", ReStr, 127, 0)
Me.Caption = ReStr
tmrCd.Enabled = False
End Sub
四、运行程序
将工程保存在指定目录,即可运行程序。
以上代码在PWin98、VB6.0中文企业版环境下运行通过。当然,为使程序的可操作性更强,还有许多工作要做;如果您使用以上代码编制了一个完美的光驱保镖,请发给土人一个免费的拷贝,谢谢!
-
vb教程之用VB编写一个光驱保镖
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比