title: How to use deviceiocontrol function properly
body: <p>This is my code:</p>
  
  <p>Code:</p>
  
  <pre><code>HANDLE HandelUsb= CreateFile(L"\\\\.\\G:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  if (HandelUsb == INVALID_HANDLE_VALUE)
  {
      printf("Terminal failure: Unable to open usb ERROR CODE:0x%x\n", GetLastError());
      return 1;
  }
  
  DISK_GEOMETRY d = { 0 };
  DWORD dwReturned = 0;
  
  int gs =DeviceIoControl(HandelUsb, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &amp;d, sizeof(DISK_GEOMETRY),&amp;dwReturned, NULL);
  printf("%d\n", gs);
  
  if (d.MediaType == RemovableMedia)
  {
      DWORD dwReturned2 = 0;
      MEDIA_SERIAL_NUMBER_DATA data={ 0 };
      if (DeviceIoControl(HandelUsb, IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, NULL, 0, &amp;data,sizeof(MEDIA_SERIAL_NUMBER_DATA), &amp;dwReturned2, NULL)) {
              printf("SerialNumberLength %d\nResult %d\nReserved[2] %s\nSerialNumberData[1] %s ", data.SerialNumberLength, data.Result, data.Reserved, data.SerialNumberData);
      }
      else {
              printf("faild to get serial number ERROR CODE:0x%x\n", GetLastError());
  
      }
  }
  
  CloseHandle(HandelUsb);
  return 0;
  </code></pre>
  
  <p>Everything works well except for the function
  Code:</p>
  
  <pre><code>DeviceIoControl(HandelUsb, IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, NULL, 0, &amp;data,sizeof(MEDIA_SERIAL_NUMBER_DATA), &amp;dwReturned2, NULL)
  </code></pre>
  
  <p>It always fails (returns a value of 0), GetLastError returns the value 0X1 (problem function).</p>
  
  <p>Maybe someone here can show me what I'm missing here?</p>
  
  <p>Following the comment of @IInspectable ,i changed the code to it:</p>
  
  <pre><code>    //volume handle to device handle
  VOLUME_DISK_EXTENTS volumeToDevice = { 0 };
  DWORD Returned = 0;
  DeviceIoControl(HandelToUsb,IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,NULL,0,&amp;volumeToDevice,sizeof(VOLUME_DISK_EXTENTS),&amp;Returned,NULL);
  
  
  WCHAR volume[150] ;
  swprintf_s(volume, L"\\\\.\\PhysicalDrive%d", volumeToDevice.Extents[0].DiskNumber);
  wprintf(L"%s\n", volume);
  
  //handle to phisicalDrive
  HANDLE HandelUsb = CreateFile(volume, 0, FILE_SHARE_READ |FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  DWORD dwReturned2 = 0;
      MEDIA_SERIAL_NUMBER_DATA data={ 0 };
      if (DeviceIoControl(HandelUsb, IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, NULL, 0, &amp;data,sizeof(MEDIA_SERIAL_NUMBER_DATA), &amp;dwReturned2, NULL)) {
              printf("SerialNumberLength %d\nResult %d\nReserved[2] %s\nSerialNumberData[1] %s ", data.SerialNumberLength, data.Result, data.Reserved, data.SerialNumberData);
      }
      else {
              printf("faild to get serial number ERROR CODE:0x%x\n", GetLastError());
  
      }
  </code></pre>
  
  <p>I still get a failure 0X1 on function</p>
  
  <pre><code> DeviceIoControl (Handel Usb, IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, NULL, 0, &amp; data, sizeof (MEDIA_SERIAL_NUMBER_DATA), &amp; dwReturned2, NULL)
  </code></pre>
  
  <ul>
  <li>I ran Windus 7</li>
  <li>i tried to Change the permissions function CreateFile (Add GENERIC_EXECUTE , replace to GENERIC_ALL add FILE_ATTRIBUTE_NORMAL ), play with the parameters of the DeviceIoControl.</li>
  </ul>