请教怎样在vc中知道我的电脑是否连上网了.有api吗,还是要接收什么消息.

xcopy 2001-03-25 03:06:00
...全文
122 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
happylaodu 2001-03-25
  • 打赏
  • 举报
回复
#include "ras.h"
引入 RASAPI32.LIB
此方法仅适用于单机拨号上网。
至于局域网,我用了InetIsOffline函数,但没有成功,目前不知道怎么弄。

RASCONN connect[20];
DWORD size,number;
DWORD i;

connect[0].dwSize=sizeof(RASCONN);
size=sizeof(connect);

BOOL bIsConnected=FALSE;
if( RasEnumConnections( connect, &size, &number ) == 0 )
{
for( i = 0; i < number; i++ )
{
// Hang up that connection
RASCONNSTATUS status;
status.dwSize=sizeof(RASCONNSTATUS);
if(RasGetConnectStatus(connect[i].hrasconn,&status)==0)
if(status.rasconnstate==RASCS_Connected)
{
bIsConnected=TRUE;
break;
}
}
}
if(bIsConnected) //就是连到网上了。
panda_w 2001-03-25
  • 打赏
  • 举报
回复
A lot of times, programming internet applications you need to know wether the computer is connected to the internet?
In some occasions you can try to use RasEnumConnections, in most cases on a typical computer opened Ras Connection mean that the user is connected to the internet. However, this idea won't work in case the user is connected to internet not via Ras device or is connected to some kind of local network.
We try to use common sence here. Since internet is something abstract, lets decide that user which is connected to internet, is the one who can connect to www.microsoft.com.
First I wanted to use ping, but found a lot of troubles with it. Therefore, I have decided to use sockets "directly". I didn't want to write some fance code here, rather I have wrote a simple function you can cut & paste into your application.
Another problem adressed in the function that in some computer a try to establish internet connection will bring dialup dialog. The function solve it by temporary disabling the feature in the registry and enabling it back afterwards.
Pay attention to the comments.


BOOL YourClassHere::IsInternetConnected (void)
{
int nCheck = AfxSocketInit();
CSocket m_Server;
HKEY hKey;
DWORD dwDial, dwDialType = REG_DWORD, dwDialSize = 4;
DWORD dwNew = 0;
BOOL bResult = true;

if ( RegOpenKeyEx ( HKEY_CURRENT_USER,
Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
0, KEY_SET_VALUE, &hKey) != ERROR_SUCCESS)
; // We cannot find the key. Handle this situation or just continue

if ( RegQueryValueEx( hKey, "EnableAutodial", NULL, &dwDialType,
(BYTE *) &dwDial, &dwDialSize ) != ERROR_SUCCESS )
; // We cannot find the value. Handle it.

if ( dwDial ) { // We need to change the value, in order to make
// a dialup window not to show up.

if ( (nCheck = RegSetValueEx( hKey, "EnableAutodial", NULL,
dwDialType, (BYTE *) &dwNew, dwDialSize )) != ERROR_SUCCESS)
; // Failed? We shouldn't get here. You decide how to handle it
}


if ( !m_Server.Create() ) {
// m_sError = _T( "Unable to create the socket." );
bResult = false;
}

// You can use www.microsoft.com in order to check whether DNS is available
// or numeric IP otherwise
else if ( !m_Server.Connect( "www.microsoft.com", 80 ) ) { // 207.46.130.150
//m_sError = _T( "Unable to connect to server" );
m_Server.Close();
bResult = false;
}


if ( dwDial ) {
if ( (nCheck = RegSetValueEx( hKey, "EnableAutodial", NULL,
dwDialType, (BYTE *) &dwDial, dwDialSize )) != ERROR_SUCCESS)
; // Failed? We shouldn't get it. You decide how to handle this.
}

RegCloseKey( hKey );
return ( bResult );
}
sunriselx 2001-03-25
  • 打赏
  • 举报
回复
关注
malletking 2001-03-25
  • 打赏
  • 举报
回复
关注!
panda_w 2001-03-25
  • 打赏
  • 举报
回复
这个函数经常不能正确的返回,比如说只要有网卡相连,就返回机器已经连上网
正确的做法是配合使用InetIsOffline函数,具体的使用见前面有人问过这个问题,你可以搜索一下
FBStudio 2001-03-25
  • 打赏
  • 举报
回复
在project中加入wininet.lib,包含afxinet.h,使用InternetGetConnectedState函数.



phoenixtree 2001-03-25
  • 打赏
  • 举报
回复
关注!

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧