just dns don't work, i need a function to change dnsprim, i try it, all worked least dns




unit UIP;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses ComObj, ActiveX, StdVcl; // <<< faltou

function ArrayToVarArray(Arr : Array Of string):OleVariant; overload;
var
i : integer;
begin
Result :=VarArrayCreate([0, High(Arr)], varVariant);
for i:=Low(Arr) to High(Arr) do
Result[i]:=Arr[i];
end;

procedure SetStaticIP;
const
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
vIPAddress : OleVariant;
vSubnetMask : OleVariant;
vDefaultIPGateway : OleVariant;
vGatewayCostMetric : OleVariant;
begin
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=True','WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
vIPAddress := ArrayToVarArray([Form1.Edit1.Text]); // <<<<<<<<<<<<<
vSubnetMask := ArrayToVarArray(['255.255.255.0']);
if FWbemObject.EnableStatic(vIPAddress, vSubnetMask) = 0 then
begin
vDefaultIPGateway := ArrayToVarArray([Form1.Edit2.Text]); // <<<<<<<<<<<<
vDefaultIPGateway := ArrayToVarArray(['Form1.Edit2.Text']); // <<<<<<<<<<<<
vGatewayCostMetric := ArrayToVarArray(['1']); // << alterar aqui
vDNSPrim := ArrayToVarArray(['192.168.20.5']);
FWbemObject.SetGateways(vDefaultIPGateway,vGatewayCostMetric,vDNSPrim);
end;
VarClear(vIPAddress);
VarClear(vSubnetMask);
VarClear(vDefaultIPGateway);
VarClear(vGatewayCostMetric);
VarClear(vDNSPrim);
FWbemObject:=Unassigned;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SetStaticIP;
end;

end.