禁用U盘的程序代码?
各位:最近想写个禁止使用U盘的程序代买,不知道如何下手,盼各位能给点思路。
--------------------编程问答-------------------- 是了,怎么判断一个外接设备是不是U盘呢.可能要禁用掉所有移动磁盘吧 --------------------编程问答-------------------- IFS,文件过滤驱动。
下载一个IFS SDK,里面有例子。不过VB实现比较困难,用C/C++吧。 --------------------编程问答-------------------- 关注 回复内容太短了! --------------------编程问答-------------------- 请在电脑主机上贴一通知:此机器禁止使用U盘。 --------------------编程问答-------------------- 引用API,通用声明部分:
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
在界面上添加一个Command,两个Option(一个为命名为开启,另一个为关闭)
在Command下输入以下代码:
Dim ret As Long, ret2 As Long, hKey As Long, hKey2 As Long
ret = RegOpenKey(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\StorageDevicePolicies", hKey)
If Option1.Value = True Then
If ret = 0 Then
RegSetValueEx hKey, "WriteProtect", 0, REG_DWORD, 1, 4
MsgBox "写保护已开启!", 0, "提示"
Else
RegCreateKey HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\StorageDevicePolicies", hKey
RegSetValueEx hKey, "WriteProtect", 0, REG_DWORD, 1, 4
MsgBox "写保护已开启!", 0, "提示"
End If
End If
If Option2.Value = True Then
If ret = 0 Then
RegSetValueEx hKey, "WriteProtect", 0, REG_DWORD, 0, 4
MsgBox "写保护已关闭!", 0, "提示"
End If
End If
RegCloseKey hKey
RegCloseKey hKey2
开启禁止U盘写入功能,须拨下U盘后再插上方起作用. --------------------编程问答-------------------- 关注...回复内容太短了! --------------------编程问答-------------------- 二楼的兄弟caozhy:
能不能贴个例子看看。 --------------------编程问答-------------------- 用vb写禁用U盘的程序是一点问题都没有的,因为我早都写成功了,而且效果非常好。
原理是,循环判断是否有移动设备插入计算机,如果发现,就弹出,类似程序非常多,去google搜吧。 --------------------编程问答-------------------- 关注...回复内容太短了! --------------------编程问答-------------------- 这段代码貌似可以禁用锁定U盘:
--------------------编程问答-------------------- 楼上的兄弟,我是一点也看不懂你的代码。 --------------------编程问答-------------------- GOU YAN~~~~ --------------------编程问答-------------------- 同意10楼,运用WMI实现可行。
'Example Name: Using DeviceIoControl
'------------------------------------------------------------------------------
'BAS Module Code
'------------------------------------------------------------------------------
Option Explicit
Public Const DRIVE_REMOVABLE = 2
Public Const DRIVE_CDROM = 5
Public Const INVALID_HANDLE_VALUE As Long = -1&
Public Const GENERIC_READ As Long = &H80000000
Public Const FILE_SHARE_READ As Long = &H1
Public Const FILE_SHARE_WRITE As Long = &H2
Public Const FILE_ANY_ACCESS As Long = &H0
Public Const FILE_READ_ACCESS As Long = &H1
Public Const FILE_WRITE_ACCESS As Long = &H2
Public Const OPEN_EXISTING As Long = 3
Public Const IOCTL_STORAGE_MEDIA_REMOVAL As Long = &H2D4804
Public Type PREVENT_MEDIA_REMOVAL
PreventMediaRemoval As Byte
End Type
Public Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long
Public Declare Function GetDriveType Lib "kernel32" _
Alias "GetDriveTypeA" _
(ByVal lpRootPathName As String) As Long
Public Declare Function DeviceIoControl Lib "kernel32" _
(ByVal hDevice As Long, _
ByVal dwIoControlCode As Long, _
lpInBuffer As Any, _
ByVal nInBufferSize As Long, _
lpOutBuffer As Any, _
ByVal nOutBufferSize As Long, _
lpBytesReturned As Long, _
lpOverlapped As Any) As Long
Public Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
lpSecurityAttributes As Any, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Public Function DeviceLock(sDrive As String, fLock As Boolean) As Boolean
Dim hDevice As Long
Dim PMR As PREVENT_MEDIA_REMOVAL
Dim bytesReturned As Long
Dim success As Long
'the drive letter has to be passed
'to CreateFile without a trailing slash (ie 'G:')
sDrive = UnQualifyPath(sDrive)
'obtain a handle to the device
'using the correct device syntax
hDevice = CreateFile("\\.\" & sDrive, _
GENERIC_READ, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
ByVal 0&, _
OPEN_EXISTING, _
0&, 0&)
If hDevice <> INVALID_HANDLE_VALUE Then
'assign the fLock value to the
'PREVENT_MEDIA_REMOVAL type
PMR.PreventMediaRemoval = CByte(Abs(fLock))
'If the operation succeeds,
'DeviceIoControl returns a nonzero value
success = DeviceIoControl(hDevice, _
IOCTL_STORAGE_MEDIA_REMOVAL, _
PMR, _
Len(PMR), _
ByVal 0&, _
0&, _
bytesReturned, _
ByVal 0&)
End If
Call CloseHandle(hDevice)
DeviceLock = success
End Function
Private Function UnQualifyPath(ByVal sPath As String) As String
'removes any trailing slash from the path
sPath = Trim$(sPath)
If Right$(sPath, 1) = "\" Then
UnQualifyPath = Left$(sPath, Len(sPath) - 1)
Else: UnQualifyPath = sPath
End If
End Function
'--end block--'
'------------------------------------------------------------------------------
'Form Code
'
'Add a listbox (List1), a command button array (Command1(0), Command1(1)),
'and a label (Label1) to a form. The third button shown (End) is optional.
'Add the following code to the form:
'------------------------------------------------------------------------------
Option Explicit
Private Sub Form_Load()
'load the removable drives and
'disable the command buttons until
'a drive is selected
LoadAvailableDrives List1
Command1(0).Enabled = False
Command1(1).Enabled = False
End Sub
Private Sub List1_Click()
Command1(0).Enabled = List1.ListIndex > -1
Command1(1).Enabled = List1.ListIndex > -1
End Sub
Private Sub Command1_Click(Index As Integer)
Dim fLock As Boolean
Dim result As Boolean
Dim sDrive As String
'nothing to do if a drive's not selected
If List1.ListIndex > -1 Then
'get the selected drive
sDrive = List1.List(List1.ListIndex)
'DeviceIoControl requires the
'IOCTL_STORAGE_MEDIA_REMOVAL
'control code and a Boolean
'indicating the lock state.
'Passing False unlocks the device;
'passing True locks it. This handily
'corresponds to the 0/1 indices of
'the Command button array.
fLock = CBool(Index)
result = DeviceLock(sDrive, fLock)
'display result
If result Then
Select Case Index
Case 0: Label1.Caption = "Device " & sDrive & " is unlocked."
Case 1: Label1.Caption = "Device " & sDrive & " is locked."
End Select
Else: Label1.Caption = "Call failed - perhaps no media in device."
End If
End If
End Sub
Private Sub LoadAvailableDrives(lst As ListBox)
Dim lpBuffer As String
Dim drvType As Long
Dim currDrive As String
'get list of available drives
lpBuffer = GetDriveString()
'Separate the drive strings
'and add to the combo. StripNulls
'will continually shorten the
'string. Loop until a single
'remaining terminating null is
'encountered.
Do Until lpBuffer = Chr(0)
'strip off one drive item
'and check for removable (or CD) status,
'and add to the combo
currDrive = StripNulls(lpBuffer)
drvType = GetDriveType(currDrive)
If (drvType = DRIVE_CDROM) Or _
(drvType = DRIVE_REMOVABLE) Then
lst.AddItem currDrive
End If
Loop
End Sub
Private Function StripNulls(startstr As String) As String
'Take a string separated by chr$(0)
'and split off 1 item, shortening the
'string so next item is ready for removal.
Dim pos As Long
pos = InStr(startstr, Chr$(0))
If pos Then
StripNulls = Mid$(startstr, 1, pos - 1)
startstr = Mid$(startstr, pos + 1, Len(startstr))
End If
End Function
Private Function GetDriveString() As String
'returns of available drives each
'separated by a null
Dim sBuffer As String
'possible 26 drives, three characters each, plus trailing null
sBuffer = Space$(26 * 4)
If GetLogicalDriveStrings(Len(sBuffer), sBuffer) Then
'trim string but do not
'remove trailing null
GetDriveString = Trim$(sBuffer)
End If
End Function
--------------------编程问答-------------------- 谁能帮我解释一下10楼的代码含义? --------------------编程问答-------------------- 十楼你真牛!很强很暴力
--------------------编程问答--------------------
支持了! --------------------编程问答--------------------
这难道就是传说中的高人 连注释都是英文的 我的妈啊 我还要注释干吗 --------------------编程问答-------------------- 帮 顶 --------------------编程问答-------------------- mark --------------------编程问答-------------------- u p --------------------编程问答-------------------- 帮 顶 --------------------编程问答-------------------- 友情up --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark --------------------编程问答-------------------- 不懂,up --------------------编程问答-------------------- 不懂,up --------------------编程问答-------------------- 直接注册表搞定
http://download.csdn.net/source/2490178 --------------------编程问答-------------------- 直接注册表搞定
http://download.csdn.net/source/2490178 --------------------编程问答-------------------- 注册表的不太好,一就是要及时监控移动设备,也就是要用timer不断执行那断代码,很耗资源.二就是从cmd里照进 --------------------编程问答--------------------
这不是要跟驱动打交道了?
我以前搜代码弹过,不过光驱没问题,U盘,不行,老出错
补充:VB , COM/DCOM/COM+