private void OpenWiimoteDeviceHandle(string devicePath)
{
// open a read/write handle to our device using the DevicePath returned
wiiHandle = HIDImports.CreateFile(devicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, HIDImports.EFileAttributes.Overlapped, IntPtr.Zero);
// open a read/write handle to the PDA
pdaHandle = HIDImports.CreateFile("\\\\.\\COM20", FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, HIDImports.EFileAttributes.Overlapped, IntPtr.Zero);
// create an attributes struct and initialize the size
HIDImports.HIDD_ATTRIBUTES attrib = new HIDImports.HIDD_ATTRIBUTES();
attrib.Size = Marshal.SizeOf(attrib);
// get the attributes of the current device
if (HIDImports.HidD_GetAttributes(wiiHandle.DangerousGetHandle(), ref attrib))
{
// if the vendor and product IDs match up
if (attrib.VendorID == VID && attrib.ProductID == PID)
{
// create a nice .NET FileStream wrapping the handle above
wiiStream = new FileStream(wiiHandle, FileAccess.ReadWrite, REPORT_LENGTH, true);
// create a FileStream wrapping the pdaHandle
pdaStream = new FileStream(pdaHandle, FileAccess.ReadWrite, REPORT_LENGTH, true);
// Loop and wait for the PDA to write bytes
PDA_ReadWriteLoop();
}
else
{
// otherwise this isn't the controller, so close up the file handle
wiiHandle.Close();
throw new WiimoteException("Attempted to open a non-Wiimote device.");
}
}
}