我给一个usb设备发送数据的时候,出现了错误.错误代码为1784,意思说用户请求的buffer无效.
这个该怎么解决.
谢谢.

解决方案 »

  1.   

    http://www.ooppoo.com/html/78/n-233178.html
      

  2.   

    谢谢hefeng_aspnet的回答,我的错误是1784 用户请求的buffer无效
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.ComponentModel;
    using System.Windows.Forms;namespace usbwritefile
    {
      class Program  {
      int HidHandle = -1;
      public const uint GENERIC_READ = 0x80000000;
      public const uint GENERIC_WRITE = 0x40000000;
      public const uint FILE_SHARE_READ = 0x00000001;
      public const uint FILE_SHARE_WRITE = 0x00000002;
      public const int OPEN_EXISTING = 3;
      //獲取設備文件
      [DllImport("kernel32.dll", SetLastError = true)]
      private static extern int CreateFile(
      string lpFileName, // file name
      uint dwDesiredAccess, // access mode
      uint dwShareMode, // share mode
      uint lpSecurityAttributes, // SD
      uint dwCreationDisposition, // how to create
      uint dwFlagsAndAttributes, // file attributes
      uint hTemplateFile // handle to template file
      );
      [DllImport("Kernel32.dll")]
      private static extern bool ReadFile(
      IntPtr hFile,
      byte[] lpBuffer,
      uint nNumberOfBytesToRead,
      ref uint lpNumberOfBytesRead,
      IntPtr lpOverlapped
      );
      [DllImport("Kernel32.dll", SetLastError = true)]
      private static extern bool WriteFile(
      IntPtr hFile,
      byte[] lpBuffer,
      uint nNumberOfBytesToWrite,
      ref uint lpNumberOfBytesWritten,
      IntPtr lpOverlapped
      );
      [DllImport("kernel32.dll")]
      private static extern bool CloseHandle(
      IntPtr hObject
      );
      //--------------------------------------------------------------------------------  
      IntPtr hFile;
      private const Int32 INVALID_HANDLE_VALUE = -1;
      private const int USB_WRITENUM = 8;
      private const int USB_READNUM = 8;
      private byte[] m_rd_data = new byte[USB_READNUM];
      public byte[] rd_data
      {
      get { return m_rd_data; }
      set { m_rd_data = value; }
      }
      private byte[] m_wr_data = new byte[USB_WRITENUM];
      public byte[] wr_data
      {
      get { return m_wr_data; }
      set { m_wr_data = value; }
      }
      public int OnInitUSB(string DeviceName)
      {
      HidHandle = CreateFile(
      DeviceName,
      GENERIC_READ | GENERIC_WRITE,//讀寫,或者一起
      FILE_SHARE_READ,// | FILE_SHARE_WRITE,//共享讀寫,或者一起
      0,
      OPEN_EXISTING,
      0,
      0);
      if (HidHandle == -1)
      {
      return 0;
      }
      else
      {
      return 1;
      }
      }
      public bool USBDataRead()
      {
      uint read = 0;
      return ReadFile(hFile, m_rd_data, (uint)USB_READNUM, ref read, IntPtr.Zero);
      }
      public bool USBDataWrite()
      {
      uint bytesWritten = 0;
      byte[] buffer = new byte[] { 0x02, 0x02, 0x00 };
      bool status = WriteFile((IntPtr)HidHandle, buffer, 8, ref bytesWritten, IntPtr.Zero);
      if (!status)
      {
      int errorCode = Marshal.GetLastWin32Error();
      string errorMessage = new Win32Exception(errorCode).Message;
      Console.WriteLine(errorCode + ":" + errorMessage);
      MessageBox.Show(errorCode + ":" + errorMessage);
      return false;
      }
      else
      {
      Console.WriteLine(bytesWritten.ToString());
      return true;
      }
      }
      public void CloseConnection()
      {
      if (hFile.ToInt32() != INVALID_HANDLE_VALUE)
      {
      CloseHandle(hFile);
      hFile = IntPtr.Zero;
      }
      }    static void Main(string[] args)
      {
      Program p = new Program();
      p.OnInitUSB("\\\\?\\hid#vid_046d&pid_0a29&mi_03&col02#7&ca16861&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}");
      p.USBDataWrite();
      Console.ReadKey();
          }
      }
    }
    上面是我的程序,请帮忙看看
      

  3.   

    缓冲区大小不对,发送最前边字节要多一个ID,无ID为0 。