ITPub博客

首页 > Linux操作系统 > Linux操作系统 > DELPHI 设置网关、DNS

DELPHI 设置网关、DNS

原创 Linux操作系统 作者:zaorv 时间:2019-01-22 08:27:04 0 删除 编辑
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure SetPrivilege; //提升权限为DEBUG的权限。
var
OldTokenPrivileges, TokenPrivileges: TTokenPrivileges;
ReturnLength: dword;
hToken: THandle;
Luid: int64;
begin
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);
LookupPrivilegeValue(nil, 'SeDebugPrivilege', Luid);
TokenPrivileges.Privileges[0].luid := Luid;
TokenPrivileges.PrivilegeCount := 1;
TokenPrivileges.Privileges[0].Attributes := 0;
AdjustTokenPrivileges(hToken, False, TokenPrivileges, SizeOf(TTokenPrivileges), OldTokenPrivileges, ReturnLength);
OldTokenPrivileges.Privileges[0].luid := Luid;
OldTokenPrivileges.PrivilegeCount := 1;
OldTokenPrivileges.Privileges[0].Attributes := TokenPrivileges.Privileges[0].Attributes or SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, False, OldTokenPrivileges, ReturnLength, PTokenPrivileges(nil)^, ReturnLength);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
netshtxt:string;
begin
//设置网关
netshtxt:='netsh interface ip set address name="本地连接" gateway=192.168.1.1 gwmetric=0';
winexec(pchar(netshtxt),sw_hide);

// showmessage('上网,网关DNS设置成功。');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
netshtxt2:string;
begin
SetPrivilege;
//设置网关
netshtxt2:='netsh interface ip set address name="本地连接" gateway=192.168.1.2 gwmetric=0';
winexec(pchar(netshtxt2),sw_hide);
//设置DNS
// netshDNS2:='netsh interface ip set dns "本地连接" static 192.168.1.2 register=PRIMARY';
/// winexec(pchar(netshDNS2),sw_hide);
// showmessage('不上网,网关DNS设置成功。');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
SetPrivilege; //提升到DEBUG权限 。
end;

procedure TForm1.Button3Click(Sender: TObject);
VAR
netshDns:string;
begin
//设置DNS
netshDNS:='netsh interface ip set dns "本地连接" static 192.168.1.1 register=PRIMARY';
winexec(pchar(netshDNS),sw_hide);
end;

procedure TForm1.Button4Click(Sender: TObject);
var
netshdns2:string;
begin
//设置DNS
netshDNS2:='netsh interface ip set dns "本地连接" static 192.168.1.2 register=PRIMARY';
winexec(pchar(netshDNS2),sw_hide);
end;

end.
提升DEBUG权限也没用,在无ADMINISTRATOR权限下还是不能将IP、DNS设置。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/729024/viewspace-564876/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 字符合并函数
下一篇: 保存功能
请登录后发表评论 登录
全部评论

注册时间:2018-09-12

  • 博文量
    192
  • 访问量
    144870