Hello.
I'm trying to create a program in Visual Basic 6 that detects when a user is attempting to remove a USB drive, by using the "Safely Remove Hardware" icon on the taskbar. There is supposed to be a message (DBT_DEVICEQUERYREMOVE) that the Windows Operating System broadcasts to applications that are "listening" for it. I'm trying to utilise this message to prevent the removal of the device.
It has been stated on the MSDN site, that you need to use the DEV_BROADCAST_HANDLE structure, open a file on the USB drive, and insert the file's handle into the structure; followed by a call to the system's RegisterDeviceNotification function.
I have successfully received the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE messages by using the DEV_BROADCAST_DEVICEINTERFACE structure, but I'm having troubles using the DEV_BROADCAST_HANDLE structure. It fails at the RegisterDeviceNotification line, returning a standard error code of 0, and a DLL error code of 1066. Declarations: - Public Declare Function CallWindowProcA Lib "user32" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
-
Public Declare Function CreateFileA Lib "kernel32" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
-
Public Declare Function RegisterDeviceNotificationA Lib "user32" (ByVal hRecipient As Long, NotificationFilter As DEV_BROADCAST_HANDLE, ByVal Flags As Long) As Long
-
Public Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
-
-
Public Type GUID
-
D1(3) As Byte
-
D2(1) As Byte
-
D3(1) As Byte
-
D4(7) As Byte
-
End Type
-
-
Public Type DEV_BROADCAST_DEVICEINTERFACE
-
dbcc_size As Long
-
dbcc_devicetype As Long
-
dbcc_reserved As Long
-
dbcc_classguid As GUID
-
dbcc_name As String
-
End Type
-
-
Public Type DEV_BROADCAST_HANDLE
-
dbch_size As Long
-
dbch_devicetype As Long
-
dbch_reserved As Long
-
dbch_handle As Long
-
dbch_devnotify As Long
-
dbch_eventguid As GUID
-
dbch_nameoffset As Long
-
dbch_data(1023) As Byte
-
End Type
-
-
Public Const DBT_DEVICEQUERYREMOVE As Long = 32769
-
Public Const DBT_DEVTYP_DEVICEINTERFACE As Long = 5
-
Public Const DBT_DEVTYP_HANDLE As Long = 6
-
Public Const DEVICE_NOTIFY_WINDOW_HANDLE As Long = 0
-
Public Const DEVICE_NOTIFY_ALL_INTERFACE_CLASSES As Long = 4
-
Public Const FILE_ATTRIBUTE_NORMAL As Long = 128
-
Public Const FILE_SHARE_READ As Long = 1
-
Public Const GENERIC_READ As Long = &H80000000
-
Public Const GWL_WNDPROC As Long = -4
-
Public Const WM_DEVICECHANGE As Long = &H219
-
General Code: -
Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
-
-
If uMsg = WM_DEVICECHANGE Then
-
MsgBox "DeviceChange message received. " & wParam & " " & lParam
-
If wParam = DBT_DEVICEQUERYREMOVE Then
-
MsgBox "DeviceQueryRemove message received."
-
End If
-
End If
-
-
WindowProc = CallWindowProcA(hUSBProc, hWnd, uMsg, wParam, lParam)
-
-
End Function
-
-
Sub Main
-
-
Form1.Hide
-
hFile = CreateFileA("E:\MYFILE.TXT", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
-
With USBNotify
-
.dbch_size = Len(USBNotify)
-
.dbch_devicetype = DBT_DEVTYPE_HANDLE
-
.dbch_reserved = 0
-
.dbch_handle = hFile
-
End With
-
I = RegisterDeviceNotificationA(Form1.hWnd, USBNotify, DEVICE_NOTIFY_WINDOW_HANDLE Or DEVICE_NOTIFY_ALL_INTERFACE_CLASSES)
-
hUSBProc = SetWindowLongA(Form1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
-
-
End Sub
-
3 5645
Hello.
I'm trying to create a program in Visual Basic 6 that detects when a user is attempting to remove a USB drive, by using the "Safely Remove Hardware" icon on the taskbar. There is supposed to be a message (DBT_DEVICEQUERYREMOVE) that the Windows Operating System broadcasts to applications that are "listening" for it. I'm trying to utilise this message to prevent the removal of the device.
It has been stated on the MSDN site, that you need to use the DEV_BROADCAST_HANDLE structure, open a file on the USB drive, and insert the file's handle into the structure; followed by a call to the system's RegisterDeviceNotification function.
I have successfully received the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE messages by using the DEV_BROADCAST_DEVICEINTERFACE structure, but I'm having troubles using the DEV_BROADCAST_HANDLE structure. It fails at the RegisterDeviceNotification line, returning a standard error code of 0, and a DLL error code of 1066.
hmmm not sure if its relevant but might be...
Have you checked the program on a different pc or with a different operating system.
it might be the os that is to blame
The main Windows Operating System that I use to test programs on is Windows XP. I don't see why this sort of code shouldn't work on Windows XP.
I plan to use the program on multiple Windows Operating System platform versions (i.e. Windows 98, Me, NT, 2000, etc.), however. I believe all of the necessary API functions exist within each of these versions.
I'm still looking for a way of getting the program to pick up the DBT_DEVICEQUERYREMOVE message. I have satisfied the RegisterDeviceNotification function by providing a handle to the USB device, but still can't get the message.
Here's the updated version: Declarations: -
Public Declare Function CallWindowProcA Lib "user32" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
-
Public Declare Function CreateFileA Lib "kernel32" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
-
Public Declare Function RegisterDeviceNotificationA Lib "user32" (ByVal hRecipient As Long, NotificationFilter As DEV_BROADCAST_HANDLE, ByVal Flags As Long) As Long
-
Public Declare Function SetWindowLongA Lib "user32" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
-
-
Public Type GUID
-
D1 As Long
-
D2 As Integer
-
D3 As Integer
-
D4(7) As Byte
-
End Type
-
-
Public Type DEV_BROADCAST_HANDLE
-
dbch_size As Long
-
dbch_devicetype As Long
-
dbch_reserved As Long
-
dbch_handle As Long
-
dbch_devnotify As Long
-
dbch_eventguid As GUID
-
dbch_nameoffset As Long
-
dbch_data As Byte
-
End Type
-
-
Public Const DBT_DEVICEQUERYREMOVE As Long = 32769
-
Public Const DBT_DEVTYP_HANDLE As Long = 6
-
Public Const DEVICE_NOTIFY_WINDOW_HANDLE As Long = 0
-
Public Const FILE_SHARE_READ As Long = 1
-
Public Const FILE_SHARE_WRITE As Long = 2
-
Public Const GENERIC_READ As Long = &H80000000
-
Public Const GWL_WNDPROC As Long = -4
-
Public Const WM_DEVICECHANGE As Long = 537
-
-
Public hFile As Long, hUSBProc As Long
-
Public USBNotify As DEV_BROADCAST_HANDLE
General Code: -
Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
-
-
If uMsg = WM_DEVICECHANGE Then
-
MsgBox "DeviceChange message received. " & wParam & " " & lParam
-
If wParam = DBT_DEVICEQUERYREMOVE Then
-
'Still not working!
-
MsgBox "DeviceQueryRemove message received."
-
End If
-
End If
-
-
WindowProc = CallWindowProcA(hUSBProc, hWnd, uMsg, wParam, lParam)
-
-
End Function
-
-
Sub Main
-
-
Form1.Hide
-
hFile = CreateFileA("USB Mass Storage Device", GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0)
-
With USBNotify
-
.dbch_size = LenB(USBNotify)
-
.dbch_devicetype = DBT_DEVTYPE_HANDLE
-
.dbch_handle = hFile
-
End With
-
I = RegisterDeviceNotificationA(Form1.hWnd, USBNotify, DEVICE_NOTIFY_WINDOW_HANDLE)
-
hUSBProc = SetWindowLongA(Form1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
-
-
End Sub
If anyone has the answer, your help would be greatly appreciated.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dipak Patel |
last post by:
Platform: Win2000 SP3, SQL 7.00 - 7.00.1063
I have a SQL-authenticated user with the following permissions:
"Process Administrators" server role
"db_owner" and "TargetServersRole" for msdb...
|
by: ja |
last post by:
I am trying to write a program that will allow me to unplug a USB mass storage device from a Windows 2000 box without getting the "Unsafe Removal of Device" warning
I tried the following program...
|
by: Mike Bennett |
last post by:
Does the .NET framework (using VB.NET) support the ability to programmatically STOP a device prior to its removal from the system?
I have need to move data from a computer on one network to a...
|
by: Viper99 |
last post by:
I need to detect or be notified of PCMCIA device removal using C# .Net.
|
by: Paul Steele |
last post by:
I am writing a C# app that needs to periodically poll for cdroms and usb
storage device insertions. I've looked at the WMI functions but haven't
found anything all that useful. The closest is...
|
by: Charles Law |
last post by:
Is there an event or notification that my application can receive when a USB
device (or other) is plugged in?
I have looked at WMI as this appears to be the right area but cannot see how
to get...
|
by: GovardhanReddy |
last post by:
Hi
Can any body help me on window service desktop alerts or notifications.
My question is i am working on windows services,I need to generate desktop Notifications using my services.I did...
|
by: JoderCoder |
last post by:
In my application that does some audio-related stuff, I constantly receive WM_DEVICECHANGE - DBT_DEVICEARRIVAL from the Vista Business OS, although there is no actual device arrivals nor removals. I...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |