[DllImport("hid.dll")]
private unsafe static extern Boolean HidD_GetInputReport(int hidDeviceObject, byte[] buffer, int bufferLength);
[DllImport("hid.dll")]
private unsafe static extern Boolean HidD_SetOutputReport(int hidDeviceObject, byte[] buffer, int bufferLength);
[DllImport("hid.dll")]
private unsafe static extern Boolean HidD_GetSerialNumberString(int hidDeviceObject, IntPtr buffer, int bufferLength);
[DllImport("user32.dll", SetLastError = true)]
public unsafe static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, Int32 Flags);
[DllImport("hid.dll")]
public static extern bool HidD_FlushQueue(int HidDeviceObject);
[DllImport("hid.dll")]
public unsafe static extern void HidD_GetHidGuid(ref Guid HidGuid);
[DllImport("setupapi.dll", SetLastError = true)]
public unsafe static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, uint Enumerator, IntPtr HwndParent, DIGCF Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public unsafe static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
[StructLayout(LayoutKind.Sequential)]
public unsafe class DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}
public unsafe struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid interfaceClassGuid;
public int flags;
public int reserved;
}
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
private unsafe static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData,
int deviceInterfaceDetailDataSize, ref int requiredSize, SP_DEVINFO_DATA deviceInfoData); [StructLayout(LayoutKind.Sequential)]
public unsafe class SP_DEVINFO_DATA
{
public int cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
public Guid classGuid = Guid.Empty; // temp
public int devInst = 0; // dumy
public int reserved = 0;
} [StructLayout(LayoutKind.Sequential, Pack = 2)]
internal unsafe struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
internal int cbSize;
internal short devicePath;
} public enum DIGCF
{
DIGCF_DEFAULT = 0x1,
DIGCF_PRESENT = 0x2,
DIGCF_ALLCLASSES = 0x4,
DIGCF_PROFILE = 0x8,
DIGCF_DEVICEINTERFACE = 0x10
} [DllImport("Kernel32.dll", SetLastError = true)]
private unsafe static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
ref uint lpNumberOfBytesWritten,
IntPtr lpOverlapped
); //獲取設備文件
[DllImport("kernel32.dll", SetLastError = true)]
private unsafe 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")]
public unsafe static extern int CloseHandle(IntPtr hObject); [DllImport("setupapi.dll", SetLastError = true)]
internal unsafe static extern IntPtr SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet); [DllImport("hid.dll", SetLastError = true)]
public unsafe static extern Boolean HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes); [DllImport("hid.dll", SetLastError = true)]
public unsafe static extern int HidD_GetPreparsedData(int HidHandle, ref int preparsedDataPointer); [DllImport("hid.dll", SetLastError = true)]
private unsafe static extern int HidP_GetCaps(int pPHIDP_PREPARSED_DATA, ref HIDP_CAPS myPHIDP_CAPS); public struct HIDD_ATTRIBUTES //HID屬性
{
public Int32 Size;
public Int32 VendorID;
public Int32 ProductID;
public Int32 VersionNumber;
}
[StructLayout(LayoutKind.Sequential)]
public struct HIDP_CAPS
{
public UInt16 Usage;
public UInt16 UsagePage;
public UInt16 InputReportByteLength;
public UInt16 OutputReportByteLength;
public UInt16 FeatureReportByteLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]
public UInt16[] Reserved;
public UInt16 NumberLinkCollectionNodes;
public UInt16 NumberInputButtonCaps;
public UInt16 NumberInputValueCaps;
public UInt16 NumberInputDataIndices;
public UInt16 NumberOutputButtonCaps;
public UInt16 NumberOutputValueCaps;
public UInt16 NumberOutputDatadices;
public UInt16 NumberFeatureButtonCaps;
public UInt16 NumberFeatureValueCaps;
public UInt16 NumberFeatureDataIndices;
}
private unsafe static extern Boolean HidD_GetInputReport(int hidDeviceObject, byte[] buffer, int bufferLength);
[DllImport("hid.dll")]
private unsafe static extern Boolean HidD_SetOutputReport(int hidDeviceObject, byte[] buffer, int bufferLength);
[DllImport("hid.dll")]
private unsafe static extern Boolean HidD_GetSerialNumberString(int hidDeviceObject, IntPtr buffer, int bufferLength);
[DllImport("user32.dll", SetLastError = true)]
public unsafe static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, Int32 Flags);
[DllImport("hid.dll")]
public static extern bool HidD_FlushQueue(int HidDeviceObject);
[DllImport("hid.dll")]
public unsafe static extern void HidD_GetHidGuid(ref Guid HidGuid);
[DllImport("setupapi.dll", SetLastError = true)]
public unsafe static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, uint Enumerator, IntPtr HwndParent, DIGCF Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public unsafe static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
[StructLayout(LayoutKind.Sequential)]
public unsafe class DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}
public unsafe struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid interfaceClassGuid;
public int flags;
public int reserved;
}
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
private unsafe static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData,
int deviceInterfaceDetailDataSize, ref int requiredSize, SP_DEVINFO_DATA deviceInfoData); [StructLayout(LayoutKind.Sequential)]
public unsafe class SP_DEVINFO_DATA
{
public int cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
public Guid classGuid = Guid.Empty; // temp
public int devInst = 0; // dumy
public int reserved = 0;
} [StructLayout(LayoutKind.Sequential, Pack = 2)]
internal unsafe struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
internal int cbSize;
internal short devicePath;
} public enum DIGCF
{
DIGCF_DEFAULT = 0x1,
DIGCF_PRESENT = 0x2,
DIGCF_ALLCLASSES = 0x4,
DIGCF_PROFILE = 0x8,
DIGCF_DEVICEINTERFACE = 0x10
} [DllImport("Kernel32.dll", SetLastError = true)]
private unsafe static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
ref uint lpNumberOfBytesWritten,
IntPtr lpOverlapped
); //獲取設備文件
[DllImport("kernel32.dll", SetLastError = true)]
private unsafe 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")]
public unsafe static extern int CloseHandle(IntPtr hObject); [DllImport("setupapi.dll", SetLastError = true)]
internal unsafe static extern IntPtr SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet); [DllImport("hid.dll", SetLastError = true)]
public unsafe static extern Boolean HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes); [DllImport("hid.dll", SetLastError = true)]
public unsafe static extern int HidD_GetPreparsedData(int HidHandle, ref int preparsedDataPointer); [DllImport("hid.dll", SetLastError = true)]
private unsafe static extern int HidP_GetCaps(int pPHIDP_PREPARSED_DATA, ref HIDP_CAPS myPHIDP_CAPS); public struct HIDD_ATTRIBUTES //HID屬性
{
public Int32 Size;
public Int32 VendorID;
public Int32 ProductID;
public Int32 VersionNumber;
}
[StructLayout(LayoutKind.Sequential)]
public struct HIDP_CAPS
{
public UInt16 Usage;
public UInt16 UsagePage;
public UInt16 InputReportByteLength;
public UInt16 OutputReportByteLength;
public UInt16 FeatureReportByteLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]
public UInt16[] Reserved;
public UInt16 NumberLinkCollectionNodes;
public UInt16 NumberInputButtonCaps;
public UInt16 NumberInputValueCaps;
public UInt16 NumberInputDataIndices;
public UInt16 NumberOutputButtonCaps;
public UInt16 NumberOutputValueCaps;
public UInt16 NumberOutputDatadices;
public UInt16 NumberFeatureButtonCaps;
public UInt16 NumberFeatureValueCaps;
public UInt16 NumberFeatureDataIndices;
}
{
Guid HID_Guid = Guid.Empty;
IntPtr hDevInfo = IntPtr.Zero; //HID 設備集合
Regex Reg_Pid = new Regex(Pid);
Regex Reg_Vid = new Regex(Vid);
string devicePathName = string.Empty;
int bufferSize = 0;
bool result = true;
IntPtr detailDataBuffer = IntPtr.Zero;
IntPtr pdevicePathName = IntPtr.Zero;
uint DeviceSerialNumber = 0;
HidD_GetHidGuid(ref HID_Guid);
hDevInfo = SetupDiGetClassDevs(ref HID_Guid, 0, IntPtr.Zero, DIGCF.DIGCF_PRESENT | DIGCF.DIGCF_DEVICEINTERFACE);
//獲取設備,true獲取到
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData);
while (result)
{
result = SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref HID_Guid, DeviceSerialNumber, ref DeviceInterfaceData);
if (result)
{
//第一次調用出錯,但可以返回正確的size
SP_DEVINFO_DATA strtInterfaceData = new SP_DEVINFO_DATA();
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, ref DeviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, strtInterfaceData);
//第二次調用傳遞返回值,調用即可成功
detailDataBuffer = Marshal.AllocHGlobal(bufferSize);
SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
detailData.cbSize = Marshal.SizeOf(typeof(SP_DEVICE_INTERFACE_DETAIL_DATA));
Marshal.StructureToPtr(detailData, detailDataBuffer, false);
result = SetupDiGetDeviceInterfaceDetail(hDevInfo, ref DeviceInterfaceData, detailDataBuffer, bufferSize, ref bufferSize, strtInterfaceData);
if (result)
{
DeviceSerialNumber++;
pdevicePathName = (IntPtr)((int)detailDataBuffer + 4);
devicePathName = Marshal.PtrToStringAuto(pdevicePathName);
Match Match_Pid = Reg_Pid.Match(devicePathName);
Match Match_Vid = Reg_Vid.Match(devicePathName);
if (Match_Pid.Success && Match_Vid.Success)
{
ArrayList_Hid_DevicePath.Add(devicePathName);
}
Marshal.FreeHGlobal(detailDataBuffer);
}
else
{
Flag = false;
SetupDiDestroyDeviceInfoList(hDevInfo);
lbl_Display_Dev.Text = "No SWROT Headset Connected";
lbl_Display_Dev.ForeColor = System.Drawing.Color.Red;
}
}
else
{
SetupDiDestroyDeviceInfoList(hDevInfo);
}
}
if (ArrayList_Hid_DevicePath.Count == 0)
{
Flag = false;
SetupDiDestroyDeviceInfoList(hDevInfo);
MessageBox.Show("未找到hid設備");
}
{
if (Flag)
{
for (int i = 0; i < ArrayList_Hid_DevicePath.Count; i++)
{
if (i == 0)
{
CreateFileHandle = CreateFile(ArrayList_Hid_DevicePath[i].ToString(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, 3, 0, 0);
GetReportByteLength(CreateFileHandle);
}
if (i == 1)
{
CreateFileHandle_1 = CreateFile(ArrayList_Hid_DevicePath[i].ToString(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, 3, 0, 0);
GetReportByteLength_1(CreateFileHandle_1);
}
} } }
public void USBDatawrite()
{
bool isread = true;
string tmpReceviceData = string.Empty;
bool UnderGrowd_Red_Light = false;
bool UnderGrowd_Green_Light = false;
bool UnderGrowd_Blue_Light = false;
bool Center_TrunOn_Light = false;
bool Center_TrunOff_Light = false;
string ReceviceData = string.Empty;
Byte[] m_rd_data = new Byte[2];
uint read = 0; //下面是给USB发送数据的部分
RedLightTest();
// HidD_FlushQueue(CreateFileHandle_overlp_1);
Thread.Sleep(200);
if (TestLight("Is the color RED?", "Underglow Light"))
{
UnderGrowd_Red_Light = true;
lbl_Display_Red.Text = "Pass";
lbl_Display_Red.ForeColor = System.Drawing.Color.Green;
}
GreenLightTest();
// HidD_FlushQueue(CreateFileHandle_overlp_1);
Thread.Sleep(200);
if (TestLight("Is the color Green?", "Underglow Light"))
{
UnderGrowd_Green_Light = true;
lbl_Display_Green.Text = "Pass";
lbl_Display_Green.ForeColor = System.Drawing.Color.Green;
}
BlueLightTest();
// HidD_FlushQueue(CreateFileHandle_overlp_1);
Thread.Sleep(200);
if (TestLight("Is the color Blue?", "Underglow Light"))
{
UnderGrowd_Blue_Light = true;
lbl_Display_Blue.Text = "Pass";
lbl_Display_Blue.ForeColor = System.Drawing.Color.Green;
} TurnOn_LightTest();
//HidD_FlushQueue(CreateFileHandle_overlp_1);
Thread.Sleep(200);
if (TestLight("Is the light On?", "Center Logo"))
{
Center_TrunOn_Light = true;
lbl_Display_Turn_On.Text = "Pass";
lbl_Display_Turn_On.ForeColor = System.Drawing.Color.Green;
}
TurnOff_LightTest();
//HidD_FlushQueue(CreateFileHandle_overlp_1);
Thread.Sleep(200);
if (TestLight("Is the light Off?", "Center Logo"))
{
Center_TrunOff_Light = true;
lbl_Display_Turn_Off.Text = "Pass";
lbl_Display_Turn_Off.ForeColor = System.Drawing.Color.Green;
}
//此部分发送数据偶尔会是电脑蓝屏
蓝屏代码如下:
Driver_irql_not_less_or_equal
stop:0x000000d1(0xba6a0000,0x00000002,0x00000000,0xba37a9d07)
usbuhci.sys -address ba37a9d0 base at 378000,datestamp 480254ce
}
private void FlushQueue(int HidHandle) //清除隊列
{
HidD_FlushQueue(HidHandle);
}
private bool TestLight(string message, string caption)
{
try
{
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result = MessageBox.Show(message, caption, buttons);
if (result == DialogResult.Yes)
{
return true;
}
else
{
return false;
}
}
catch (System.Exception ex)
{ MessageBox.Show(ex.ToString());
return false;
} }
public void hid_outputreport(byte[] USBData)
{
HidD_SetOutputReport(CreateFileHandle, USBData, Outreportlength);
} public void CloseUsbHandle(IntPtr HidHandle) //關閉設備
{
CloseHandle(HidHandle);
} private void GetReportByteLength(int FileHandle) //獲取報告長度
{
int PreparsedData = -1;
int r1 = HidD_GetPreparsedData(FileHandle, ref PreparsedData);
HIDP_CAPS caps = new HIDP_CAPS();
int r2 = HidP_GetCaps(PreparsedData, ref caps);
Inreportlength = caps.InputReportByteLength;
Outreportlength = caps.OutputReportByteLength;
}
private void GetReportByteLength_1(int FileHandle) //獲取報告長度
{
int PreparsedData = -1;
int r1 = HidD_GetPreparsedData(FileHandle, ref PreparsedData);
HIDP_CAPS caps = new HIDP_CAPS();
int r2 = HidP_GetCaps(PreparsedData, ref caps);
Inreportlength_1 = caps.InputReportByteLength;
Outreportlength_1 = caps.OutputReportByteLength;
} private void GetHidHandle()
{
GetHidDevicePath("002c", "1532");
CT_CreateFile();
}
private void button7_Click(object sender, EventArgs e)
{
InitialData();
GetHidHandle();
if (Flag)
{
Thread Thread_USB_ReadData = new Thread(new ThreadStart(USBDataRead));
Thread_USB_ReadData.Start();
}
}
private void RedLightTest() //Red light Test
{
byte[] buffer = new byte[] { 06, 03, 192, 00, 162, 255, 00, 00 };
//USBDataWrite(buffer);
hid_outputreport(buffer);
}
private void GreenLightTest() //Red light Test
{
byte[] buffer = new byte[] { 06, 03, 192, 00, 162, 00, 255, 00 };
//USBDataWrite(buffer);
hid_outputreport(buffer);
} private void BlueLightTest() //Blue Light Test
{
byte[] buffer = new byte[] { 06, 03, 192, 00, 162, 00, 00, 255 };
//USBDataWrite(buffer);
hid_outputreport(buffer);
} private void TurnOn_LightTest() //turn On Light Test
{
byte[] buffer1 = new byte[] { 06, 03, 192, 00, 162, 00, 00, 00 };
byte[] buffer2 = new byte[] { 06, 01, 192, 00, 168, 255, 00, 00 };
byte[] buffer3 = new byte[] { 06, 01, 192, 00, 165, 00, 00, 00 };
byte[] buffer4 = new byte[] { 06, 01, 192, 00, 168, 255, 00, 00 };
byte[] buffer5 = new byte[] { 06, 01, 192, 00, 165, 255, 00, 00 };
// USBDataWrite(buffer1);
//USBDataWrite(buffer2);
//USBDataWrite(buffer3);
// USBDataWrite(buffer4);
// USBDataWrite(buffer5);
hid_outputreport(buffer1);
hid_outputreport(buffer2);
hid_outputreport(buffer3);
hid_outputreport(buffer4);
hid_outputreport(buffer5);
}
private void TurnOff_LightTest()//turn Off Light Test
{
byte[] buffer1 = new byte[] { 06, 01, 192, 00, 168, 255, 00, 00 };
byte[] buffer2 = new byte[] { 06, 01, 192, 00, 165, 00, 00, 00 };
//USBDataWrite(buffer1);
// USBDataWrite(buffer2);
hid_outputreport(buffer1);
hid_outputreport(buffer2);
}
}}问题在红色部分。