向USB设备发送SCSI命令

http://bbs3.driverdevelop.com/simple/?t84347.html

{
  BOOL status = 0;
  DWORD accessMode = 0, shareMode = 0;
  HANDLE fileHandle = NULL;
//PUCHAR dataBuffer = NULL;
  SCSI_PASS_THROUGH_WITH_BUFFERS sptwb;
  SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER sptdwb;
  UCHAR buffer[ 2048 ];
  UCHAR string[ 25 ];
  ULONG length = 0, errorCode = 0;
  DWORD returned = 0;

  strcpy(string, "\\\\.\\");
  strcat(string, "I:");

  shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;//default
  accessMode = GENERIC_WRITE | GENERIC_READ;//default

  fileHandle = CreateFile( string, accessMode, shareMode, NULL, OPEN_EXISTING, 0,
      NULL );

  if (fileHandle == INVALID_HANDLE_VALUE)
  {
    MessageBox(NULL, "Create file fail", "tester", MB_OK);
    return false;
  }

  ZeroMemory(&sptwb, sizeof(SCSI_PASS_THROUGH_WITH_BUFFERS));

  sptwb.spt.Length = sizeof(SCSI_PASS_THROUGH);
  sptwb.spt.PathId = 0;
  sptwb.spt.TargetId = 1;
  sptwb.spt.Lun = 0;
  sptwb.spt.CdbLength = CDB6GENERIC_LENGTH;
  sptwb.spt.SenseInfoLength = 24;
  sptwb.spt.DataIn = SCSI_IOCTL_DATA_IN;
  sptwb.spt.DataTransferLength = 192;
  sptwb.spt.TimeOutValue = 2;
  sptwb.spt.DataBufferOffset =
  offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS,ucDataBuf);
  sptwb.spt.SenseInfoOffset =
  offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS,ucSenseBuf);
  sptwb.spt.Cdb[0] = SCSI_OPCODE_HWTEST_COMMAND; //SCSI_OPCODE_SIGMATEL_WRITE_COMMAND;//
  sptwb.spt.Cdb[1] = HWTEST_DISPLAY_ID;//ALLOCATE_MEDIA;
  sptwb.spt.Cdb[4] = 192;
  length = offsetof( SCSI_PASS_THROUGH_WITH_BUFFERS, ucDataBuf )
  + sptwb.spt.DataTransferLength;

  status = DeviceIoControl( fileHandle, IOCTL_SCSI_PASS_THROUGH, &sptwb,
      sizeof(SCSI_PASS_THROUGH), &sptwb, length, &returned, FALSE );

  if (!status)
  {
    MessageBox(NULL, "Device I/0 control fail!", "tester", MB_OK);
    errorCode = GetLastError();
    ShowMessage(IntToStr(errorCode));
    PrintError(errorCode);
    CloseHandle(fileHandle);
    return false;
  }

  CloseHandle( fileHandle );
  return true;
}

 

posted @ 2013-06-30 22:21  IAmAProgrammer  阅读(4866)  评论(0编辑  收藏  举报