If you are as curious as I am, you have probably wondered whether or not it is possible to determine if a memory stick was plugged in or taken out. To answer your question, yes you can! As the saying goes: Curiosity killed the cat. In this case, if you are not careful, curiosity can cause very serious errors, even some bad, unpredictable results. So, if you are careful and do things right, everything will work smoothly, so make sure to follow every step carefully, and double check your progress against mine.
I will cover how to determine whether or not a disk is plugged in or taken out in this article—that is the main topic, but another “thing” I’ve been wondering about was: How can you make the disk Autorun, similar to CD drives, and, how can you run a normal VB 6 program from this disk, without the need of installing it on the client machine? Sound interesting? Well, it’s time to get started.
What Is Needed?
The obvious answer would be: A Lot Of APIs. Now, for the inexperienced VB 6 developer, the Win 32 Application Programming Interface would seem too advanced or too complicated to go into. To put you a bit more at ease, I won’t be shy in mentioning the fact that many a experienced VB 6 developer finds the API just as scary as you! You may think that if experienced developers find APIs scary, they must indeed be extremely difficult. No; with time you will realise that the Win 32 API is extremely huge—there are literally millions of pages written on the various APIs found—and still some of the APIs remain a mystery. In case you haven’t run away in fear for the APIs, you could have a look at these resources about the API.
- Application Programming Interface
- AllAPI.net (Mentalis.org)
- Discovering the API Using Visual Basic 6 and Visual Basic .NET
The Needed APIs
In the sample program, you will be using the following APIs:
|The SetWindowLong function changes an attribute of the specified window.
The function also sets the 32-bit (long) value at the specified offset into the extra window memory.
|The CallWindowProc function passes message information to the specified window procedure
|The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive
|The RtlMoveMemory routine moves memory either forward or backward, aligned or unaligned, in 4-byte blocks, followed by any remaining bytes
|The GetDWORD method retrieves a DWORD property
|GetWORD method retrieves a WORD property
|The DeviceIoControl function sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.
|The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object: → files → pipes → mailslots → communications resources → disk devices (Windows NT only) → consoles → directories (open only)
|The CloseHandle function closes an open object handle.
|The RegisterDeviceNotification function registers the device or type of device for which a window will receive notifications
|The UnregisterDeviceNotification function closes the specified device notification handle
The Needed API Types
To make all these APIs work properly, you need a couple of API Types, and several API Constants. In the sample program, you will be working with the following API Types:
|API Type Name
|API Type Description/Usage
|The DEV_BROADCAST_HDR structure is a standard header for information related to a device event reported through the WM_DEVICECHANGE message
|The DEV_BROADCAST_DEVICEINTERFACE structure contains information about a class of devices
The Needed API Constants
You will need the following Constants in your sample program:
|API Constant Name
|API Constant Description/Use
|Use the GWL_WNDPROC constant to tell the SetWindowLong function that you want to change the address of the target window’s WindowProc function
|The WM_DEVICECHANGE device message notifies an application of a change to the hardware configuration of a device or the computer
|The system broadcasts the DBT_DEVICEARRIVAL device event when a device or piece of media has been inserted and becomes available
|The system broadcasts the DBT_DEVICEREMOVECOMPLETE device event when a device or piece of media has been physically removed
|The application must check the event to ensure that the type of device arriving is a volume. Logical volume
|Device interface class
|Causes media to be ejected from a SCSI device
|Subsequent open operations on the object will succeed only if read access is requested
|Subsequent open operations on the object will succeed only if write access is requested
|Opens the file
|If the function fails, the return value is INVALID_HANDLE_VALUE
|Handle to the window or service that will receive device events for the devices specified in the NotificationFilter parameter
|Device interface class
|Notifies the recipient of device interface events for all device interface classes