none
Please tell me what's the problem with the following code? RRS feed

  • Question

  • How to get the CID of SD card  under windows xp?

    Some code for example:

    The DeviceIoControl return zero and the GetLastError return 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);
      }
    }

    Please help me.Thanks!

    Wednesday, July 17, 2013 9:57 AM

All replies

  • Thanks for your reply;

    My card is generic USB storage .

    But i tried the code the DeviceIoControl return zero and the GetLastError return 50;

    Thursday, July 18, 2013 2:23 AM