问题描述
我是相当新的C#以及Windows编程,我试图建立通信之间的USB HID设备。我得到了成功的设备路径使用SetupDiGetDevicexxxxxx和使用的CreateFile()来获得处理。 。下面是我的代码
公共常量UINT FILE_SHARE_READ = 00000001;
公共常量UINT FILE_SHARE_WRITE = 0x00000002;
公共const int的OPEN_EXISTING = 3;
公共常量UINT GENERIC_READ = 0x80000000的;
公共常量UINT GENERIC_WRITE = 0x40000000之后;
的CreateFile(串的DevicePath)
{
HidHandle =的CreateFile(DevicePath中,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
}
获取设备句柄信息,我如下调用写文件功能后。
结果= WriteFile的(HidHandle,outputReportBuffer [],outputReportBuffer.Length,NumberOfBytesWritten,0);
OutputBuffer中是一个长度为8,出于某种原因的字节数组,我是不是能够写入USB HID设备。 结果始终为零。任何帮助表示赞赏。此外,任何一个可以告诉我如何验证 HidHandle
是有效与否。当我跑我正在为1124的节目
我也跟着上这类问题以前的帖子:的无法顺利沟通,但没有帮助。
下面是创建文件和WriteFile两个贪利。
函数[DllImport(KERNEL32.DLL,SetLastError = TRUE)]
私人静态外部INT的CreateFile(字符串lpFileName的对象,UINT dwDesiredAccess,UINT dwShareMode,UINT lpSecurityAttributes,UINT CREATE_NEW标志,UINT dwFlagsAndAttributes,UINT hTemplateFile);
函数[DllImport(KERNEL32.DLL)]
静态公网的extern BOOL WriteFile的(INT HFILE,字节lpBuffer,诠释nNumberOfBytesToWrite,诠释lpNumberOfBytesWritten,诠释lpOverlapped的)
您可能会需要使用USB库,像的
I am fairly new to C# as well as windows programming and I am attempting to establish communication between a USB HID device. I got the device path successfully using 'SetupDiGetDevicexxxxxx' and used 'CreateFile()' to get Handle. Below is my code.
public const uint FILE_SHARE_READ = 0x00000001;
public const uint FILE_SHARE_WRITE = 0x00000002;
public const int OPEN_EXISTING = 3;
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
CreateFile(string Devicepath)
{
HidHandle = CreateFile(Devicepath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
}
After obtaining Device handle info, I am calling write file functions as below.
Result = WriteFile(HidHandle, outputReportBuffer[], outputReportBuffer.Length, NumberOfBytesWritten, 0);
Outputbuffer is the byte array of length 8. For some reason, I was not able to write to USB HID device. "Result" is always zero. Any Help is appreciated. Also, can any one tell me how to verify that HidHandle
is a valid or not. When I run the program I am getting it as "1124".
I did followed previous post on this type of question: Cannot communicate successfully with USB HID device using writefile(), but no help.
Below are the two menthods for create file and writefile.
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, uint lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, uint hTemplateFile );
[DllImport("kernel32.dll")]
static public extern bool WriteFile(int hFile, byte lpBuffer, int nNumberOfBytesToWrite, int lpNumberOfBytesWritten, int lpOverlapped)
You'll probably have to use a USB library, like this one
这篇关于C#WriteFile的(),无法写入USB HID设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!