2012-02-07 10 views
2

HIDデバイスに書き込んでいて、WriteFile関数で無効なパラメータ87を取得しているプログラムで作業しています。私はJan AxelsonのUSB Completeの機能を持っているので、なぜエラーが出るのか分かりません。 私は私のデバイスを見つけるために、これを使用しています:エラーコード87を返すWriteFile 87

private void USBInit() 
    { 
     IntPtr deviceInfoSet; 
     Int32 memberIndex = 0; 
     SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); 
     Int32 bufferSize = 0; 
     IntPtr detailDataBuffer; 
     Boolean success = false; 
     deviceFound = false; 

     HidD_GetHidGuid(ref hidGuid);   // Get the GUID 

     deviceInfoSet = SetupDiGetClassDevs  // Get pointer to a device info set 
      (ref hidGuid, 
      IntPtr.Zero, 
      IntPtr.Zero, 
      DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 

     do 
     { 
      MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData); // Identify Device Interface 
      success = SetupDiEnumDeviceInterfaces 
       (deviceInfoSet, 
       IntPtr.Zero, 
       ref hidGuid, 
       memberIndex, 
       ref MyDeviceInterfaceData); 

      success = SetupDiGetDeviceInterfaceDetail   // Request Structure with Device Path Name 
       (deviceInfoSet, 
       ref MyDeviceInterfaceData, 
       IntPtr.Zero, 
       0, 
       ref bufferSize, 
       IntPtr.Zero); 

      detailDataBuffer = Marshal.AllocHGlobal(bufferSize); 
      Marshal.WriteInt32(detailDataBuffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8); 

      success = SetupDiGetDeviceInterfaceDetail 
       (deviceInfoSet, 
       ref MyDeviceInterfaceData, 
       detailDataBuffer, 
       bufferSize, 
       ref bufferSize, 
       IntPtr.Zero); 

      IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4); 
      devicePathName = Marshal.PtrToStringAuto(pDevicePathName); 
      Marshal.FreeHGlobal(detailDataBuffer); 

      /* Request Communications Handle */ 
      deviceHandle = CreateFile 
       (devicePathName, 
       (GENERIC_WRITE | GENERIC_READ), 
       FILE_SHARE_READ | FILE_SHARE_WRITE, 
       IntPtr.Zero, 
       OPEN_EXISTING, 
       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 
       0); 

      /* Get Vendor ID and Product ID */ 
      DeviceAttributes.Size = Marshal.SizeOf(DeviceAttributes); 
      success = HidD_GetAttributes(deviceHandle, ref DeviceAttributes); 

      // Compare Vendor ID and Product ID. 
      if ((DeviceAttributes.VendorID == myVendorID) && (DeviceAttributes.ProductID == myProductID)) 
      { 
       MessageBoxResult res = System.Windows.MessageBox.Show("Device Found", "K-IX", MessageBoxButton.OK); 
       deviceFound = true; 

       /* Get pointer to capabilities */ 
       success = HidD_GetPreparsedData(deviceHandle, ref preparsedData); 

       /* Get Device Capabilities */ 
       Int32 result = 0; 
       result = HidP_GetCaps(preparsedData, ref Capabilities); 

       return; 
      } 
      else 
      { 
       // Not my device 
       memberIndex++; 
       deviceHandle.Close(); 
       if (memberIndex == 128) 
       { break; } 
      } 
     } while (!deviceFound); 
    } 

そして、これは私がデバイスに送信しようとする使用していたコードです:

private void SendUSB() 
    { 
     Int32 numberOfBytesWritten = 0; 
     Byte[] outputReportBuffer = null; 
     Boolean success; 
     Boolean success2; 
     // Set size of the Output report buffer. 
     Array.Resize(ref outputReportBuffer, Capabilities.InputReportByteLength); 

     // Store Report ID in first byte of header. 
     outputReportBuffer[0] = 0; 

     // Store report data following the Report ID. 
     outputReportBuffer[1] = 0x01; 
     //outputReportBuffer[2] = 0x02; 
     // outputReportBuffer[3] = 0x03; 
     // Send Report 
     success = WriteFile 
      (deviceHandle, 
      outputReportBuffer, 
      outputReportBuffer.Length, 
      ref numberOfBytesWritten, 
      IntPtr.Zero); 

     if (!success) 
     { 
      Int32 lastError = Marshal.GetLastWin32Error(); 
     } 
     success2 = HidD_FreePreparsedData(preparsedData); 
    } 

私は私のレポートの長さが予想されることを確認しましたデバイスは2ですが、USBとHIDのプログラミングは私のために新しいのでここからどこに行くのか分かりません。

+0

私はHIDデバイスを扱う専門家ではありませんが、Scott Hanselmanの "[それはあなたですか?クールなUSBハードウェア用に優れたソフトウェアを作成しています](http://channel9.msdn.com/coding4fun/articles/Is-that上記のコードよりもはるかに少ないC++イディオムを使用しているので、参考になるかもしれません。 – Powerlord

答えて

7

CreateFile()呼び出しでFILE_FLAG_OVERLAPPEDを指定しましたが、WriteFile()呼び出しでlpOverlapped引き数にnullを渡しました。それは合法ではありません。

FILE_FLAG_OVERLAPPEDオプションを削除します。

+0

完璧、ありがとうございます。 –

関連する問題