Beispiel #1
0
        public static string GetDevRegPropertyStr(
            SetupApi.DeviceInfoSet devInfoSet,
            SetupApi.SP_DEVINFO_DATA devInfoData,
            SetupApi.SPDRP property)
        // Use this function for any 'Device Registry
        // Property' that returns a string,
        // e.g. SPDRP_CLASSGUID
        {
            int propertyRegDataType;
            int requiredSize;

            // 'buffer' is 1KB  but Unicode chars are 2 bytes,
            // hence 'buffer' can hold up to 512 chars
            const int BUFFER_SIZE = 1024;

            byte[] buffer = new byte[BUFFER_SIZE];

            SetupApi.SetupDiGetDeviceRegistryProperty(
                devInfoSet.Get(),
                devInfoData,
                property,
                out propertyRegDataType,
                buffer,
                BUFFER_SIZE,
                out requiredSize
                );

            return(System.Text.Encoding.Unicode.GetString(
                       buffer,
                       0,
                       requiredSize
                       ));
        }
Beispiel #2
0
        public static string[] GetDevRegPropertyMultiStr(
            SetupApi.DeviceInfoSet devInfoSet,
            SetupApi.SP_DEVINFO_DATA devInfoData,
            SetupApi.SPDRP property)
        // Use this function for any 'Device Registry
        // Property' that returns 'REG_MULTI_SZ',
        // e.g. SPDRP_HARDWAREID
        {
            int propertyRegDataType;
            int requiredSize;

            // 'buffer' is 4KB  but Unicode chars are 2 bytes,
            // hence 'buffer' can hold up to 2K chars
            const int BUFFER_SIZE = 4096;

            byte[] buffer = new byte[BUFFER_SIZE];

            SetupApi.SetupDiGetDeviceRegistryProperty(
                devInfoSet.Get(),
                devInfoData,
                property,
                out propertyRegDataType,
                buffer,
                BUFFER_SIZE,
                out requiredSize
                );

            return(Helpers.StringArrayFromMultiSz(buffer));
        }
Beispiel #3
0
        public static void UninstallDriversAndDevices()
        {
            using (SetupApi.DeviceInfoSet devInfoSet =
                       new SetupApi.DeviceInfoSet(
                           IntPtr.Zero,
                           IntPtr.Zero,
                           IntPtr.Zero,
                           SetupApi.DiGetClassFlags.DIGCF_ALLCLASSES))
            {
                foreach (string hwId in pvHwIds)
                {
                    Helpers.UninstallDriverPackages(hwId);

                    while (
                        Device.RemoveFromSystem(
                            devInfoSet,
                            hwId,
                            false)
                        )
                    {
                        ;
                    }
                }
            }
        }
Beispiel #4
0
        // Static constructor
        static XenBus()
        {
            hwIDs = new string[
                Enum.GetNames(typeof(Devs)).Length // == # of devs
                    ];

            const string XENBUS_DEV_PREFIX = @"PCI\VEN_5853&";

            using (SetupApi.DeviceInfoSet devInfoSet =
                       new SetupApi.DeviceInfoSet(
                           IntPtr.Zero,
                           "PCI",
                           IntPtr.Zero,
                           SetupApi.DiGetClassFlags.DIGCF_ALLCLASSES |
                           SetupApi.DiGetClassFlags.DIGCF_PRESENT))
            {
                for (int i = 0; i < hwIDs.Length; ++i)
                {
                    SetupApi.SP_DEVINFO_DATA xenBusDevInfoData;

                    xenBusDevInfoData = Device.FindInSystem(
                        XENBUS_DEV_PREFIX +
                        Enum.GetName(typeof(Devs), 1 << i),     // == Dev name
                        devInfoSet,
                        false
                        );

                    if (xenBusDevInfoData != null)
                    {
                        // Just get the first string returned.
                        // Should be the most explicit.
                        hwIDs[i] = Device.GetDevRegPropertyMultiStr(
                            devInfoSet,
                            xenBusDevInfoData,
                            SetupApi.SPDRP.HARDWAREID
                            )[0];
                    }
                    else
                    {
                        hwIDs[i] = "";
                    }
                }

                // In descending order of preference
                if (IsPresent(Devs.DEV_C000, true))
                {
                    preferredXenBus = Devs.DEV_C000;
                }
                else if (IsPresent(Devs.DEV_0001, true))
                {
                    preferredXenBus = Devs.DEV_0001;
                }
                else if (IsPresent(Devs.DEV_0002, true))
                {
                    preferredXenBus = Devs.DEV_0002;
                }
            }
        }
Beispiel #5
0
        public static string GetDeviceInstanceId(string enum_device)
        // Returns the device instance ID of the specified device
        // 'enum_device' should have the following format:
        // <enumerator>\<device_id>
        {
            const int     BUFFER_SIZE      = 4096;
            string        enumerator       = enum_device.Split(new char[] { '\\' })[0];
            StringBuilder deviceInstanceId = new StringBuilder(BUFFER_SIZE);

            SetupApi.SP_DEVINFO_DATA devInfoData;
            int reqSize;

            using (SetupApi.DeviceInfoSet devInfoSet =
                       new SetupApi.DeviceInfoSet(
                           IntPtr.Zero,
                           enumerator,
                           IntPtr.Zero,
                           SetupApi.DiGetClassFlags.DIGCF_ALLCLASSES |
                           SetupApi.DiGetClassFlags.DIGCF_PRESENT))
            {
                devInfoData = Device.FindInSystem(
                    enum_device,
                    devInfoSet,
                    false
                    );

                if (devInfoData == null)
                {
                    return("");
                }

                if (!SetupApi.SetupDiGetDeviceInstanceId(
                        devInfoSet.Get(),
                        devInfoData,
                        deviceInstanceId,
                        BUFFER_SIZE,
                        out reqSize))
                {
                    Win32Error.Set("SetupDiGetDeviceInstanceId");
                    throw new Exception(Win32Error.GetFullErrMsg());
                }
            }

            return(deviceInstanceId.ToString());
        }
Beispiel #6
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);
        }
Beispiel #7
0
        public static string GetDriverVersion(
            SetupApi.DeviceInfoSet devInfoSet,
            SetupApi.SP_DEVINFO_DATA devInfoData)
        {
            string driverKeyName = GetDevRegPropertyStr(
                devInfoSet,
                devInfoData,
                SetupApi.SPDRP.DRIVER
                );

            if (String.IsNullOrEmpty(driverKeyName))
            {
                return("0.0.0.0");
            }

            driverKeyName =
                @"SYSTEM\CurrentControlSet\Control\Class\" + driverKeyName;

            RegistryKey rk = Registry.LocalMachine.OpenSubKey(
                driverKeyName, true
                );

            return((string)rk.GetValue("DriverVersion"));
        }
Beispiel #8
0
        public static bool RemoveFromSystem(
            SetupApi.DeviceInfoSet devInfoSet,
            string hwID,
            bool strictSearch)
        // WARNING: Removes ONLY the particular device
        // instance referenced by 'devInfoSet'
        {
            SetupApi.SP_DEVINFO_DATA devInfoData;

            devInfoData = FindInSystem(
                hwID,
                devInfoSet,
                strictSearch
                );

            if (devInfoData == null)
            {
                return(false);
            }

            SetupApi.SP_REMOVEDEVICE_PARAMS rparams =
                new SetupApi.SP_REMOVEDEVICE_PARAMS();

            rparams.ClassInstallHeader.cbSize =
                (uint)Marshal.SizeOf(rparams.ClassInstallHeader);

            rparams.ClassInstallHeader.InstallFunction =
                SetupApi.DI_FUNCTION.DIF_REMOVE;

            rparams.HwProfile = 0;
            rparams.Scope     = SetupApi.DI_REMOVE_DEVICE_GLOBAL;
            GCHandle handle1 = GCHandle.Alloc(rparams);

            if (!SetupApi.SetupDiSetClassInstallParams(
                    devInfoSet.Get(),
                    devInfoData,
                    ref rparams,
                    Marshal.SizeOf(rparams)))
            {
                Win32Error.Set("SetupDiSetClassInstallParams");
                throw new Exception(
                          Win32Error.GetFullErrMsg()
                          );
            }

            Trace.WriteLine(
                "Removing device \'" + hwID +
                "\' from system"
                );

            if (!SetupApi.SetupDiCallClassInstaller(
                    SetupApi.DI_FUNCTION.DIF_REMOVE,
                    devInfoSet.Get(),
                    devInfoData))
            {
                Win32Error.Set("SetupDiCallClassInstaller");
                if (Win32Error.GetErrorNo() != WinError.ERROR_KEY_DOES_NOT_EXIST)
                {
                    throw new Exception(
                              Win32Error.GetFullErrMsg()
                              );
                }
            }

            Trace.WriteLine("Remove should have worked");
            return(true);
        }
Beispiel #9
0
        public static SetupApi.SP_DEVINFO_DATA FindInSystem(
            string hwID,
            SetupApi.DeviceInfoSet devInfoSet,
            bool strictSearch)
        // The function takes as input an initialized 'deviceInfoSet'
        // object and a hardware ID string we want to search the system
        // for. If 'strictSearch' is true, the device needs to exactly
        // match the hwID to be returned. Otherwise, the device's name
        // needs to start with the supplied hwID string. If the device
        // is found, a fully initialized 'SP_DEVINFO_DATA' object is
        // returned. If not, the function returns 'null'.
        {
            SetupApi.SP_DEVINFO_DATA devInfoData =
                new SetupApi.SP_DEVINFO_DATA();
            devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData);

            // Select which string comparison function
            // to use, depending on 'strictSearch'
            Func <string, string, bool> hwIDFound;

            if (strictSearch)
            {
                hwIDFound = (string _enumID, string _hwID) =>
                            _enumID.Equals(
                    _hwID,
                    StringComparison.OrdinalIgnoreCase
                    );
            }
            else
            {
                hwIDFound = (string _enumID, string _hwID) =>
                            _enumID.StartsWith(
                    _hwID,
                    StringComparison.OrdinalIgnoreCase
                    );
            }

            Trace.WriteLine(
                "Searching system for device: \'" + hwID +
                "\'; (strict search: \'" + strictSearch + "\')"
                );

            for (uint i = 0;
                 SetupApi.SetupDiEnumDeviceInfo(
                     devInfoSet.Get(),
                     i,
                     devInfoData);
                 ++i)
            {
                string [] ids = GetDevRegPropertyMultiStr(
                    devInfoSet,
                    devInfoData,
                    SetupApi.SPDRP.HARDWAREID
                    );

                foreach (string id in ids)
                {
                    if (hwIDFound(id, hwID))
                    {
                        Trace.WriteLine(
                            "Found: \'" + String.Join("  ", ids) + "\'"
                            );
                        return(devInfoData);
                    }
                }
            }

            Win32Error.Set("SetupDiEnumDeviceInfo");
            if (Win32Error.GetErrorNo() == WinError.ERROR_NO_MORE_ITEMS)
            {
                Trace.WriteLine("Device not found");
                return(null);
            }

            throw new Exception(Win32Error.GetFullErrMsg());
        }
Beispiel #10
0
        private static void SetPVToolsVersionOnFirstRun(
            RegistryKey openRegKey)
        {
            string regValName = "PVToolsVersionOnFirstRun";
            int    tmp        = (int)openRegKey.GetValue(regValName, -1);
            string drvVer;

            if (tmp != -1)
            {
                // We can save some indentation..
                goto SetStaticVariable;
            }

            if (xenBusDev == (XenBus.Devs) 0)
            {
                tmp    = 0; // None
                drvVer = "0.0.0.0";
            }
            else
            {
                using (SetupApi.DeviceInfoSet devInfoSet =
                           new SetupApi.DeviceInfoSet(
                               IntPtr.Zero,
                               "PCI",
                               IntPtr.Zero,
                               SetupApi.DiGetClassFlags.DIGCF_ALLCLASSES |
                               SetupApi.DiGetClassFlags.DIGCF_PRESENT))
                {
                    int idx = Helpers.BitIdxFromFlag((uint)xenBusDev);

                    SetupApi.SP_DEVINFO_DATA devInfoData =
                        Device.FindInSystem(
                            XenBus.hwIDs[idx],
                            devInfoSet,
                            true
                            );

                    drvVer = Device.GetDriverVersion(
                        devInfoSet, devInfoData
                        );
                }

                // Split the DriverVersion string on the '.'
                // char and parse the 1st substring (major
                // version number)
                tmp = Int32.Parse(
                    drvVer.Split(new char[] { '.' })[0]
                    );

                if (tmp != 8) // Eight
                {
                    tmp = 7;  // LessThanEight
                }
            }

            Trace.WriteLine(
                "XenBus driver version on first run: \'" + drvVer + "\'"
                );

            openRegKey.SetValue(
                regValName,
                tmp,
                RegistryValueKind.DWord
                );

SetStaticVariable:
            pvToolsVer = (PVToolsVersion)Enum.Parse(
                typeof(PVToolsVersion), tmp.ToString()
                );
        }
Beispiel #11
0
        private static void VifDisableEnable(bool enable)
        {
            string action = enable ? "enable" : "disable";

            Trace.WriteLine("===> VifDisableEnable: \'" + action + "\'");

            using (SetupApi.DeviceInfoSet devInfoSet =
                       new SetupApi.DeviceInfoSet(
                           IntPtr.Zero,
                           "XENVIF",
                           IntPtr.Zero,
                           SetupApi.DiGetClassFlags.DIGCF_PRESENT |
                           SetupApi.DiGetClassFlags.DIGCF_ALLCLASSES))
            {
                SetupApi.SP_DEVINFO_DATA devInfoData =
                    new SetupApi.SP_DEVINFO_DATA();

                devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData);

                for (uint i = 0;
                     SetupApi.SetupDiEnumDeviceInfo(
                         devInfoSet.Get(),
                         i,
                         devInfoData);
                     ++i)
                {
                    SetupApi.PropertyChangeParameters pcParams =
                        new SetupApi.PropertyChangeParameters();

                    pcParams.size       = 8;
                    pcParams.diFunction = SetupApi.DI_FUNCTION.DIF_PROPERTYCHANGE;
                    pcParams.scope      = SetupApi.Scopes.Global;

                    if (enable)
                    {
                        pcParams.stateChange = SetupApi.StateChangeAction.Enable;
                    }
                    else
                    {
                        pcParams.stateChange = SetupApi.StateChangeAction.Disable;
                    }

                    pcParams.hwProfile = 0;
                    var pinned = GCHandle.Alloc(pcParams, GCHandleType.Pinned);

                    byte[] temp = new byte[Marshal.SizeOf(pcParams)];
                    Marshal.Copy(
                        pinned.AddrOfPinnedObject(),
                        temp,
                        0,
                        Marshal.SizeOf(pcParams)
                        );

                    var pdd = GCHandle.Alloc(devInfoData, GCHandleType.Pinned);

                    if (!SetupApi.SetupDiSetClassInstallParams(
                            devInfoSet.Get(),
                            pdd.AddrOfPinnedObject(),
                            pinned.AddrOfPinnedObject(),
                            Marshal.SizeOf(pcParams)))
                    {
                        Win32Error.Set("SetupDiSetClassInstallParams");
                        Trace.WriteLine(Win32Error.GetFullErrMsg());
                    }

                    if (!SetupApi.SetupDiCallClassInstaller(
                            SetupApi.DI_FUNCTION.DIF_PROPERTYCHANGE,
                            devInfoSet.Get(),
                            pdd.AddrOfPinnedObject()))
                    {
                        Win32Error.Set("SetupDiCallClassInstaller");
                        Trace.WriteLine(Win32Error.GetFullErrMsg());
                    }

                    pdd.Free();
                    pinned.Free();
                }
            }
            Trace.WriteLine("<=== VifDisableEnable");
        }