打印

获取SD卡CID

[复制链接]
2883|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
MOSHEN|  楼主 | 2013-7-15 14:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
SD卡, ic, ce, AN, vi
请问如何在pc端获取USB读卡器里SD卡的CID 信息???用VC++的。
下面的代码是否可以获取sd卡cid信息??
为什么 DeviceIoControl()返回0,GetLastError();返回为50,不支持请求。错在哪里了。跪求指导。。。
void GetSD_CID()
{
  // TODO: Add your control notification handler code here
  //CComboBoxEx * pComBox = (CComboBoxEx *) GetDlgItem(IDC_COMBO1);
  TCHAR StrDeviceName[64] = {0};
  wsprintf(StrDeviceName,_T("\\\\.\\%C:"),'G'/*pComBox->GetCurSel()*/);
  // Create device handle
  HANDLE hVol = CreateFile(StrDeviceName,
    GENERIC_READ|GENERIC_WRITE,                // no access to the drive
    FILE_SHARE_READ | // share mode
    FILE_SHARE_WRITE,
    NULL,             // default security attributes
    OPEN_EXISTING,    // disposition
    0,                // file attributes
    NULL);            // do not copy file attributes
  //CString strOutputMsg;
  UCHAR ucSDCID[SD_CID_LENGTH] = {0};
  if(INVALID_HANDLE_VALUE != hVol && NULL != hVol){
    // Structure used in IOCTL_SFFDISK_DEVICE_COMMAND
    // The layout of the buffer passed to this IOCTL is as follows:
    //
    //        +-----------------------------+
    //        | header (this structure)     |
    //        +-----------------------------+
    //        | protocol arguments          |
    //        +-----------------------------+
    //        | device data buffer          |
    //        +-----------------------------+
    //
    // The actual layout of the protocol arguments depends on the protocol of
    // the target device. So as an example, if the target device an SD (Secure Digital)
    // storage device, then the protocol arguments would consist of an SDCMD_DESCRIPTOR,
    // which is defined in SDDEF.H. The SD argument for the command should be stored
    // in the "Information" field of this structure. In that case, ProtocolArgumentSize
    // would be sizeof(SDCMD_DESCRIPTOR).
    //
    // The three size fields in the structure (HeaderSize, ProtocolArgumentSize,
    // DeviceDataBufferSize) each hold the length in bytes of each respective area as
    // described by the diagram above. Thus, the entire length of the buffer must be
    // at least as large as the sum of these three fields.
    //
    int nSizeOfCmd = sizeof(SFFDISK_DEVICE_COMMAND_DATA) + sizeof(SDCMD_DESCRIPTOR) + SD_CID_LENGTH;
    SFFDISK_DEVICE_COMMAND_DATA *pCmdBuf = (SFFDISK_DEVICE_COMMAND_DATA*) new BYTE[nSizeOfCmd];
        SFFDISK_DEVICE_COMMAND_DATA *pCmdBufout = (SFFDISK_DEVICE_COMMAND_DATA*) new BYTE[nSizeOfCmd];
    ULONG_PTR info = 0;
    memset(pCmdBuf, 0, nSizeOfCmd);      
    pCmdBuf->HeaderSize = sizeof (SFFDISK_DEVICE_COMMAND_DATA);
    pCmdBuf->Command = SFFDISK_DC_DEVICE_COMMAND;
    pCmdBuf->ProtocolArgumentSize = sizeof (SDCMD_DESCRIPTOR);
    pCmdBuf->DeviceDataBufferSize = SD_CID_LENGTH;
    pCmdBuf->Information = info;
    ///Command protocol
    SDCMD_DESCRIPTOR sdCmdDescriptor = { 0 };
    sdCmdDescriptor.Cmd = SFFDISK_SC_GET_CARD_VERSION;   // Issue CMD10 to driver
    sdCmdDescriptor.CmdClass = SDCC_STANDARD;
    sdCmdDescriptor.TransferDirection = SDTD_READ;
    sdCmdDescriptor.TransferType = SDTT_CMD_ONLY;
    sdCmdDescriptor.ResponseType = SDRT_1;      
/*    memcpy((BYTE *)(&(pCmdBuf->Data[0])), &sdCmdDescriptor, sizeof(SDCMD_DESCRIPTOR));   */   
    memcpy(pCmdBuf->Data, &sdCmdDescriptor, sizeof(SDCMD_DESCRIPTOR));   
    DWORD dwBytesReturned = 0;
    BOOL bResult = DeviceIoControl(hVol,
      IOCTL_SFFDISK_DEVICE_COMMAND,
      pCmdBuf,
      nSizeOfCmd,
      pCmdBufout,
      nSizeOfCmd,
      &dwBytesReturned,
      NULL);      
    DWORD dwErr = GetLastError();
    CloseHandle(hVol);
    int nCIDBaseIndex = sizeof(SDCMD_DESCRIPTOR);
    if(!bResult){
      printf("Failed to get CID, err code is |%d|",dwErr);
    }else{
    }
    ////////////////////////////////////////////////////////////////
    delete [] pCmdBuf;
  }else{
    //strOutputMsg.Format(L"Failed to open device handle |%s|", StrDeviceName);
  }
}

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

2

帖子

0

粉丝