I have an application that at some point should disable and enable the Windows network adapter, I searched the internet, but I did not find anything.
Test before posting a response because dubious information only slows down my work.
I have an application that at some point should disable and enable the Windows network adapter, I searched the internet, but I did not find anything.
Test before posting a response because dubious information only slows down my work.
Using the following code you can enable and disable a network card of choice, sending commands to the command line and handling your output, I tried to detail everything which happens in the code to realize the steps:
Procedure TfrmMain.ResetNetwork;
var i, APos: Integer;
ALine, AIndex, AName: String;
begin
//pega todas as redes do pc
Memo1.Text := Memo1.Text + GetDosOutput('wmic nic get name, index');
Application.ProcessMessages;
//vai ler todas linhas do memo uma a uma
for i := 0 to Memo1.Lines.Count - 1 do
Begin
//passa a linha para uma variavel
//exemplo da linha pretendida:
//"0 Microsoft Kernel Debug Network Adapter"
ALine := AnsiUpperCase(Memo1.Lines[i]);
//pega o valor "index
APos := Pos(' ', ALine);
if (APos > 0) then
begin
AIndex := Trim(AnsiMidStr(ALine, 1, APos - 1));
Delete(ALine, 1, APos);
end;
//verifica se o valor "name" é igual ao pretendido
if AnsiLeftStr(Trim(ALine), 38) = 'MICROSOFT KERNEL DEBUG NETWORK ADAPTER' then
Begin
//se o valor for igual passa aqui
Memo1.Lines.Add('DISABLE index: ' + AIndex);
//aqui desabilita
GetDosOutput('wmic path win32_networkadapter where index='+ AIndex +' call disable');
Application.ProcessMessages;
sleep(2000);
Memo1.Lines.Add('ENABLE index: ' + AIndex);
//aqui habilita
GetDosOutput('wmic path win32_networkadapter where index='+ AIndex +' call enable');
Application.ProcessMessages;
End;
End;
End;
Function TfrmMain.GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
var SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[0..255] of AnsiChar;
BytesRead: Cardinal;
WorkDir: string;
Handle: Boolean;
begin
Result := '';
with SA do
begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
try
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
WorkDir := Work;
Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine), nil, nil, True, 0, nil, PChar(WorkDir), SI, PI);
CloseHandle(StdOutPipeWrite);
if Handle then
try
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
Result := Result + Buffer;
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(PI.hProcess, INFINITE);
finally
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
finally
CloseHandle(StdOutPipeRead);
end;
end;