title: Cannot communicate successfully with USB HID device using writefile()
body: <p>I am fairly new to C# as well as windows programming and I am attempting to establish communication between a USB HID device and an app written in c# on a windows xp pc. I have obtained the device path successfully and have used this to establish a valid Handle on the device: </p>
  
  <pre><code>IntPtr drive = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE,
                 FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero,
                 OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, IntPtr.Zero);
  </code></pre>
  
  <p>This proved to be a valid handle that yields a positive integer when tested. After creating the handle I call 'HidD_GetPreparsedData' and 'HidP_GetCaps' which yield a struct previously imported (HIDP_CAPS) that stores specific data related to the device attached. Next, I try to call writefile():</p>
  
  <pre><code>bool success = WriteFile(drive, ref outputReportBuffer, 
                 caps.OutputReportByteLength, ref numberOfBytesWritten, 
                 IntPtr.Zero); 
  </code></pre>
  
  <p>drive: The Handle of the device</p>
  
  <p>outputReportBuffer:The array of bytes to write [65]</p>
  
  <p>caps.OutputReportByteLength: The amount of bytes to write, caps is the struct of HIDP_CAPS</p>
  
  <p>numberOfBytesWritten: hardcoded to 0</p>
  
  <p>IntPtr.Zero: Everything I have read passes this last parameter as I have done. In the formal parameters of 'writefile' the last parameter is listed as 'IntPtr ipOverlapped', but I have not been able to figure out what that is, except that It should be passed as some form of 0</p>
  
  <p>How WriteFile is imported:</p>
  
  <pre><code>[DllImport("kernel32.dll", SetLastError = true)]
  public static extern bool WriteFile(IntPtr hFile, 
  ref byte[] lpBuffer, 
  uint nNumberOfBytesToWrite,
  ref uint lpNumberOfBytesWritten, 
  IntPtr ipOverlapped);
  </code></pre>
  
  <p>When writefile is executed it simply does nothing. I get no error, but when tested, it reveals that communication was never established and it was not successful. If anyone could share any knowledge or provide any resources to help I would really appreciate it. Thank you for reading.</p>
  
  <p>Regards.</p>