找了不少以前的文章,还是没有解决我的问题
读取SD卡的序列号,老实说,我还没有做到,我不知是为什么,下边的代码希望能抛砖引玉,解决好这个问题。
问题的关键所在:在WINCE下,IOCTL_DISK_GET_STORAGEID 的定义在哪里?下边的这段代码应该能读出来CF卡的序列号,但SD卡不行。提示错误是:参数错误,我看过,在WIN2K下,CTL_CODE 被定义成 CTL_CODE(IOCTL_STORAGE_BASE, 0x304, METHOD_BUFFERED, FILE_ANY_ACCESS),在CE下是0X709,读SD卡会不会不同呢?IOCTL_STORAGE_BASE 到底该定义成多少呢? 在MSDN种好像没有说明,谁有这样的文档?共享一下,共同进步好吗?
#define IOCTL_STORAGE_BASE FILE_DEVICE_DFS
#ifndef IOCTL_DISK_GET_STORAGEID
#define IOCTL_DISK_GET_STORAGEID CTL_CODE(IOCTL_STORAGE_BASE, 0x709, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif
BOOL GetSDCardSerialNumber(CString strCardName,CString &strManufactureID, CString &strSerialNum)
{
BOOL bRet = FALSE;
HANDLE hVolume = NULL;
BYTE *pbySerialNo = NULL;
BYTE *pbyManuID = NULL;
DWORD dwBytesRet = 0;
DWORD dwReqSize = 0;
PSTORAGE_IDENTIFICATION pStoreInfo = NULL;
strCardName += TEXT("\\Vol:");
hVolume=CreateFile(strCardName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (hVolume != NULL && hVolume != INVALID_HANDLE_VALUE)
{
pStoreInfo=(STORAGE_IDENTIFICATION *)GlobalAlloc(GMEM_ZEROINIT,sizeof(STORAGE_IDENTIFICATION));
pStoreInfo->dwSize = 0;
bRet = DeviceIoControl(hVolume,IOCTL_DISK_GET_STORAGEID,NULL,0,
(LPVOID)pStoreInfo,sizeof(STORAGE_IDENTIFICATION), &dwBytesRet,NULL);
dwReqSize = pStoreInfo->dwSize;
GlobalFree(pStoreInfo);
DWORD dwErr = GetLastError();
CString strTemp;
if( (FALSE != bRet) || (ERROR_INSUFFICIENT_BUFFER != dwErr))
{
strTemp.Format(TEXT("Err1, %d \n %s"),dwErr,gGetErrorMessage());
MessageBox(strTemp);
CloseHandle(hVolume);
return FALSE;
}
ASSERT(dwReqSize > 0);
pStoreInfo=(STORAGE_IDENTIFICATION *)GlobalAlloc(GMEM_ZEROINIT,dwReqSize);
pStoreInfo->dwSize = dwReqSize;
bRet = DeviceIoControl(hVolume,IOCTL_DISK_GET_STORAGEID,NULL,0,
(LPVOID)pStoreInfo,dwReqSize, &dwBytesRet,NULL);
if((FALSE == bRet) || (ERROR_INSUFFICIENT_BUFFER == GetLastError()) )
{
MessageBox(TEXT("ERR2"));
GlobalFree(pStoreInfo);
CloseHandle(hVolume);
return FALSE;
}
pbySerialNo=(((BYTE *)pStoreInfo)+pStoreInfo->dwSerialNumOffset);
pbyManuID =(((BYTE *)pStoreInfo)+pStoreInfo->dwManufactureIDOffest);
int i=0;
while (pbySerialNo!=0 && i<200 &&
i<(int)(dwBytesRet- pStoreInfo->dwSerialNumOffset))
{
i++;
}
pbySerialNo = '\0';
i=0;
while(pbyManuID !=0 && i<200 &&
i<(int)(pStoreInfo->dwSerialNumOffset - pStoreInfo->dwManufactureIDOffest))
{
i++;
}
pbyManuID = '\0';
strManufactureID = (char*)pbyManuID;
strSerialNum = (char*)pbySerialNo;
GlobalFree(pStoreInfo);
CloseHandle(hVolume);
return TRUE;
}
return FALSE;
}
使用FILE_DEVICE_DISK
另外 HANDLE hCard = CreateFile(TEXT("DSK1:"), //设备标识DISK1由查询注册表获得