Hello, I am reading the serial number of the CF and SD cards in an iPaq
hx2415. If I turn the device off and on after reading the serial number of
the CF card, I can no longer access the card, except for listing files (in
File Explorer). Any attempt to read from or write to the card fails, as does
attempting to issue a new IOCTL. If I reset the device, it asks for the name
of the driver for the card (doesn't recognize it as a storage card). The
only way to fix it is to remove and reinsert the card. It is reading the
serial numbers successfully, as long as I do not turn it off with the CF
card in the slot. I HAVE traced it

I am using an MFC dialog app in eVC4. This is what you would need to
reproduce the problem:

#include <WinIoCtl.h>
#include <memory.h>

#define IOCTL_DISK_BASE FILE_DEVICE_DISK
#define IOCTL_DISK_GET_STORAGEID CTL_CODE(IOCTL_DISK_BASE, 0x709,
METHOD_BUFFERED, FILE_ANY_ACCESS)

typedef struct _STORAGE_IDENTIFICATION {
DWORD dwSize;
DWORD dwFlags;
DWORD dwManufactureIDOffset;
DWORD dwSerialNumOffset;
} STORAGE_IDENTIFICATION, *PSTORAGE_IDENTIFICATION;

#define MANUFACTUREID_INVALID 0x01
#define SERIALNUM_INVALID 0x02

(the data is read out byte swapped)

void swapBytes(char* buff, DWORD len) {
char b;
for (int i = 0; i < len; i += 2) {
b = buff[i];
buff[i] = buff[i + 1];
buff[i + 1] = b;
}
}

(and the member function I am using to read the numbers)

void CSnreadDlg::UpdateSns() {
char cfNum[100] = {0}, sdNum[100] = {0};
char tmp[30];
HANDLE cf, sd;
DWORD len;
BYTE buff[128];
PSTORAGE_IDENTIFICATION sid = (PSTORAGE_IDENTIFICATION)buff;
cf = CreateFile(L"\\CF Card\\Vol:", GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE , NULL, OPEN_EXISTING,
0, NULL);
if (cf == INVALID_HANDLE_VALUE) {
AfxMessageBox(L"Error opening CF volume");
}
sd = CreateFile(L"\\SD Card\\Vol:", GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE , NULL, OPEN_EXISTING,
0, NULL);
if (sd == INVALID_HANDLE_VALUE) {
AfxMessageBox(L"Error opening SD volume");
}

memset(buff, 0, 128);
if (DeviceIoControl(cf, IOCTL_DISK_GET_STORAGEID, NULL, 0,
sid, 128, &len, NULL)) {
if (sid->dwManufactureIDOffset > 0) {
memset(tmp, 0, 30);
if (sid->dwSerialNumOffset > 0) {
memcpy(tmp, buff + sid->dwManufactureIDOffset,
sid->dwSerialNumOffset - sid->dwManufactureIDOffset);
} else {
memcpy(tmp, buff + sid->dwManufactureIDOffset,
sid->dwSize - sid->dwManufactureIDOffset);
}
swapBytes(tmp, 30);
strcat(cfNum, tmp);
strcat(cfNum, "/");
}
if (sid->dwSerialNumOffset > 0) {
memset(tmp, 0, 30);
memcpy(tmp, buff + sid->dwSerialNumOffset,
sid->dwSize - sid->dwSerialNumOffset);
swapBytes(tmp, 30);
strcat(cfNum, tmp);
}
}

memset(buff, 0, 128);
if (DeviceIoControl(sd, IOCTL_DISK_GET_STORAGEID, NULL, 0,
sid, 128, &len, NULL)) {
if (sid->dwManufactureIDOffset > 0) {
memset(tmp, 0, 30);
if (sid->dwSerialNumOffset > 0) {
memcpy(tmp, buff + sid->dwManufactureIDOffset,
sid->dwSerialNumOffset - sid->dwManufactureIDOffset);
} else {
memcpy(tmp, buff + sid->dwManufactureIDOffset,
sid->dwSize - sid->dwManufactureIDOffset);
}
swapBytes(tmp, 30);
strcat(sdNum, tmp);
strcat(sdNum, "/");
}
if (sid->dwSerialNumOffset > 0) {
memset(tmp, 0, 30);
memcpy(tmp, buff + sid->dwSerialNumOffset,
sid->dwSize - sid->dwSerialNumOffset);
swapBytes(tmp, 30);
strcat(sdNum, tmp);
}
}
CloseHandle(cf);
CloseHandle(sd);
SetDlgItemText(IDC_EDIT1, CString(sdNum));
SetDlgItemText(IDC_EDIT2, CString(cfNum));
}