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;
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;
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) // USB2.0
then Memo1.Lines.AddStrings(Output); Memo1.Lines.Add('');
if GetConsoleOutput('devcon.exe disable USB\ROOT_HUB30', Output, Errors) // USB3.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) // USB2.0
then Memo1.Lines.AddStrings(Output); Memo1.Lines.Add('');
if GetConsoleOutput('devcon.exe enable USB\ROOT_HUB30', Output, Errors) // USB3.0
then Memo1.Lines.AddStrings(Output);
finally
Output.free;
Errors.free;
end;
end;