Example #1
0
        protected Int64 SendSausageCommands(ushort[] commandSequence)
        {
            if (outputReportSize != 2 || hidUsagePage != 1 || hidUsage != 6)             // hid page 1, usage 6 is keyboard
            {
                return(1302);
            }

            FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            securityAttributes.lpSecurityDescriptor = IntPtr.Zero;
            securityAttributes.bInheritHandle       = System.Convert.ToInt32(true);        //patti keep int32 here
            securityAttributes.nLength = Marshal.SizeOf(securityAttributes);

            IntPtr hF = FileIOApiDeclarations.CreateFile(path,
                                                         FileIOApiDeclarations.GENERIC_WRITE,
                                                         FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                                         IntPtr.Zero,
                                                         FileIOApiDeclarations.OPEN_EXISTING,
                                                         0,
                                                         0);
            SafeFileHandle hFile = new SafeFileHandle(hF, true);

            if (hFile.IsInvalid)
            {
                return(1301);;
            }
            FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED();
            overlapped.hEvent     = IntPtr.Zero;
            overlapped.Offset     = 0; // IntPtr.Zero;
            overlapped.OffsetHigh = 0; // IntPtr.Zero;
            foreach (ushort command in commandSequence)
            {
                uint cmd           = ((uint)command) << 16;
                uint bytesReturned = 0;

                if (!DeviceManagementApiDeclarations.DeviceIoControl(hFile, (uint)0x000b0008, ref cmd, 4, IntPtr.Zero, 0, ref bytesReturned, ref overlapped))
                {
                    int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                    return(result);
                }
            }
            hFile.Close();
            return(0);
        }
Example #2
0
        /// <summary>
        /// Enumerates all valid USB devics of the specified Vid.
        /// </summary>
        /// <returns>list of all devices found, ordered by USB port connection</returns>
        public static PIEDevice[] EnumeratePIE(Int64 vid)
        {
            // FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttrUnusedE = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            LinkedList <PIEDevice> devices = new LinkedList <PIEDevice>();

            // Get all device paths
            Guid guid = Guid.Empty;

            HidApiDeclarations.HidD_GetHidGuid(ref guid);
            IntPtr deviceInfoSet = DeviceManagementApiDeclarations.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero,
                                                                                       DeviceManagementApiDeclarations.DIGCF_PRESENT
                                                                                       | DeviceManagementApiDeclarations.DIGCF_DEVICEINTERFACE);

            DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA();
            deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); //28;

            LinkedList <String> paths = new LinkedList <String>();

            for (int i = 0; 0 != DeviceManagementApiDeclarations.SetupDiEnumDeviceInterfaces(
                     deviceInfoSet, 0, ref guid, i, ref deviceInterfaceData); i++)
            {
                int buffSize = 0;
                DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
                                                                                ref deviceInterfaceData, IntPtr.Zero, 0, ref buffSize, IntPtr.Zero);
                // Use IntPtr to simulate detail data structure
                IntPtr detailBuffer = Marshal.AllocHGlobal(buffSize);
                // Simulate setting cbSize to 4 bytes + one character (seems to be what everyone has always done, even though it makes no sense)
                //onur modified for 64-bit compatibility - March 2009
                if (IntPtr.Size == 8)                                                 //64-bit
                {
                    Marshal.WriteInt64(detailBuffer, Marshal.SizeOf(typeof(IntPtr))); //????patti
                }
                else //32-bit
                {
                    Marshal.WriteInt64(detailBuffer, 4 + Marshal.SystemDefaultCharSize); //patti
                }
                if (DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
                                                                                    ref deviceInterfaceData, detailBuffer, buffSize, ref buffSize, IntPtr.Zero))
                {
                    // convert buffer (starting past the cbsize variable) to string path
                    paths.AddLast(Marshal.PtrToStringAuto(new IntPtr(detailBuffer.ToInt64() + 4)));
                }
            }
            DeviceManagementApiDeclarations.SetupDiDestroyDeviceInfoList(deviceInfoSet);
            //Security attributes not used anymore - not necessary Onur March 2009
            // Open each device file and test for vid
            FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            securityAttributes.lpSecurityDescriptor = IntPtr.Zero;
            securityAttributes.bInheritHandle       = System.Convert.ToInt32(true);       //patti keep Int32 here
            securityAttributes.nLength = Marshal.SizeOf(securityAttributes);

            for (LinkedList <String> .Enumerator en = paths.GetEnumerator(); en.MoveNext();)
            {
                String path = en.Current;

                IntPtr fileH = FileIOApiDeclarations.CreateFile(path, FileIOApiDeclarations.GENERIC_WRITE,
                                                                FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                                                IntPtr.Zero, FileIOApiDeclarations.OPEN_EXISTING, 0, 0);
                SafeFileHandle fileHandle = new SafeFileHandle(fileH, true);
                if (fileHandle.IsInvalid)
                {
                    // Bad handle, try next path
                    continue;
                }
                try
                {
                    HidApiDeclarations.HIDD_ATTRIBUTES hidAttributes = new HidApiDeclarations.HIDD_ATTRIBUTES();
                    hidAttributes.Size = Marshal.SizeOf(hidAttributes);
                    if (0 != HidApiDeclarations.HidD_GetAttributes(fileHandle, ref hidAttributes) &&
                        hidAttributes.VendorID == vid)
                    {
                        // Good attributes and right vid, try to get caps
                        IntPtr pPerparsedData = new IntPtr();
                        if (HidApiDeclarations.HidD_GetPreparsedData(fileHandle, ref pPerparsedData))
                        {
                            HidApiDeclarations.HIDP_CAPS hidCaps = new HidApiDeclarations.HIDP_CAPS();
                            if (0 != HidApiDeclarations.HidP_GetCaps(pPerparsedData, ref hidCaps))
                            {
                                // Got Capabilities, add device to list
                                byte[] Mstring = new byte[128];
                                String ssss    = "";;
                                if (0 != HidApiDeclarations.HidD_GetManufacturerString(fileHandle, ref Mstring[0], 128))
                                {
                                    for (int i = 0; i < 64; i++)
                                    {
                                        byte[] t = new byte[2];
                                        t[0] = Mstring[2 * i];
                                        t[1] = Mstring[2 * i + 1];
                                        if (t[0] == 0)
                                        {
                                            break;
                                        }
                                        ssss += System.Text.Encoding.Unicode.GetString(t);
                                    }
                                }
                                byte[] Pstring = new byte[128];
                                String psss    = "";
                                if (0 != HidApiDeclarations.HidD_GetProductString(fileHandle, ref Pstring[0], 128))
                                {
                                    // Pstring[0] = 0xa0;  Test unicode
                                    // Pstring[1] = 0x03;
                                    for (int i = 0; i < 64; i++)
                                    {
                                        byte[] t = new byte[2];
                                        t[0] = Pstring[2 * i];
                                        t[1] = Pstring[2 * i + 1];
                                        if (t[0] == 0)
                                        {
                                            break;
                                        }
                                        psss += System.Text.Encoding.Unicode.GetString(t);
                                    }
                                    // psss=(System.Text.Encoding.Unicode.GetString(Pstring)).Trim(new char[]{' '});
                                }

                                devices.AddLast(new PIEDevice(path, hidAttributes.VendorID, hidAttributes.ProductID, hidAttributes.VersionNumber,
                                                              hidCaps.Usage, hidCaps.UsagePage, hidCaps.InputReportByteLength, hidCaps.OutputReportByteLength,
                                                              ssss, psss));
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    continue;
                }
                finally
                {
                    fileHandle.Close();
                }
            }
            PIEDevice[] ret = new PIEDevice[devices.Count];
            devices.CopyTo(ret, 0);
            return(ret);
        }