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; }
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"); }
// 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'. public static SetupApi.SP_DEVINFO_DATA FindInSystem( string hwID, SetupApi.DeviceInfoSet devInfoSet, bool strictSearch) { 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()); }