Exemplo n.º 1
0
        public static string[] GetDeviceIdentifiers()
        {
            if (CfgMgr32.CM_Get_Device_Interface_List_Size(out uint size, ref CfgMgr32.GUID_DISPLAY_DEVICE_ARRIVAL, null, CfgMgr32.CM_GET_DEVICE_INTERFACE_LIST_PRESENT) != CfgMgr32.CR_SUCCESS)
            {
                return(null);
            }


            char[] data = new char[size];
            if (CfgMgr32.CM_Get_Device_Interface_List(ref CfgMgr32.GUID_DISPLAY_DEVICE_ARRIVAL, null, data, (uint)data.Length, CfgMgr32.CM_GET_DEVICE_INTERFACE_LIST_PRESENT) == CfgMgr32.CR_SUCCESS)
            {
                return(new string(data).Split('\0').Where(m => !string.IsNullOrEmpty(m)).ToArray());
            }


            return(null);
        }
Exemplo n.º 2
0
        public static bool EjectDevice(string Path)
        {
            if (string.IsNullOrEmpty(Path) || string.IsNullOrEmpty(System.IO.Path.GetPathRoot(Path)))
            {
                throw new ArgumentNullException(nameof(Path), "Argument is not legal");
            }

            try
            {
                int DeviceNumber = 0;

                using (Kernel32.SafeHFILE hVolume = Kernel32.CreateFile($@"\\.\{Path.Substring(0, 2)}", 0, FileShare.ReadWrite, null, FileMode.Open, FileFlagsAndAttributes.SECURITY_ANONYMOUS))
                {
                    if (hVolume.IsNull || hVolume.IsInvalid)
                    {
                        return(false);
                    }

                    DeviceNumber = GetDeviceNumber(hVolume);
                }

                if (DeviceNumber >= 0)
                {
                    uint DevInst = GetDrivesDevInstByDeviceNumber(DeviceNumber, Kernel32.GetDriveType(Path));

                    if (DevInst == 0)
                    {
                        return(false);
                    }

                    if (CfgMgr32.CM_Get_Parent(out uint DevInstParent, DevInst) == CfgMgr32.CONFIGRET.CR_SUCCESS)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            if (CfgMgr32.CM_Request_Device_Eject(DevInstParent, out CfgMgr32.PNP_VETO_TYPE Result, null, 0) == CfgMgr32.CONFIGRET.CR_SUCCESS)
                            {
                                return(true);
                            }
                            else
                            {
                                LogTracer.Log($"Could not reject the USB device, PNP_VETO reason: {Enum.GetName(typeof(CfgMgr32.PNP_VETO_TYPE), Result)}");
                            }

                            Thread.Sleep(300);
                        }
                    }
Exemplo n.º 3
0
        public static bool ChildrenInstalled(string enumName)
        {
            UInt32 devStatus;
            UInt32 devProblemCode;

            SetupApi.SP_DEVINFO_DATA devInfoData =
                new SetupApi.SP_DEVINFO_DATA();
            devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData);

            using (SetupApi.DeviceInfoSet devInfoSet =
                       new SetupApi.DeviceInfoSet(
                           IntPtr.Zero,
                           enumName,
                           IntPtr.Zero,
                           SetupApi.DiGetClassFlags.DIGCF_PRESENT |
                           SetupApi.DiGetClassFlags.DIGCF_ALLCLASSES))
            {
                for (uint i = 0;
                     SetupApi.SetupDiEnumDeviceInfo(
                         devInfoSet.Get(),
                         i,
                         devInfoData);
                     ++i)
                {
                    CfgMgr32.CM_Get_DevNode_Status(
                        out devStatus,
                        out devProblemCode,
                        devInfoData.devInst,
                        0
                        );

                    if ((devStatus & (uint)SetupApi.DNFlags.DN_STARTED) == 0)
                    {
                        Trace.WriteLine(
                            enumName +
                            " child not started " +
                            devStatus.ToString()
                            );

                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 4
0
        public static int GetDevNode(string enum_device = "")
        // Returns the device node of the specified device
        // 'enum_device' should have the following format:
        // <enumerator>\<device_id>
        // If it is the empty string, the root of the device
        // tree will be returned
        {
            CfgMgr32.CR err;
            int         devNode;
            string      deviceInstanceId;

            if (!String.IsNullOrEmpty(enum_device))
            {
                deviceInstanceId = GetDeviceInstanceId(enum_device);

                if (String.IsNullOrEmpty(deviceInstanceId))
                {
                    Trace.WriteLine("No instance exists in system");
                    return(-1);
                }
            }
            else
            {
                deviceInstanceId = "";
            }

            err = CfgMgr32.CM_Locate_DevNode(
                out devNode,
                deviceInstanceId,
                CfgMgr32.CM_LOCATE_DEVNODE.NORMAL
                );

            if (err != CfgMgr32.CR.SUCCESS)
            {
                Win32Error.SetCR("CM_Locate_DevNode", err);
                throw new Exception(Win32Error.GetFullErrMsg());
            }

            return(devNode);
        }
Exemplo n.º 5
0
        public static bool Enumerate(
            string enum_device  = "",
            bool installDevices = false)
        // 'enum_device' should have the following format:
        // <enumerator>\<device_id>
        // If it is the empty string, the root of the device
        // tree will be enumerated
        // If 'installDevices' is 'true', PnP will try to complete
        // installation of any not-fully-installed devices.
        {
            CfgMgr32.CR err;
            int         devNode = GetDevNode(enum_device);

            CfgMgr32.CM_REENUMERATE ulFlags = CfgMgr32.CM_REENUMERATE.NORMAL;

            if (installDevices)
            {
                ulFlags |= CfgMgr32.CM_REENUMERATE.RETRY_INSTALLATION;
            }

            if (devNode == -1)
            {
                Trace.WriteLine("Could not get DevNode");
                return(false);
            }

            Helpers.AcquireSystemPrivilege(AdvApi32.SE_LOAD_DRIVER_NAME);

            err = CfgMgr32.CM_Reenumerate_DevNode(devNode, ulFlags);

            if (err != CfgMgr32.CR.SUCCESS)
            {
                Win32Error.SetCR("CM_Reenumerate_DevNode", err);
                throw new Exception(Win32Error.GetFullErrMsg());
            }

            Trace.WriteLine("Enumeration completed successfully");
            return(true);
        }
Exemplo n.º 6
0
        public static bool BlockUntilNoDriversInstalling(uint timeout)
        // Returns true, if no drivers are installing before the timeout
        // is reached. Returns false, if timeout is reached. To block
        // until no drivers are installing pass PInvoke.Winbase.INFINITE
        // 'timeout' is counted in seconds.
        {
            Winbase.WAIT result;

            Trace.WriteLine("Checking if drivers are currently installing");

            if (timeout != Winbase.INFINITE)
            {
                Trace.WriteLine("Blocking for " + timeout + " seconds..");
                timeout *= 1000;
            }
            else
            {
                Trace.WriteLine("Blocking until no drivers are installing");
            }

            result = CfgMgr32.CMP_WaitNoPendingInstallEvents(
                timeout
                );

            if (result == Winbase.WAIT.OBJECT_0)
            {
                Trace.WriteLine("No drivers installing");
                return(true);
            }
            else if (result == Winbase.WAIT.FAILED)
            {
                Win32Error.Set("CMP_WaitNoPendingInstallEvents");
                throw new Exception(Win32Error.GetFullErrMsg());
            }

            Trace.WriteLine("Timeout reached - drivers still installing");
            return(false);
        }
Exemplo n.º 7
0
        public static bool HasChildren(Devs xenBusDev)
        {
            CfgMgr32.CR err;

            string xenBusHwId = XenBus.hwIDs[
                Helpers.BitIdxFromFlag((uint)xenBusDev)
                                ];

            int xenBusNode = Device.GetDevNode(xenBusHwId);
            int xenBusChild;

            if (xenBusNode == -1)
            {
                Trace.WriteLine("Could not get XenBus DevNode");
                return(false);
            }

            err = CfgMgr32.CM_Get_Child(
                out xenBusChild, xenBusNode, 0
                );

            if (err == CfgMgr32.CR.NO_SUCH_DEVNODE)
            {
                Trace.WriteLine("XenBus device has no children");
                return(false);
            }
            else if (err != CfgMgr32.CR.SUCCESS)
            {
                Win32Error.SetCR("CM_Get_Child", err);
                throw new Exception(Win32Error.GetFullErrMsg());
            }

            Trace.WriteLine("XenBus device has at least one child");

            return(true);
        }