![]() |
Energieverwaltung von Netzwerkadaptern und USB Root Hub
Hallo,
ich würde gerne ein Programm schreiben um die Energieverwaltung Netzwerkadaptern und USB Geräten auszulesen und auch zu verändern. Speziell geht es dabei um die Einstellung unter Windows 7: Geräter-Manager->Netwerkadapter->Mein-USBLAN Adapter->Energieverwaltung->"Computer kann das Gerät ausschalten, um Energie zu sparen" bzw. Geräter-Manager->USB-Controller->Root-Hub->Energieverwaltung->"Computer kann das Gerät ausschalten, um Energie zu sparen" Ich habe im Beitrag ![]() Kann mir jemand sagen ob ich über die WMI Funktionen die Energieverwaltung lesen/setzen kann, oder ob ich hier an der komplett falschen Stelle suche? Und außerdem wie ich das ganze auch für USB-Controller verwenden kann? Vielen Dank schonmal! |
AW: Energieverwaltung von Netzwerkadaptern und USB Root Hub
Mit
![]() Note: To change the status or configuration of a device, you must be a member of the Administrators group on the computer. Beispiele:
Delphi-Quellcode:
Einen Energieeffizienzdiagnose-Bericht bekommt man damit:
function GetConsoleOutput(Command : string;
Output, Errors : TStringList) : Boolean; var Buffer : array[0..255] of Char; CreationFlags : DWORD; NumberOfBytesRead : DWORD; PipeErrorsRead : THandle; PipeErrorsWrite : THandle; PipeOutputRead : THandle; PipeOutputWrite : THandle; ProcessInfo : TProcessInformation; SecurityAttr : TSecurityAttributes; StartupInfo : TStartupInfo; Stream : TMemoryStream; begin //Initialisierung ProcessInfo FillChar(ProcessInfo, SizeOf(TProcessInformation), 0); //Initialisierung SecurityAttr FillChar(SecurityAttr, SizeOf(TSecurityAttributes), 0); SecurityAttr.nLength := SizeOf(TSecurityAttributes); SecurityAttr.bInheritHandle := True; SecurityAttr.lpSecurityDescriptor := nil; //Pipes erzeugen CreatePipe(PipeOutputRead, PipeOutputWrite, @SecurityAttr, 0); CreatePipe(PipeErrorsRead, PipeErrorsWrite, @SecurityAttr, 0); //Initialisierung StartupInfo FillChar(StartupInfo, SizeOf(TStartupInfo), 0); StartupInfo.cb := SizeOf(TStartupInfo); StartupInfo.hStdInput := 0; StartupInfo.hStdOutput := PipeOutputWrite; StartupInfo.hStdError := PipeErrorsWrite; StartupInfo.wShowWindow := SW_HIDE; StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES; CreationFlags := CREATE_DEFAULT_ERROR_MODE or CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS; // Folgende Zeile ist nur für Delphi ab 2009 erforderlich: UniqueString(Command); if CreateProcess(nil, PChar(Command), nil, nil, True, CreationFlags, nil, nil, StartupInfo, ProcessInfo) then begin Result := True; //Write-Pipes schließen CloseHandle(PipeOutputWrite); CloseHandle(PipeErrorsWrite); //Ausgabe Read-Pipe auslesen Stream := TMemoryStream.Create; try while ReadFile(PipeOutputRead, Buffer, 255, NumberOfBytesRead, nil) do begin Stream.Write(Buffer, NumberOfBytesRead); end; OEMToAnsiBuff(Stream.Memory, Stream.Memory, Stream.Size); //<-------------- Stream.Position := 0; Output.LoadFromStream(Stream); finally Stream.Free; end; CloseHandle(PipeOutputRead); //Fehler Read-Pipe auslesen Stream := TMemoryStream.Create; try while ReadFile(PipeErrorsRead, Buffer, 255, NumberOfBytesRead, nil) do begin Stream.Write(Buffer, NumberOfBytesRead); end; OEMToAnsiBuff(Stream.Memory, Stream.Memory, Stream.Size); Stream.Position := 0; Errors.LoadFromStream(Stream); finally Stream.Free; end; CloseHandle(PipeErrorsRead); WaitForSingleObject(ProcessInfo.hProcess, INFINITE); CloseHandle(ProcessInfo.hProcess); end else begin Result := False; CloseHandle(PipeOutputRead); CloseHandle(PipeOutputWrite); CloseHandle(PipeErrorsRead); CloseHandle(PipeErrorsWrite); end; end; procedure TForm2.Button1Click(Sender: TObject); var Output : TStringList; Errors : TStringList; begin Output := TStringList.Create; Errors := TStringList.Create; Memo1.Lines.Add('----------------------USB OFF---------------------'); try if GetConsoleOutput('devcon.exe disable USB\ROOT_HUB20', Output, Errors) // USB 2.0 then Memo1.Lines.AddStrings(Output); Memo1.Lines.Add(''); if GetConsoleOutput('devcon.exe disable USB\ROOT_HUB30', Output, Errors) // USB 3.0 then Memo1.Lines.AddStrings(Output); finally Output.free; Errors.free; end; end; procedure TForm2.Button2Click(Sender: TObject); var Output : TStringList; Errors : TStringList; begin Output := TStringList.Create; Errors := TStringList.Create; Memo1.Lines.Add('----------------------USB ON----------------------'); try if GetConsoleOutput('devcon.exe enable USB\ROOT_HUB20', Output, Errors) // USB 2.0 then Memo1.Lines.AddStrings(Output); Memo1.Lines.Add(''); if GetConsoleOutput('devcon.exe enable USB\ROOT_HUB30', Output, Errors) // USB 3.0 then Memo1.Lines.AddStrings(Output); finally Output.free; Errors.free; end; end; powercfg energy (Administrator-Rechte erforderlich) Mit powercfg.exe kann man sehr viel machen. WMI hast Du selbst schon erwähnt. |
AW: Energieverwaltung von Netzwerkadaptern und USB Root Hub
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo t.roller,
vielen Dank für die Antwort, aber das ist nicht ganz das, was ich suche. Ich will nicht den gesamten USB Hub deaktivieren, sondern nur die folgende Option auslesen bzw. deaktivieren: Geräter-Manager->USB-Controller->Root-Hub->Energieverwaltung->"Computer kann das Gerät ausschalten, um Energie zu sparen" Der USB Hub soll weiter laufen, es ist aber wichtig, dass er nicht das angeschlossene Gerät in einem bestimmten Zustand des Power Managements deaktiviertv (s. Bild im Anhang). |
AW: Energieverwaltung von Netzwerkadaptern und USB Root Hub
Hab mich jetzt auch etwas durch die ganzen WMI Funtionen geschlagen. Bei den WMI Klassen
Win32_NetworkAdapter bzw. Win32_USBHub kann ich die gesuchten Devices finden, aber bei der Abfrage der Attribute PowerManagementCapabilities und PowerManagementSupported bekomme ich nicht die gesuchten Einstellungen, sondern nur NULL zurück... |
AW: Energieverwaltung von Netzwerkadaptern und USB Root Hub
PowerManagementCapabilities sind Arrays, die man entsprechend auswerten muss.
|
AW: Energieverwaltung von Netzwerkadaptern und USB Root Hub
ok, kannst du vielleicht ein Beispiel geben wie ich diese auslese?
|
AW: Energieverwaltung von Netzwerkadaptern und USB Root Hub
Wenn der WMI-Provider bestimmte Daten nicht zur Verfügung stellt, kommt trotzdem nichts.
Delphi-Quellcode:
//-----------------------------------------------------------------------------------------------------
// This code was generated by the Wmi Delphi Code Creator http://theroadtodelphi.wordpress.com // Version: 1.8.3.0 // LIABILITY DISCLAIMER // THIS GENERATED CODE IS DISTRIBUTED "AS IS". NO WARRANTY OF ANY KIND IS EXPRESSED OR IMPLIED. // YOU USE IT AT YOUR OWN RISK. THE AUTHOR NOT WILL BE LIABLE FOR DATA LOSS, // DAMAGES AND LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING OR MISUSING THIS CODE. //---------------------------------------------------------------------------------------------------- program GetWMI_Info; {$APPTYPE CONSOLE} uses (* SysUtils, ActiveX, ComObj, Variants; *) System.SysUtils, Winapi.ActiveX, System.Win.ComObj, System.Variants; function VarToInt(const AVariant: Variant): INT64;// integer; begin Result := StrToIntDef(Trim(VarToStr(AVariant)), 0); end; // Die Klasse "Win32_USBHub" stellt die Verwaltungseigenschaften eines USB-Hubs dar. procedure GetWin32_USBHubInfo; const WbemUser =''; WbemPassword =''; WbemComputer ='localhost'; wbemFlagForwardOnly = $00000020; var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet: OLEVariant; FWbemObject : OLEVariant; oEnum : IEnumvariant; iValue : LongWord; j : Integer; begin; FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword); FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_USBHub','WQL',wbemFlagForwardOnly); oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; while oEnum.Next(1, FWbemObject, iValue) = 0 do begin Writeln(Format('Availability %d',[VarToInt(FWbemObject.Availability)]));// Uint16 Writeln(Format('Caption %s',[VarToStr(FWbemObject.Caption)]));// String Writeln(Format('ClassCode %d',[VarToInt(FWbemObject.ClassCode)]));// Uint8 Writeln(Format('ConfigManagerErrorCode %d',[VarToInt(FWbemObject.ConfigManagerErrorCode)]));// Uint32 Writeln(Format('ConfigManagerUserConfig %s',[VarToStr(FWbemObject.ConfigManagerUserConfig)]));// Boolean Writeln(Format('CreationClassName %s',[VarToStr(FWbemObject.CreationClassName)]));// String // Writeln(Format('CurrentAlternateSettings %d',[VarToInt(FWbemObject.CurrentAlternateSettings)]));// Array of Uint8 Writeln(Format('CurrentConfigValue %d',[VarToInt(FWbemObject.CurrentConfigValue)]));// Uint8 Writeln(Format('Description %s',[VarToStr(FWbemObject.Description)]));// String Writeln(Format('DeviceID %s',[VarToStr(FWbemObject.DeviceID)]));// String Writeln(Format('ErrorCleared %s',[VarToStr(FWbemObject.ErrorCleared)]));// Boolean Writeln(Format('ErrorDescription %s',[VarToStr(FWbemObject.ErrorDescription)]));// String Writeln(Format('GangSwitched %s',[VarToStr(FWbemObject.GangSwitched)]));// Boolean Writeln(Format('InstallDate %s',[VarToStr(FWbemObject.InstallDate)]));// Datetime Writeln(Format('LastErrorCode %d',[VarToInt(FWbemObject.LastErrorCode)]));// Uint32 Writeln(Format('Name %s',[VarToStr(FWbemObject.Name)]));// String Writeln(Format('NumberOfConfigs %d',[VarToInt(FWbemObject.NumberOfConfigs)]));// Uint8 Writeln(Format('NumberOfPorts %d',[VarToInt(FWbemObject.NumberOfPorts)]));// Uint8 Writeln(Format('PNPDeviceID %s',[VarToStr(FWbemObject.PNPDeviceID)]));// String // Writeln(Format('PowerManagementCapabilities %d',[VarToInt(FWbemObject.PowerManagementCapabilities)]));// Array of Uint16 for j:= low(VarToInt(FWbemObject.PowerManagementCapabilities)) to high(VarToInt(FWbemObject.PowerManagementCapabilities)) do Writeln(Format('PowerManagementCapabilities %d',[VarToInt(FWbemObject.PowerManagementCapabilities[j])])); Writeln(Format('PowerManagementSupported %s',[VarToStr(FWbemObject.PowerManagementSupported)]));// Boolean Writeln(Format('ProtocolCode %d',[VarToInt(FWbemObject.ProtocolCode)]));// Uint8 Writeln(Format('Status %s',[VarToStr(FWbemObject.Status)]));// String Writeln(Format('StatusInfo %d',[VarToInt(FWbemObject.StatusInfo)]));// Uint16 Writeln(Format('SubclassCode %d',[VarToInt(FWbemObject.SubclassCode)]));// Uint8 Writeln(Format('SystemCreationClassName %s',[VarToStr(FWbemObject.SystemCreationClassName)]));// String Writeln(Format('SystemName %s',[VarToStr(FWbemObject.SystemName)]));// String Writeln(Format('USBVersion %d',[VarToInt(FWbemObject.USBVersion)]));// Uint16 Writeln(StringOfChar('-',80)); FWbemObject:=Unassigned; end; end; // Die Klasse "Win32_NetworkAdapter" stellt einen Netzwerkadapter in einem Win32- // System dar. procedure GetWin32_NetworkAdapterInfo; const WbemUser =''; WbemPassword =''; WbemComputer ='localhost'; wbemFlagForwardOnly = $00000020; var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet: OLEVariant; FWbemObject : OLEVariant; oEnum : IEnumvariant; iValue : LongWord; j : Integer; begin; FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword); FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapter','WQL',wbemFlagForwardOnly); oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; while oEnum.Next(1, FWbemObject, iValue) = 0 do begin Writeln(Format('AdapterType %s',[VarToStr(FWbemObject.AdapterType)]));// String Writeln(Format('AdapterTypeId %d',[VarToInt(FWbemObject.AdapterTypeId)]));// Uint16 Writeln(Format('AutoSense %s',[VarToStr(FWbemObject.AutoSense)]));// Boolean Writeln(Format('Availability %d',[VarToInt(FWbemObject.Availability)]));// Uint16 Writeln(Format('Caption %s',[VarToStr(FWbemObject.Caption)]));// String Writeln(Format('ConfigManagerErrorCode %d',[VarToInt(FWbemObject.ConfigManagerErrorCode)]));// Uint32 Writeln(Format('ConfigManagerUserConfig %s',[VarToStr(FWbemObject.ConfigManagerUserConfig)]));// Boolean Writeln(Format('CreationClassName %s',[VarToStr(FWbemObject.CreationClassName)]));// String Writeln(Format('Description %s',[VarToStr(FWbemObject.Description)]));// String Writeln(Format('DeviceID %s',[VarToStr(FWbemObject.DeviceID)]));// String Writeln(Format('ErrorCleared %s',[VarToStr(FWbemObject.ErrorCleared)]));// Boolean Writeln(Format('ErrorDescription %s',[VarToStr(FWbemObject.ErrorDescription)]));// String Writeln(Format('GUID %s',[VarToStr(FWbemObject.GUID)]));// String Writeln(Format('Index %d',[VarToInt(FWbemObject.Index)]));// Uint32 Writeln(Format('InstallDate %s',[VarToStr(FWbemObject.InstallDate)]));// Datetime Writeln(Format('Installed %s',[VarToStr(FWbemObject.Installed)]));// Boolean Writeln(Format('InterfaceIndex %d',[VarToInt(FWbemObject.InterfaceIndex)]));// Uint32 Writeln(Format('LastErrorCode %d',[VarToInt(FWbemObject.LastErrorCode)]));// Uint32 Writeln(Format('MACAddress %s',[VarToStr(FWbemObject.MACAddress)]));// String Writeln(Format('Manufacturer %s',[VarToStr(FWbemObject.Manufacturer)]));// String Writeln(Format('MaxNumberControlled %d',[VarToInt(FWbemObject.MaxNumberControlled)]));// Uint32 Writeln(Format('MaxSpeed %d',[VarToInt(FWbemObject.MaxSpeed)]));// Uint64 Writeln(Format('Name %s',[VarToStr(FWbemObject.Name)]));// String Writeln(Format('NetConnectionID %s',[VarToStr(FWbemObject.NetConnectionID)]));// String Writeln(Format('NetConnectionStatus %d',[VarToInt(FWbemObject.NetConnectionStatus)]));// Uint16 Writeln(Format('NetEnabled %s',[VarToStr(FWbemObject.NetEnabled)]));// Boolean // Writeln(Format('NetworkAddresses %s',[VarToStr(FWbemObject.NetworkAddresses)]));// Array of String // Writeln(Format('PermanentAddress %s',[VarToStr(FWbemObject.PermanentAddress)]));// String Writeln(Format('PhysicalAdapter %s',[VarToStr(FWbemObject.PhysicalAdapter)]));// Boolean Writeln(Format('PNPDeviceID %s',[VarToStr(FWbemObject.PNPDeviceID)]));// String // Writeln(Format('PowerManagementCapabilities %d',[VarToInt(FWbemObject.PowerManagementCapabilities)]));// Array of Uint16 for j:= low(VarToInt(FWbemObject.PowerManagementCapabilities)) to high(VarToInt(FWbemObject.PowerManagementCapabilities)) do Writeln(Format('PowerManagementCapabilities %d',[VarToInt(FWbemObject.PowerManagementCapabilities[j])])); Writeln(Format('PowerManagementSupported %s',[VarToStr(FWbemObject.PowerManagementSupported)]));// Boolean Writeln(Format('ProductName %s',[VarToStr(FWbemObject.ProductName)]));// String Writeln(Format('ServiceName %s',[VarToStr(FWbemObject.ServiceName)]));// String Writeln(Format('Speed %d',[VarToInt(FWbemObject.Speed)]));// Uint64 Writeln(Format('Status %s',[VarToStr(FWbemObject.Status)]));// String Writeln(Format('StatusInfo %d',[VarToInt(FWbemObject.StatusInfo)]));// Uint16 Writeln(Format('SystemCreationClassName %s',[VarToStr(FWbemObject.SystemCreationClassName)]));// String Writeln(Format('SystemName %s',[VarToStr(FWbemObject.SystemName)]));// String Writeln(Format('TimeOfLastReset %s',[VarToStr(FWbemObject.TimeOfLastReset)]));// Datetime Writeln(StringOfChar('-',80)); FWbemObject:=Unassigned; end; end; begin try CoInitialize(nil); try // GetWin32_USBHubInfo; GetWin32_NetworkAdapterInfo; finally CoUninitialize; end; except on E:EOleException do Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode])); on E:Exception do Writeln(E.Classname, ':', E.Message); end; Writeln('Press Enter to exit'); Readln; end. |
AW: Energieverwaltung von Netzwerkadaptern und USB Root Hub
ok, danke schonmal. Läuft bei mir auch soweit, aber die entsprechenden Zeilen
Delphi-Quellcode:
geben auch hier keine Werte aus. Dann ist die von mir gesuchte Information anscheinend nicht über WMI Funktionen auslesbar, oder sehe ich das falsch?
for j:= low(VarToInt(FWbemObject.PowerManagementCapabilities)) to high(VarToInt(FWbemObject.PowerManagementCapabilities)) do
Writeln(Format('PowerManagementCapabilities %d',[VarToInt(FWbemObject.PowerManagementCapabilities[j])])); Writeln(Format('PowerManagementSupported %s',[VarToStr(FWbemObject.PowerManagementSupported)]));// Boolean |
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:36 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz