首先要看你所认为的“连接到了internet”是什么标准。
简单的只要 ping 的通internet上面的一个服务器就行了。
简单的只要 ping 的通internet上面的一个服务器就行了。
解决方案 »
- 用delphi可以直接针对数据链路层进行编程吗?
- 如何标识DELPHI中的TreeView的某一个节点??
- DBNavigator控件中有没有办法在Post按钮之前对数据有效性进行检查?
- 100分求助:请问怎样用Delphi实现IP地址与MAC地址的绑定,急,在线等!!
- 安装后运行,提示没找到VCLJPG70.BPL?
- 1.MIDAS中接口的注册?2.服务器端DataModel的继承问题?
- 这个函数调用的不对吗?
- DBEdit如何实现文字中间对齐
- 急求installshield express for delphi 7.0
- 请教查询时间的sql语句!
- 如何取得color的值 onLine...
- 请问各位,如何用Printer实现选择页的打印,我只想打印第3页到第6页,如何实现???????????????????
uses
Windows, SysUtils, Registry, WinSock, WinInet, ShellAPI;type
TConnectionType = (ctNone, ctProxy, ctDialup);function ConnectedToInternet : TConnectionType;
function RasConnectionCount : Integer;
function RasHangUp : Boolean;implementation//For RasConnectionCount =======================
const
cERROR_BUFFER_TOO_SMALL = 603;
cRAS_MaxEntryName = 256;
cRAS_MaxDeviceName = 128;
cRAS_MaxDeviceType = 16;
type
ERasError = class(Exception); HRASConn = DWord;
PRASConn = ^TRASConn;
TRASConn = record
dwSize: DWORD;
rasConn: HRASConn;
szEntryName: Array[0..cRAS_MaxEntryName] Of Char;
szDeviceType : Array[0..cRAS_MaxDeviceType] Of Char;
szDeviceName : Array [0..cRAS_MaxDeviceName] of char;
end; TRasEnumConnections =
function (RASConn: PrasConn; { buffer to receive Connections data }
var BufSize: DWord; { size in bytes of buffer }
var Connections: DWord { number of Connections written to buffer }
): LongInt; stdcall;
//End RasConnectionCount =======================
TRasHangUp =
function (RasConn : HRasConn
): Boolean; stdcall;
function ConnectedToInternet: TConnectionType;
var
Reg : TRegistry;
bUseProxy : Boolean;
UseProxy : LongWord;
begin
Result := ctNone;
Reg := TRegistry.Create;
with REG do
try
try
RootKey := HKEY_CURRENT_USER;
if OpenKey('\Software\Microsoft\Windows\CurrentVersion\Internet settings',False) then begin
//I just try to read it, and trap an exception
if GetDataType('ProxyEnable') = rdBinary then
ReadBinaryData('ProxyEnable', UseProxy, SizeOf(LongWord) )
else begin
bUseProxy := ReadBool('ProxyEnable');
if bUseProxy then
UseProxy := 1
else
UseProxy := 0;
end;
if (UseProxy <> 0) and ( ReadString('ProxyServer') <> '' ) then Result := ctProxy;
end;
except
//Obviously not connected through a proxy
end;
finally
Free;
end; //We can check RasConnectionCount even if dialup networking is not installed
//simply because it will return 0 if the DLL is not found.
if Result = ctNone then begin
if RasConnectionCount > 0 then Result := ctDialup;
end;
end;function RasConnectionCount : Integer;
var
RasDLL : HInst;
Conns : Array[1..4] of TRasConn;
RasEnums : TRasEnumConnections;
BufSize : DWord;
NumConns : DWord;
RasResult : Longint;
begin
Result := 0; //Load the RAS DLL
RasDLL := LoadLibrary('rasapi32.dll');
if RasDLL = 0 then exit; try
RasEnums := GetProcAddress(RasDLL,'RasEnumConnectionsA');
if @RasEnums = nil then
raise ERasError.Create('RasEnumConnectionsA not found in rasapi32.dll'); Conns[1].dwSize := Sizeof (Conns[1]);
BufSize := SizeOf(Conns); RasResult := RasEnums(@Conns, BufSize, NumConns); If (RasResult = 0) or (Result = cERROR_BUFFER_TOO_SMALL) then Result := NumConns;
finally
FreeLibrary(RasDLL);
end;
end;function RasHangUp : Boolean;
var
RasDLL : HInst;
Conns : Array[1..4] of TRasConn;
RasEnums : TRasEnumConnections;
BufSize : DWord;
NumConns : DWord;
RasResult : Longint;
begin
Result := False; //Load the RAS DLL
RasDLL := LoadLibrary('rasapi32.dll');
if RasDLL = 0 then exit; try
RasEnums := GetProcAddress(RasDLL,'RasEnumConnectionsA');
if @RasEnums = nil then
raise ERasError.Create('RasEnumConnectionsA not found in rasapi32.dll'); Conns[1].dwSize := Sizeof (Conns[1]);
BufSize := SizeOf(Conns); RasResult := RasEnums(@Conns, BufSize, NumConns);
If (RasResult = 0) or (RasResult = cERROR_BUFFER_TOO_SMALL) then Result := False;
finally
FreeLibrary(RasDLL);
end;
end;end.
state:Boolean;
begin
state := InternetGetConnectedState(@flags, 0);
//返回值为true,则与internet相连,否则为断开
if state then
showmessage('connected')
else
showmessage('not connected');
end;
试一试,看看能不能用
刚写的代码如下,看看哪有问题。
flags就是返回的结果var
flags: DWORD;
state: boolean;
begin
flags:=0;
state:= InternetGetConnectedState(@flags, 0);
if (State) then begin
ShowMessage(IntToStr(flags));
end
else
ShowMessage(':( No');
end; INTERNET_CONNECTION_MODEM = 1;
{$EXTERNALSYM INTERNET_CONNECTION_MODEM}
INTERNET_CONNECTION_LAN = 2;
{$EXTERNALSYM INTERNET_CONNECTION_LAN}
INTERNET_CONNECTION_PROXY = 4;
{$EXTERNALSYM INTERNET_CONNECTION_PROXY}
INTERNET_CONNECTION_MODEM_BUSY = 8;
{$EXTERNALSYM INTERNET_CONNECTION_MODEM_BUSY}我的机器返回的是18,变为16+2,但是我把网线把下来还是18