I'm trying to automatically respond to the insertion of a USB memory stick in CVI 8.5. I found some sample C# code that showed how to interpret the data returned for a WM_DEVICECHANGE message. The problem is that when I cast the lParam returned to my callback to the appropriate structure, the data isn't correct. I install the callback using:
// install USB callback
InstallWinMsgCallback (panelHandle, WM_DEVICECHANGE, UsbCallback, VAL_MODE_IN_QUEUE, NULL, &junk);
Note: I wan't quite sure about the usage of the last parameter, so I'm passing a junk integer.
My callback looks like this:
int CVICALLBACK UsbCallback (int panelHandle, int message, unsigned int* wParam, unsigned int* lParam, void* callbackData)
{
struct _DEV_BROADCAST_HEADER *DeviceHeader = (struct _DEV_BROADCAST_HEADER *)lParam;
unsigned int DeviceType = DeviceHeader->dbcd_devicetype;
switch (*wParam)
{
case DBT_DEVICEARRIVAL:
if (DeviceType == DBT_DEVTYP_VOLUME)
{
DEV_BROADCAST_VOLUME *DeviceHeader = (DEV_BROADCAST_VOLUME *)lParam;
char DriveLetter = DriveMaskToLetter(DeviceHeader->dbcv_unitmask);
} /* if-then */
break;
} /* switch */
return 0;
} /* UsbCallback */
The structures are defined in DBT.H, a Windows header I downloaded from MSDN.
When I insert my USB stick I get the DBT_DEVICE_ARRIVAL message. But when I cast the lParam to the _DEV_BROADCAST_HEADER header, the data is all wrong. The definition for this structure is:
struct _DEV_BROADCAST_HEADER { /* */
DWORD dbcd_size;
DWORD dbcd_devicetype;
DWORD dbcd_reserved;
};
According to
MSDN, the size is supposed to be the size of the device and the type is one of the following:
|
DBT_DEVTYP_DEVICEINTERFACE 0x00000005 |
|
|
DBT_DEVTYP_HANDLE 0x00000006 |
|
|
DBT_DEVTYP_OEM 0x00000000 |
|
|
DBT_DEVTYP_PORT 0x00000003 |
|
|
DBT_DEVTYP_VOLUME 0x00000002 |
When I set a breakpoint inside the case statement, insert the key, and then exmaine the header structure I see this:
DeviceHeader->dbcd_size = 1244468
DeviceHeader->dbcd_devicetype = 4423667
The size is wrong (It's a 1GB stick) and the device type is an illegal value.
Am I casting the lParam incorrectly? Is there some other step I have to take to extract the data from lParam? When I install the callback, does the last parameter have anything to do with this? Am I missing something obvious?