this slowpoke moves

Change Gateway IP

Hier ist ein Beispiel, wie man der Windows-Konsole über eine Form den Befehl zur Änderung des Gateways und Subnetzmaske erteilt.

uses ActiveX, ComObj

//

function ExecConsole(const ACommand: String;
var AOutput, AErrors: String; var AExitCode: Cardinal): Boolean;
var
    StartupInfo: TStartupInfo;
    ProcessInfo: TProcessInformation;
    SecurityAttr: TSecurityAttributes;
    PipeOutputRead, PipeOutputWrite, PipeErrorsRead, PipeErrorsWrite: THandle;

procedure ReadPipeToString(const hPipe: THandle; var Result: String);
const MEM_CHUNK_SIZE = 8192;
var
      NumberOfBytesRead,  NumberOfBytesTotal: Cardinal;
begin
    Result := '';  NumberOfBytesTotal := 0; repeat
    SetLength(Result,Length(Result) +MEM_CHUNK_SIZE);
      if ReadFile(hPipe,(@Result[1+NumberOfBytesTotal])^,MEM_CHUNK_SIZE,
                  NumberOfBytesRead,NIL) then
        Inc(NumberOfBytesTotal,NumberOfBytesRead);
      SetLength(Result,NumberOfBytesTotal);  until (NumberOfBytesRead = 0);
  end;
begin
  FillChar(ProcessInfo,SizeOf(TProcessInformation),0);
  FillChar(SecurityAttr,SizeOf(TSecurityAttributes),0);
  SecurityAttr.nLength := SizeOf(SecurityAttr);
  SecurityAttr.bInheritHandle := TRUE;
  SecurityAttr.lpSecurityDescriptor := NIL;
  CreatePipe(PipeOutputRead,PipeOutputWrite,@SecurityAttr,0);
  CreatePipe(PipeErrorsRead,PipeErrorsWrite,@SecurityAttr,0);
  FillChar(StartupInfo,SizeOf(TStartupInfo),0);
  StartupInfo.cb := SizeOf(StartupInfo);
  StartupInfo.hStdInput := 0;
  StartupInfo.hStdOutput := PipeOutputWrite;
  StartupInfo.hStdError := PipeErrorsWrite;
  StartupInfo.wShowWindow := SW_HIDE;
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  Result := CreateProcess(NIL,PChar(ACommand),NIL,NIL,TRUE,
                          CREATE_DEFAULT_ERROR_MODE or CREATE_NEW_CONSOLE
  or NORMAL_PRIORITY_CLASS, NIL,NIL,StartupInfo,ProcessInfo);
  CloseHandle(PipeOutputWrite); CloseHandle(PipeErrorsWrite);
  if (Result) then begin
    ReadPipeToString(PipeOutputRead,AOutput);
    ReadPipeToString(PipeErrorsRead,AErrors);
    WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess,AExitCode);
    CloseHandle(ProcessInfo.hProcess);
  end;  CloseHandle(PipeOutputRead); CloseHandle(PipeErrorsRead);
end;

procedure  SetStaticIpAddress(const NetworkCard, IPAddress, Mask, GateWay :string);
const
  WbemUser    ='';
  WbemPassword='';
  WbemComputer='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator   : OLEVariant;
  FWMIService     : OLEVariant;
  FWbemObjectSet  : OLEVariant;
  FWbemObject     : OLEVariant;
  FOutParams      : OLEVariant;
  vIpAddress      : OLEVariant;
  vGateWays       : OLEVariant;
  vMask           : OLEVariant;
  oEnum           : IEnumvariant;
  iValue          : LongWord;
begin
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);

  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_NetworkAdapterConfiguration Where Description="%s"',[NetworkCard]),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin
    vIpAddress   := VarArrayCreate([0, 0], varVariant);
    vIpAddress[0]:= IPAddress;
    vMask   := VarArrayCreate([0, 0], varVariant);
    vMask[0]:=  Mask;
    FOutParams:=FWbemObject.EnableStatic(vIpAddress, vMask);
    // 0 - Successful completion, no reboot required
    // 1 - Successful completion, reboot required
    //Writeln(Format('EnableStatic ReturnValue  %s',[FOutParams]));

    vGateWays   := VarArrayCreate([0, 0], varVariant);
    vGateWays[0]:= GateWay;

    FOutParams:=FWbemObject.SetGateways(vGateWays);
    // 0 - Successful completion, no reboot required
    // 1 - Successful completion, reboot required
    Writeln(Format('SetGateways ReturnValue  %s',[FOutParams]));
  end
  else
  //Writeln('Network card not found');
end;
Addresse ändern :
procedure TForm1.Button1Click(Sender: TObject);
begin
  begin
 try
    CoInitialize(nil);
    try
      SetStaticIpAddress(Edit1.Text, Edit2.Text, Edit3.Text,'192.168.0.1');
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        ShowMessage(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
 end;
 //Writeln('Press Enter to exit');
 //Readln;
 end;
end;
Controller auslesen :
procedure TForm1.Button2Click(Sender: TObject);
var Output, Errors: String; RC: Cardinal; tmp: TStringList;
    s : string;
begin
    Memo1.Clear;
    tmp := TStringList.Create; try
    if ExecConsole('wmic nic get Name', Output,Errors,RC) then begin
      if (Output <> '') then begin
        OemToCharBuffA(PChar(Output),PChar(Output),Length(Output));
        tmp.Text :=  Output;
        Memo1.Lines.AddStrings(tmp);
      end;
      if (Errors <> '') then begin
        OemToCharBuffA(PChar(Errors),PChar(Errors),Length(Errors));
        tmp.Text := Errors;
        Memo1.Lines.AddStrings(tmp);
        end; end else
  finally
  tmp.Free;
  end;
  s := Memo1.Lines[4];
  S := Copy(S, 1 ,length(S)-5);
  Edit1.Text := s;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate