如何去掉VPN的“在远程网络上使用默认网关” ( 积分: 100 )

  • 主题发起人 主题发起人 冷雨叶
  • 开始时间 开始时间

冷雨叶

Unregistered / Unconfirmed
GUEST, unregistred user!
我下面的代码没效果

RasEntry.dwfOptions := RasEntry.dwfOptions or (not RASEO_RemoteDefaultGateway);


MSDN上是这样讲的,

RASEO_RemoteDefaultGateway

If this flag is set, the default route for IP packets is through the dial-up adapter when the connection is active. If this flag is clear, the default route is not modified.
This flag corresponds to the Use default gateway on remote network check box in the TCP/IP settings dialog box.
 
我下面的代码没效果

RasEntry.dwfOptions := RasEntry.dwfOptions or (not RASEO_RemoteDefaultGateway);


MSDN上是这样讲的,

RASEO_RemoteDefaultGateway

If this flag is set, the default route for IP packets is through the dial-up adapter when the connection is active. If this flag is clear, the default route is not modified.
This flag corresponds to the Use default gateway on remote network check box in the TCP/IP settings dialog box.
 
高手在哪里?
 
从语法上讲,应该是RasEntry.dwfOptions := RasEntry.dwfOptions and (not RASEO_RemoteDefaultGateway);吧,你试试.
 
也就是说用 and 而不是 or
 
不知道你的为什么不行,看一下我的程序吧,我测试过没问题(我的VPN连接名称为"vpn.google.com")
unit Unit1;

interface

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

type
TForm1 = class(TForm)
btGetOption: TButton;
btSetFlag: TButton;
CheckBox1: TCheckBox;
procedure btGetOptionClick(Sender: TObject);
procedure btSetFlagClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Ras;

{$R *.dfm}

procedure TForm1.btGetOptionClick(Sender: TObject);
var
entry: TRasEntry;
w: DWORD;
begin
FillChar(entry, sizeof(entry), 0);
entry.dwSize:=sizeof(entry);

//取得当前状态
RasGetEntryProperties(nil, 'vpn.google.com', @entry, w, nil, nil);

CheckBox1.Checked:=(entry.dwfOptions and RASEO_RemoteDefaultGateway)=RASEO_RemoteDefaultGateway;
if CheckBox1.Checked then
ShowMessage('当前使用默认网关!')
else
ShowMessage('当前没有使用默认网关!');
end;

procedure TForm1.btSetFlagClick(Sender: TObject);
var
entry: TRasEntry;
w: DWORD;
begin
FillChar(entry, sizeof(entry), 0);
entry.dwSize:=sizeof(entry);

//取得当前状态

RasGetEntryProperties(nil, 'vpn.google.com', @entry, w, nil, nil);

//设置使用默认网关
if CheckBox1.Checked and ((entry.dwfOptions and RASEO_RemoteDefaultGateway)=0) then
entry.dwfOptions := entry.dwfOptions or RASEO_RemoteDefaultGateway

//取消使用默认网关
else if (not CheckBox1.Checked) and ((entry.dwfOptions and RASEO_RemoteDefaultGateway)=RASEO_RemoteDefaultGateway) then
entry.dwfOptions := entry.dwfOptions and (not RASEO_RemoteDefaultGateway)
else
Exit;

//设置状态
if RasSetEntryProperties(nil, 'vpn.google.com', @entry, w, nil, 0)=0 then
ShowMessage('设置成功')
else
ShowMessage('设置失败: '+SysErrorMessage(GetLastError));
end;

end.
 
需要引用Ras.pas.
还有dfm在下面:
object Form1: TForm1
Left = 192
Top = 129
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object btGetOption: TButton
Left = 56
Top = 96
Width = 75
Height = 25
Caption = 'GetOption'
TabOrder = 0
OnClick = btGetOptionClick
end
object btSetFlag: TButton
Left = 224
Top = 80
Width = 75
Height = 25
Caption = 'SetFlag'
TabOrder = 1
OnClick = btSetFlagClick
end
object CheckBox1: TCheckBox
Left = 224
Top = 44
Width = 181
Height = 17
Caption = 'Use default Gateway'
TabOrder = 2
end
end
 
我测试了,你的可以!谢拉。
还是delphibbs高手多,CSDN上没人回答。。
 
在创建时
entry.dwfOptions := entry.dwfOptions and (not RASEO_RemoteDefaultGateway)
还是不行的
只能在创建后,再修改entry的属性。
 
后退
顶部