Beispiel #1
0
        private bool GetObjectName(SafeFileHandle usbHandle, int objectNameIndex, out string objectName)
        {
            int objNameLength;

            Byte[]   regKeyPathname   = new byte[512];
            GCHandle gcRegKeyPathname = GCHandle.Alloc(regKeyPathname, GCHandleType.Pinned);

#if RESERVED_FOR_FUTURE_USE
            LibUsbRequest req = new LibUsbRequest();
            req.ObjectName.Index = objectNameIndex;
            Marshal.Copy(req.Bytes, 0, gcRegKeyPathname.AddrOfPinnedObject(), LibUsbRequest.Size);
#endif

            bool bSuccess = LibUsbDriverIO.UsbIOSync(usbHandle,
                                                     LibUsbIoCtl.GET_OBJECT_NAME,
                                                     regKeyPathname,
                                                     regKeyPathname.Length,
                                                     gcRegKeyPathname.AddrOfPinnedObject(),
                                                     regKeyPathname.Length,
                                                     out objNameLength);

            gcRegKeyPathname.Free();

            if (bSuccess && objNameLength > 1)
            {
                objectName = Encoding.ASCII.GetString(regKeyPathname, 0, objNameLength - 1);
            }
            else
            {
                objectName = String.Empty;
            }

            return(bSuccess);
        }
Beispiel #2
0
        public static byte[] RegSetStringRequest(string name, string value)
        {
            LibUsbRequest req     = new LibUsbRequest();
            int           uOffset = LibUsbRequest.Size;

            req.DeviceRegKey.KeyType = (int)KeyType.RegSz;

            byte[] bytesName  = Encoding.Unicode.GetBytes(name + "\0");
            byte[] bytesValue = Encoding.Unicode.GetBytes(value + "\0");

            req.DeviceRegKey.NameOffset = uOffset;
            uOffset += bytesName.Length;
            req.DeviceRegKey.ValueOffset = uOffset;
            req.DeviceRegKey.ValueLength = bytesValue.Length;

            uOffset += bytesValue.Length;
            byte[] buffer   = new byte[uOffset];
            byte[] regBytes = req.Bytes;

            Array.Copy(regBytes, buffer, regBytes.Length);
            Array.Copy(bytesName, 0, buffer, req.DeviceRegKey.NameOffset, bytesName.Length);
            Array.Copy(bytesValue, 0, buffer, req.DeviceRegKey.ValueOffset, bytesValue.Length);

            return(buffer);
        }
Beispiel #3
0
        public static byte[] RegGetRequest(string name, int valueBufferSize)
        {
            if (valueBufferSize < 1 || name.Trim().Length == 0)
            {
                throw new UsbException("Global", "Invalid DeviceRegistry het parameter.");
            }

            LibUsbRequest req     = new LibUsbRequest();
            int           uOffset = LibUsbRequest.Size;

            req.DeviceRegKey.KeyType = (int)KeyType.RegBinary;

            byte[] bytesName = Encoding.Unicode.GetBytes(name + "\0");

            req.DeviceRegKey.NameOffset = uOffset;
            uOffset += bytesName.Length;
            req.DeviceRegKey.ValueOffset = uOffset;
            req.DeviceRegKey.ValueLength = (valueBufferSize);

            uOffset += Math.Max(uOffset + 1, valueBufferSize - (LibUsbRequest.Size + bytesName.Length));
            byte[] buffer   = new byte[uOffset];
            byte[] regBytes = req.Bytes;

            Array.Copy(regBytes, buffer, regBytes.Length);
            Array.Copy(bytesName, 0, buffer, req.DeviceRegKey.NameOffset, bytesName.Length);

            return(buffer);
        }
Beispiel #4
0
        /// <summary>
        /// Sets an alternate interface for the specified interface.
        /// </summary>
        /// <param name="interfaceID">The interface index to specify an alternate setting for.</param>
        /// <param name="alternateID">The alternate interface setting.</param>
        /// <returns>True on success.</returns>
        public bool SetAltInterface(int interfaceID, int alternateID)
        {
            if (!mClaimedInterfaces.Contains(interfaceID))
            {
                throw new UsbException(this, String.Format("You must claim interface {0} before setting an alternate interface.", interfaceID));
            }
            LibUsbRequest req = new LibUsbRequest();

            req.Iface.ID          = interfaceID;
            req.Iface.AlternateID = alternateID;
            req.Timeout           = UsbConstants.DEFAULT_TIMEOUT;

            int ret;

            return(UsbIoSync(LibUsbIoCtl.SET_INTERFACE, req, LibUsbRequest.Size, IntPtr.Zero, 0, out ret));
        }
Beispiel #5
0
        /// <summary>
        /// Releases an interface that was previously claimed with <see cref="ClaimInterface"/>.
        /// </summary>
        /// <param name="interfaceID">The interface to release.</param>
        /// <returns>True on success.</returns>
        public bool ReleaseInterface(int interfaceID)
        {
            LibUsbRequest req = new LibUsbRequest();

            req.Iface.ID = interfaceID;
            if (!mClaimedInterfaces.Remove(interfaceID))
            {
                return(true);
            }

            req.Timeout = UsbConstants.DEFAULT_TIMEOUT;

            int ret;
            // NOTE: A claimed interface is ALWAYS removed from the internal list.
            bool bSuccess = UsbIoSync(LibUsbIoCtl.RELEASE_INTERFACE, req, LibUsbRequest.Size, IntPtr.Zero, 0, out ret);

            return(bSuccess);
        }
Beispiel #6
0
        /// <summary>
        /// Gets the alternate interface number for the specified interfaceID.
        /// </summary>
        /// <param name="interfaceID">The interface number of to get the alternate setting for.</param>
        /// <param name="alternateID">The currrently selected alternate interface number.</param>
        /// <returns>True on success.</returns>
        public bool GetAltInterface(int interfaceID, out int alternateID)
        {
            LibUsbRequest req = new LibUsbRequest();

            req.Iface.ID = interfaceID;
            req.Timeout  = UsbConstants.DEFAULT_TIMEOUT;

            int      ret;
            GCHandle gc = GCHandle.Alloc(req, GCHandleType.Pinned);
            bool     success;

            alternateID = -1;
            if ((success = UsbIoSync(LibUsbIoCtl.GET_INTERFACE, req, LibUsbRequest.Size, gc.AddrOfPinnedObject(), 1, out ret)) == true)
            {
                alternateID = Marshal.ReadByte(gc.AddrOfPinnedObject());
                UsbAltInterfaceSettings[interfaceID] = (byte)alternateID;
            }
            gc.Free();
            return(success);
        }
Beispiel #7
0
        /// <summary>
        /// Claims the specified interface of the device.
        /// </summary>
        /// <param name="interfaceID">The interface to claim.</param>
        /// <returns>True on success.</returns>
        public bool ClaimInterface(int interfaceID)
        {
            if (mClaimedInterfaces.Contains(interfaceID))
            {
                return(true);
            }

            LibUsbRequest req = new LibUsbRequest();

            req.Iface.ID = interfaceID;
            req.Timeout  = UsbConstants.DEFAULT_TIMEOUT;

            int  ret;
            bool bSuccess = UsbIoSync(LibUsbIoCtl.CLAIM_INTERFACE, req, LibUsbRequest.Size, IntPtr.Zero, 0, out ret);

            if (bSuccess)
            {
                mClaimedInterfaces.Add(interfaceID);
            }

            return(bSuccess);
        }
Beispiel #8
0
        private void GetPropertiesSPDRP(SafeHandle usbHandle)
        {
            byte[]   propBuffer   = new byte[1024];
            GCHandle gcPropBuffer = GCHandle.Alloc(propBuffer, GCHandleType.Pinned);

            LibUsbRequest            req      = new LibUsbRequest();
            Dictionary <string, int> allProps = Helper.GetEnumData(typeof(DevicePropertyType));

            foreach (KeyValuePair <string, int> prop in allProps)
            {
                object oValue = String.Empty;

                req.DeviceProperty.ID = prop.Value;
                int  iReturnBytes;
                bool bSuccess = LibUsbDriverIO.UsbIOSync(usbHandle,
                                                         LibUsbIoCtl.GET_REG_PROPERTY,
                                                         req,
                                                         LibUsbRequest.Size,
                                                         gcPropBuffer.AddrOfPinnedObject(),
                                                         propBuffer.Length,
                                                         out iReturnBytes);
                if (bSuccess)
                {
                    switch ((DevicePropertyType)prop.Value)
                    {
                    case DevicePropertyType.PhysicalDeviceObjectName:
                    case DevicePropertyType.LocationInformation:
                    case DevicePropertyType.Class:
                    case DevicePropertyType.Mfg:
                    case DevicePropertyType.DeviceDesc:
                    case DevicePropertyType.Driver:
                    case DevicePropertyType.EnumeratorName:
                    case DevicePropertyType.FriendlyName:
                    case DevicePropertyType.ClassGuid:
                        oValue = GetAsString(propBuffer, iReturnBytes);
                        break;

                    case DevicePropertyType.HardwareId:
                    case DevicePropertyType.CompatibleIds:
                        oValue = GetAsStringArray(propBuffer, iReturnBytes);
                        break;

                    case DevicePropertyType.BusNumber:
                    case DevicePropertyType.InstallState:
                    case DevicePropertyType.LegacyBusType:
                    case DevicePropertyType.RemovalPolicy:
                    case DevicePropertyType.UiNumber:
                    case DevicePropertyType.Address:
                        oValue = GetAsStringInt32(propBuffer, iReturnBytes);
                        break;

                    case DevicePropertyType.BusTypeGuid:
                        oValue = GetAsGuid(propBuffer, iReturnBytes);
                        break;
                    }
                }
                else
                {
                    oValue = String.Empty;
                }

                mDeviceProperties.Add(prop.Key, oValue);
            }
            gcPropBuffer.Free();
        }