Usb port aç kapa

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
Lord_Ares
Üye
Mesajlar: 1070
Kayıt: 15 Eki 2006 04:33
Konum: Çorlu

Usb port aç kapa

Mesaj gönderen Lord_Ares »

Merhaba,
Telefonu bilgisayarın usb portuna takıp şarj etmelerini engellemek için usb portlarını açıp kapatabilen bir uygulama yazmak istiyorum. Amacım programdan izin verilirse usb portu kullanıma açmak verilmezse kapatmak. Konuya nereden başlarım, nasıl yaparım fikrim yok. Konu hakkında bilgilerinizi paylaşırsanız sevinirim.

Teşekkürler
cagatay77
Üye
Mesajlar: 37
Kayıt: 31 Eki 2003 12:34

Re: Usb port aç kapa

Mesaj gönderen cagatay77 »

Windows içerisindeki Windows Device Console (Devcon.exe)'yi kullanarak yapabilirsin. Aşağıda, hazırlanmış örnek bir kodu görebilirsin.

Kod: Tümünü seç

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;
Lord_Ares
Üye
Mesajlar: 1070
Kayıt: 15 Eki 2006 04:33
Konum: Çorlu

Re: Usb port aç kapa

Mesaj gönderen Lord_Ares »

Çok teşekkürler hemen inceleyip deneyip sonuçları bildiriyorum.
Cevapla