阅读:1180回复:0
如何获取SD 卡的 Serail Number ?这是我在网上找到的一段代码,但我的代码却读不出来?请高手指正我哪里写错了。 我的编译环境是 EVC 3.0 MFC void CReadSdCardSnDlg::OnButton1() { CCeFileFind find; CString strMId,strSdSn,strText; if(find.FindFlashCard()) { find.FindNextCard(); if(find.IsStorageCard()) { strText.Format(TEXT(\"Stroage Card : %s\"),find.GetFilePath()); MessageBox(strText); GetSDCardSerialNumber(find.GetFilePath(),strMId,strSdSn); } find.Close(); } strText.Format(TEXT(\"ID: %s \\n SN: %s\"),strMId,strSdSn); MessageBox(strText); } typedef struct _STORAGE_IDENTIFICATION { DWORD dwSize; DWORD dwFlags; DWORD dwManufactureIDOffest; DWORD dwSerialNumOffset; } STORAGE_IDENTIFICATION, *PSTORAGE_IDENTIFICATION; #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 CReadSdCardSnDlg::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; } |
|
|