注意不是局域网!我查找到两个函数在连上局域网的情况下都当成了连上internet:
procedure TForm1.Button1Click(Sender: TObject);
begin
if GetSystemMetrics(SM_NETWORK) AND $01 = $01 then
ShowMessage('Machine is attached to network') else
ShowMessage('Machine is not attached to network');
end;
if InternetGetConnectedState(@flags, 0) then
showmessage('在网上')
else
showmessage('不在网上');INetDetector也是局域网的。
procedure TForm1.Button1Click(Sender: TObject);
begin
if GetSystemMetrics(SM_NETWORK) AND $01 = $01 then
ShowMessage('Machine is attached to network') else
ShowMessage('Machine is not attached to network');
end;
if InternetGetConnectedState(@flags, 0) then
showmessage('在网上')
else
showmessage('不在网上');INetDetector也是局域网的。
解决方案 »
- 请教,新出的rad studio xe2有教程卖吗?
- object pascal问题 (数据类开型转换问题)
- 新手求助,存储过程问题
- 怎么实现DBEDIT上的DATETIMEPICKER获得日期的效果?
- 祝大家中秋快乐!
- 我能不能即用except 又用finally请问怎么写
- delphi 字体font在哪个单元里定义?
- 在Delphi 里面如何查看一个动态链里面有什么函数
- 如何获取ClientDataSet的当前行的状态(如插入\修改等)
- 用Inno_Setup打包文件夹时怎么排除其中一个文件。
- 如何用 TREGINIFile 类(或其它类)向注册表中写入 DWORD 类型的数据呢?
- 如何改变Delphi的快捷键
判断是否已经联到internet
使用 NetMasters Powersock 控件读取本地IP 地址,如果是"0.0.0.0" 说明没有连接。
例子:
void __fastcall TForm1::Button1Click(Tobject *Sender)
{
if (Powersock1->LocalIP == "0.0.0.0")
ShowMessage("not connected");
else
ShowMessage(Powersock1->LocalIP);
}
这种方法最简单,利用FastNet页的 Powersock控件的LocalIP属性即可判断:
if (Powersock1->LocalIP<>"127.0.0.1"):在线
else:离线
特点:[1]判断连接状态,[2]获得本地IP。 2.使用URL.DLL的InetIsOffline(0) 函数:
Win2K:URL.DLL存放在\SYSTEM32\;
Win9x:URL.DLL存放在\SYSTEM\;
用GetSystemDirectory(...)得到系统目录。
InetIsOffline(0)返回值:
TRUE: 离线; FALSE:在线。
特点:判断连接状态。 3.WinSock编程法:见程序
特点:[1]判断连接状态;[2]获得本地IP和主机名。 4.WinInet.DLL的InternetGetConnectedState(&dwFlag,0)函数:
注意:为使用该函数,须在项目文件中加入:USELIB("WinInet.LIB")
特点:获得较详的连接描述! 5.RASAPI32.DLL的RasEnumConnections函数:
要使用该“枚举所有活动连接”函数,必须:
#include "ras.h"。 若连接数>0:本机当前已连入Internet;
否则: 本机当前未连入Internet; 源码如下,在[BCB5 + WIN2K + 拨号上网]下通过(N字头的为菜单项): -------------Powersock控件法-----------------------------------------
procedure TForm1.N11Click(Sender:TObject)
begin
if (Powersock1->LocalIP="127.0.0.1") then
ShowMessage("未连接:"+Powersock1.LocalIP)
else ShowMessage("已连接:"+Powersock1.LocalIP);
end;
-------------URL.DLL的InetIsOffline函数法----------------------------
HINSTANCE hDLL;
typedef bool __stdcall(*FUN)(int); 定义DLL函数指针FUN
FUN isOffLine;
void __fastcall TForm1::N21Click(TObject *Sender)
{
char Buffer[MAX_PATH];
GetSystemDirectory(Buffer,MAX_PATH);
hDLL=LoadLibrary((AnsiString(Buffer)+"\\URL.DLL").c_str());
if(hDLL==NULL){ ShowMessage("Cannot load URL.DLL! Return... "); return; }
isOffLine=(FUN)GetProcAddress(hDLL,"InetIsOffline");
if(isOffLine==NULL){ ShowMessage("Cannot load InetIsOffline(int), Return..."); return; }
if(!isOffLine(0)) ShowMessage("已连接");
else ShowMessage("未连接");
FreeLibrary(hDLL);
}
------------WinSock法------------------------------------------------
void __fastcall TForm1::N31Click(TObject *Sender)
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested=MAKEWORD(1,1); Start up WinSock
WSAStartup(wVersionRequested,&wsaData);
-----------------------------------------
hostent *p; char *p2; char s[128];
gethostname(s,128); Get the computer name
p=gethostbyname(s);
p2=inet_ntoa(*((in_addr *)p->h_addr)); Get the IpAddress
-----------------------------------------
AnsiString LocationIP=p2;
if(LocationIP=="127.0.0.1")
ShowMessage("未连接:"+LocationIP);
else ShowMessage("已连接:"+LocationIP);
WSACleanup();
}
-----------WinInet.DLL的InternetGetConnectedState函数法----------------
void __fastcall TForm1::N41Click(TObject *Sender)
{
StaticText1->Caption=""; StaticText2->Caption=""; StaticText3->Caption="";
StaticText4->Caption=""; StaticText5->Caption=""; StaticText6->Caption="";
StaticText7->Caption="";
DWORD dwFlag;
InternetGetConnectedState(&dwFlag,0);
if(dwFlag & INTERNET_CONNECTION_MODEM) StaticText1->Caption="Yes"; MODEM连接
else StaticText1->Caption="No";
if(dwFlag & INTERNET_CONNECTION_LAN) StaticText2->Caption="Yes"; LAN连接
else StaticText2->Caption="No";
if(dwFlag & INTERNET_CONNECTION_PROXY) StaticText3->Caption="Yes"; 代理连接
else StaticText3->Caption="No";
---------检查是否连接-------------------------------------------
if(InternetGetConnectedState(NULL,0)) StaticText4->Caption="Yes"; 在线
else StaticText4->Caption="No";
if(dwFlag & INTERNET_CONNECTION_OFFLINE) StaticText5->Caption="Yes";//离线。注:不好用!
else StaticText5->Caption="No";
----------------------------------------------------------------
if(dwFlag & INTERNET_RAS_INSTALLED) StaticText6->Caption="Yes";
else StaticText6->Caption="No";
if(dwFlag & INTERNET_CONNECTION_CONFIGURED) StaticText7->Caption="Yes";
else StaticText7->Caption="No";
}
----------RASAPI32.DLL的RasEnumConnections函数法---------------------------
#include "ras.h"
void __fastcall TForm1::N51Click(TObject *Sender)
{
RASCONN RASconn[256]; 活动连接数组
DWORD BuffSize; 数组所占内存大小;
DWORD ConnNum; 活动连接数目
RASconn[0].dwSize=sizeof(RASCONN); 必须指定一个连接[数组元素]的内存大小;
BuffSize=sizeof(RASCONN)*256;
DWORD dwReturn=RasEnumConnections(RASconn,&BuffSize,&ConnNum);
if(dwReturn==0)
{
if(ConnNum>0) ShowMessage("已连接。当前激活连接数:"+AnsiString(ConnNum));
else ShowMessage("未连接。当前激活连接数:"+AnsiString(ConnNum));
}
else ShowMessage("RasEnumConnections函数失败!");
}