Ejemplo n.º 1
0
        private void initDevice(string devicePath, bool useAsyncReads)
        {
            deviceConnected = false;

            //create file handles using CT_CreateFile
            handle_read = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
                                     IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

            handle_write = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
                                      IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

            //get capabilites - use getPreParsedData, and getCaps
            //store the reportlengths
            IntPtr ptrToPreParsedData = new IntPtr();
            bool   ppdSucsess         = HidD_GetPreparsedData(handle_read, ref ptrToPreParsedData);

            capabilities = new HIDP_CAPS();
            int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities);

            HIDD_ATTRIBUTES attributes       = new HIDD_ATTRIBUTES();
            bool            hidAttribSucsess = HidD_GetAttributes(handle_read, ref attributes);

            string productName = "";
            string SN          = "";
            string manfString  = "";
            IntPtr buffer      = Marshal.AllocHGlobal(126);//max alloc for string;

            if (HidD_GetProductString(handle_read, buffer, 126))
            {
                productName = Marshal.PtrToStringAuto(buffer);
            }
            if (HidD_GetSerialNumberString(handle_read, buffer, 126))
            {
                SN = Marshal.PtrToStringAuto(buffer);
            }
            if (HidD_GetManufacturerString(handle_read, buffer, 126))
            {
                manfString = Marshal.PtrToStringAuto(buffer);
            }
            Marshal.FreeHGlobal(buffer);

            //Call freePreParsedData to release some stuff
            HidD_FreePreparsedData(ref ptrToPreParsedData);
            //SetupDiDestroyDeviceInfoList(i);

            if (handle_read.IsInvalid)
            {
                return;
            }

            deviceConnected = true;

            //If connection was sucsessful, record the values in a global struct
            productInfo              = new interfaceDetails();
            productInfo.devicePath   = devicePath;
            productInfo.manufacturer = manfString;
            productInfo.product      = productName;
            //productInfo.serialNumber = Convert.ToInt32(SN);
            productInfo.PID           = (ushort)attributes.ProductID;
            productInfo.VID           = (ushort)attributes.VendorID;
            productInfo.versionNumber = (ushort)attributes.VersionNumber;
            //productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength;
            //productInfo.OUT_reportByteLength =(int)capabilities.OutputReportByteLength;

            //use a filestream object to bring this stuff into .NET
            FS_read  = new FileStream(handle_read, FileAccess.ReadWrite, 8 /*capabilities.OutputReportByteLength*/, false);
            FS_write = new FileStream(handle_write, FileAccess.ReadWrite, 8 /*capabilities.OutputReportByteLength*/, false);

            this.useAsyncReads = useAsyncReads;
            if (useAsyncReads)
            {
                readAsync();
            }
        }
Ejemplo n.º 2
0
        private void initDevice(string devicePath, bool useAsyncReads)
        {
            deviceConnected = false;

            //create file handles using CT_CreateFile
            handle_read = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
                IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

            handle_write = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
                IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

            //get capabilites - use getPreParsedData, and getCaps
            //store the reportlengths
            IntPtr ptrToPreParsedData = new IntPtr();
            bool ppdSucsess = HidD_GetPreparsedData(handle_read, ref ptrToPreParsedData);

            capabilities = new HIDP_CAPS();
            int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities);

            HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES();
            bool hidAttribSucsess = HidD_GetAttributes(handle_read, ref attributes);

            string productName = "";
            string SN = "";
            string manfString = "";
            IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string;
            if (HidD_GetProductString(handle_read, buffer, 126)) productName = Marshal.PtrToStringAuto(buffer);
            if (HidD_GetSerialNumberString(handle_read, buffer, 126)) SN = Marshal.PtrToStringAuto(buffer);
            if (HidD_GetManufacturerString(handle_read, buffer, 126)) manfString = Marshal.PtrToStringAuto(buffer);
            Marshal.FreeHGlobal(buffer);

            //Call freePreParsedData to release some stuff
            HidD_FreePreparsedData(ref ptrToPreParsedData);
            //SetupDiDestroyDeviceInfoList(i);

            if (handle_read.IsInvalid)
                return;

            deviceConnected = true;

            //If connection was sucsessful, record the values in a global struct
            productInfo = new interfaceDetails();
            productInfo.devicePath = devicePath;
            productInfo.manufacturer = manfString;
            productInfo.product = productName;
            productInfo.serialNumber = Convert.ToInt32(SN);
            productInfo.PID = (ushort)attributes.ProductID;
            productInfo.VID = (ushort)attributes.VendorID;
            productInfo.versionNumber = (ushort)attributes.VersionNumber;
            productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength;
            productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength;

            //use a filestream object to bring this stuff into .NET
            FS_read = new FileStream(handle_read, FileAccess.ReadWrite, capabilities.OutputReportByteLength, false);
            FS_write = new FileStream(handle_write, FileAccess.ReadWrite, capabilities.InputReportByteLength, false);

            this.useAsyncReads = useAsyncReads;
            if (useAsyncReads)
                readAsync();
        }
Ejemplo n.º 3
0
        public static interfaceDetails[] getConnectedDevices()
        {
            interfaceDetails[] devices = new interfaceDetails[0];

            //Create structs to hold interface information
            SP_DEVINFO_DATA          devInfo  = new SP_DEVINFO_DATA();
            SP_DEVICE_INTERFACE_DATA devIface = new SP_DEVICE_INTERFACE_DATA();

            devInfo.cbSize  = (uint)Marshal.SizeOf(devInfo);
            devIface.cbSize = (uint)(Marshal.SizeOf(devIface));

            Guid G = new Guid();

            HidD_GetHidGuid(ref G); //Get the guid of the HID device class

            IntPtr i = SetupDiGetClassDevs(ref G, IntPtr.Zero, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

            //Loop through all available entries in the device list, until false
            SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();

            if (IntPtr.Size == 8) // for 64 bit operating systems
            {
                didd.cbSize = 8;
            }
            else
            {
                didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // for 32 bit systems
            }
            int            j = -1;
            bool           b = true;
            int            error;
            SafeFileHandle tempHandle;

            while (b)
            {
                j++;

                b     = SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref G, (uint)j, ref devIface);
                error = Marshal.GetLastWin32Error();
                if (b == false)
                {
                    break;
                }

                uint   requiredSize = 0;
                bool   b1           = SetupDiGetDeviceInterfaceDetail(i, ref devIface, ref didd, 256, out requiredSize, ref devInfo);
                string devicePath   = didd.DevicePath;

                //create file handles using CT_CreateFile
                tempHandle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
                                        IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

                //get capabilites - use getPreParsedData, and getCaps
                //store the reportlengths
                IntPtr ptrToPreParsedData = new IntPtr();
                bool   ppdSucsess         = HidD_GetPreparsedData(tempHandle, ref ptrToPreParsedData);
                if (ppdSucsess == false)
                {
                    continue;
                }

                HIDP_CAPS capabilities   = new HIDP_CAPS();
                int       hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities);

                HIDD_ATTRIBUTES attributes       = new HIDD_ATTRIBUTES();
                bool            hidAttribSucsess = HidD_GetAttributes(tempHandle, ref attributes);

                string productName = "";
                string SN          = "";
                string manfString  = "";
                IntPtr buffer      = Marshal.AllocHGlobal(126);//max alloc for string;
                if (HidD_GetProductString(tempHandle, buffer, 126))
                {
                    productName = Marshal.PtrToStringAuto(buffer);
                }
                if (HidD_GetSerialNumberString(tempHandle, buffer, 126))
                {
                    SN = Marshal.PtrToStringAuto(buffer);
                }
                if (HidD_GetManufacturerString(tempHandle, buffer, 126))
                {
                    manfString = Marshal.PtrToStringAuto(buffer);
                }
                Marshal.FreeHGlobal(buffer);

                //Call freePreParsedData to release some stuff
                HidD_FreePreparsedData(ref ptrToPreParsedData);

                //If connection was sucsessful, record the values in a global struct
                interfaceDetails productInfo = new interfaceDetails();
                productInfo.devicePath           = devicePath;
                productInfo.manufacturer         = manfString;
                productInfo.product              = productName;
                productInfo.PID                  = (ushort)attributes.ProductID;
                productInfo.VID                  = (ushort)attributes.VendorID;
                productInfo.versionNumber        = (ushort)attributes.VersionNumber;
                productInfo.IN_reportByteLength  = (int)capabilities.InputReportByteLength;
                productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength;

                //if (utilities.utilities.stringIsInteger(SN))
                //    productInfo.serialNumber = Convert.ToInt32(SN);     //Check that serial number is actually a number
                //f**k you!
                productInfo.serialNumber = SN;

                int newSize = devices.Length + 1;
                Array.Resize(ref devices, newSize);
                devices[newSize - 1] = productInfo;
            }
            SetupDiDestroyDeviceInfoList(i);

            return(devices);
        }
Ejemplo n.º 4
0
        public static interfaceDetails[] getConnectedDevices()
        {
            interfaceDetails[] devices = new interfaceDetails[0];

            //Create structs to hold interface information
            SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA();
            SP_DEVICE_INTERFACE_DATA devIface = new SP_DEVICE_INTERFACE_DATA();
            devInfo.cbSize = (uint)Marshal.SizeOf(devInfo);
            devIface.cbSize = (uint)(Marshal.SizeOf(devIface));

            Guid G = new Guid();
            HidD_GetHidGuid(ref G); //Get the guid of the HID device class

            IntPtr i = SetupDiGetClassDevs(ref G, IntPtr.Zero, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

            //Loop through all available entries in the device list, until false
            SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
            if (IntPtr.Size == 8) // for 64 bit operating systems
                didd.cbSize = 8;
            else
                didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // for 32 bit systems

            int j = -1;
            bool b = true;
            int error;
            SafeFileHandle tempHandle;

            while (b)
            {
                j++;

                b = SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref G, (uint)j, ref devIface);
                error = Marshal.GetLastWin32Error();
                if (b == false)
                    break;

                uint requiredSize = 0;
                bool b1 = SetupDiGetDeviceInterfaceDetail(i, ref devIface, ref didd, 256, out requiredSize, ref devInfo);
                string devicePath = didd.DevicePath;

                //create file handles using CT_CreateFile
                tempHandle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
                    IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

                //get capabilites - use getPreParsedData, and getCaps
                //store the reportlengths
                IntPtr ptrToPreParsedData = new IntPtr();
                bool ppdSucsess = HidD_GetPreparsedData(tempHandle, ref ptrToPreParsedData);
                if (ppdSucsess == false)
                    continue;

                HIDP_CAPS capabilities = new HIDP_CAPS();
                int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities);

                HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES();
                bool hidAttribSucsess = HidD_GetAttributes(tempHandle, ref attributes);

                string productName = "";
                string SN = "";
                string manfString = "";
                IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string;
                if (HidD_GetProductString(tempHandle, buffer, 126)) productName = Marshal.PtrToStringAuto(buffer);
                if (HidD_GetSerialNumberString(tempHandle, buffer, 126)) SN = Marshal.PtrToStringAuto(buffer);
                if (HidD_GetManufacturerString(tempHandle, buffer, 126)) manfString = Marshal.PtrToStringAuto(buffer);
                Marshal.FreeHGlobal(buffer);

                //Call freePreParsedData to release some stuff
                HidD_FreePreparsedData(ref ptrToPreParsedData);

                //If connection was sucsessful, record the values in a global struct
                interfaceDetails productInfo = new interfaceDetails();
                productInfo.devicePath = devicePath;
                productInfo.manufacturer = manfString;
                productInfo.product = productName;
                productInfo.PID = (ushort)attributes.ProductID;
                productInfo.VID = (ushort)attributes.VendorID;
                productInfo.versionNumber = (ushort)attributes.VersionNumber;
                productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength;
                productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength;

                if (stringIsInteger(SN))
                    productInfo.serialNumber = Convert.ToInt32(SN);     //Check that serial number is actually a number

                int newSize = devices.Length + 1;
                Array.Resize(ref devices, newSize);
                devices[newSize - 1] = productInfo;
            }
            SetupDiDestroyDeviceInfoList(i);

            return devices;
        }