Search code examples
c#unity-game-enginefilestreamhidwiimote

Createfile works, but handle is invalid on FileStream Construction


I'm trying to connect to a Wiimote in Unity3D through HID.

When I call CreateFile() (from kernel32) a "valid" pointer is returned, and I can even get the Wiimote's capabilities through HidD_GetPreparsedData and HidP_GetCaps. Buffer-size etc. is returned, and is the size that I'm expecting. However, when I try to open a FileStream on the handle, it errors with "Invalid Handle".

How can this handle be invalid here if I can GetCaps? Marshal32.GetLastWinError() is also returning 0.

Relevant code:

int i = 0;
foreach (string devPath in devicePaths)
{
    Debug.Log(i);
    i++;
    if (devPath.Contains(VID_NINTENDO))
        if (devPath.Contains(PID_WIIMOTE)) // ADD OTHER PID (IF EVER FOUND)
        {
            try
            {
                IntPtr dev_Handle = CreateFile(devPath, FileAccess.Read | FileAccess.Write, FileShare.Read | FileShare.Write, IntPtr.Zero, FileMode.Open, FILE_FLAG_OVERLAPPED, IntPtr.Zero); // Gives a "Valid" pointer
                IntPtr device_data = IntPtr.Zero;
                int inputLength = 0;
                try
                {
                    if (!HidD_GetPreparsedData(dev_Handle, out device_data))
                    {
                        throw new HIDException("Unable to get Preparsed data from device");
                    }
                    HidCaps device_capabilities; // Struct to contain inputlength etc.
                    HidP_GetCaps(device_data, out device_capabilities);
                    inputLength = device_capabilities.InputReportByteLength;
                    int outputLength = device_capabilities.OutputReportByteLength;
                    FileStream stream = new FileStream(dev_Handle, FileAccess.Read | FileAccess.Write, true, inputLength); // INVALID HANDLE
                    //FileStream stream = new FileStream(new SafeFileHandle(dev_Handle, false), FileAccess.Read | FileAccess.Write, inputLength, true); // Invalid Handle
                    //FileStream stream = new FileStream(dev_Handle, FileAccess.Read | FileAccess.Write, true, inputLength); // Invalid Handle
                    //FileStream stream = new FileStream(devPath, FileMode.Open, FileAccess.Read | FileAccess.Write, FileShare.Read | FileShare.Write, inputLength); // nullref (The file isnt being created in this path)
                    Debug.Log(dev_Handle);
                }
                catch (Exception e)
                {
                    HandleException(e, "HIDException");
                }
                finally
                {
                    HidD_FreePreparsedData(ref device_data);
                }
            }
            catch (Exception e)
            {
                HandleException(e, "HIDException");
            }
        }                            
    }

Also, in a regular C# project it simply works :( (although I did have to set useAsync = true in the FileStream Constructor (still Invalid Handle in Unity though)


Solution

  • Finally got it working through the use of kernel32.dll's ReadFile Method... Apparently Unity has problems with SafeFileHandles.. :S Using this API: http://buiba.blogspot.nl/2009/06/using-winapi-createfile-readfile.html Makes reading possible