我给一个usb设备发送数据的时候,出现了错误.错误代码为1784,意思说用户请求的buffer无效.
这个该怎么解决.
谢谢.
这个该怎么解决.
谢谢.
解决方案 »
- datagridview能实现下面功能吗?
- 高分求助,不够可以再开贴,Outlook AddIns,怎样修改选定Email的图标。
- Oracle数据库表结构导入到SQL SERVER
- 多事务处理?Gridview取值
- 为什么编译代码的时候主机会发出“嘀”的一声
- 无法启动windows service
- ajax问题?!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 求C#初学的书
- 能不能在一个类里面,复制以前保存过的一个实例到本类中来?
- C#读文本文件中的汉字出错!怎么办
- IBaitsNet查询参数问题
- 如何手工调用DataGridView的OnCellPainting事件
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();
}
}
}
上面是我的程序,请帮忙看看