没环境可试.
===================
The following code works on my side:
using System;using System.Runtime.InteropServices; namespace RAS_Application
{ [StructLayout(LayoutKind.Sequential, Pack=1)] public struct RASCONN { public int dwSize; public IntPtr hrasconn; public string szEntryName;
} public class RAS
{
[DllImport("Rasapi32.dll", EntryPoint="RasEnumConnectionsA",
SetLastError=true)] public static extern int API_RasEnumConnections ( ref RASCONN lprasconn, // buffer to receive connections data ref int lpcb, // size in bytes of buffer ref int lpcConnections // number of connections written to buffer );
}
class Test
{
[STAThread]
static void Main(string[] args)
{ RASCONN lpRasConn =new RASCONN(); int lpcb =0; int lpcConnections =0; lpRasConn.dwSize = 412; lpcb = lpRasConn.dwSize; int nRet = RAS.API_RasEnumConnections(ref lpRasConn, ref lpcb, ref
lpcConnections); if(nRet != 0) { Trace.WriteLine("RasEnumConnections failed: Error = " +nRet.ToString());
} else { }
}
}
}The following link may provide some hints:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Bas
ic/Q_10248746.html
http://www.dotnet247.com/247reference/msgs/4/22651.aspx
===================
The following code works on my side:
using System;using System.Runtime.InteropServices; namespace RAS_Application
{ [StructLayout(LayoutKind.Sequential, Pack=1)] public struct RASCONN { public int dwSize; public IntPtr hrasconn; public string szEntryName;
} public class RAS
{
[DllImport("Rasapi32.dll", EntryPoint="RasEnumConnectionsA",
SetLastError=true)] public static extern int API_RasEnumConnections ( ref RASCONN lprasconn, // buffer to receive connections data ref int lpcb, // size in bytes of buffer ref int lpcConnections // number of connections written to buffer );
}
class Test
{
[STAThread]
static void Main(string[] args)
{ RASCONN lpRasConn =new RASCONN(); int lpcb =0; int lpcConnections =0; lpRasConn.dwSize = 412; lpcb = lpRasConn.dwSize; int nRet = RAS.API_RasEnumConnections(ref lpRasConn, ref lpcb, ref
lpcConnections); if(nRet != 0) { Trace.WriteLine("RasEnumConnections failed: Error = " +nRet.ToString());
} else { }
}
}
}The following link may provide some hints:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Bas
ic/Q_10248746.html
http://www.dotnet247.com/247reference/msgs/4/22651.aspx
帮助!!!!!!!!!!!!!!!!!!!!!!!!!
斑竹你们在吗?俺是没办法了.呵呵!
using System.Diagnostics;
using System.Runtime.InteropServices;namespace IsOnline
{
[StructLayout(LayoutKind.Sequential, Pack=1)] public struct RASCONN
{
public int dwSize;
public IntPtr hrasconn;
public string szEntryName;
} public class RAS
{
[DllImport("Rasapi32.dll",
EntryPoint="RasEnumConnectionsA",
SetLastError=true)
]
public static extern int API_RasEnumConnections
(
ref RASCONN lprasconn, // buffer to receive
// connections data
ref int lpcb, // size in bytes of buffer
ref int lpcConnections // number of connections
// written to buffer
);
} class Test
{
[STAThread]
static void Main(string[] args)
{
RASCONN lpRasConn =new RASCONN();
int lpcb =0;
int lpcConnections =0;
lpRasConn.dwSize = 412;
lpcb = lpRasConn.dwSize;
int nRet = RAS.API_RasEnumConnections(ref lpRasConn, ref lpcb, ref
lpcConnections);
if(nRet != 0)
{
Trace.WriteLine("RasEnumConnections failed: Error = " +nRet.ToString());
}
else
{
Trace.WriteLine("RasEnumConnections succeeded: Return = " +nRet.ToString());
}
}
}
}
int nRet = RAS.API_RasEnumConnections(ref lpRasConn, ref lpcb, ref
lpcConnections);2.这个API再不同的平台上会一些微小的差异,比如:
Windows Me/98/95: RasEnumConnections enumerates a connection as Active as soon as it starts dialing.Windows Server 2003/Windows XP/2000/NT: The connection cannot be enumerated as Active until RAS has successfully connected.关键是你抓抓到底返回了一个什么错误.
ADSL断线也是0,连线还是0。就是该函数更本就没工作。要不就是该函数更本不能用于测试ADSL这样的网络连接!这我真不知道该如何抓错呀!
我调用该函数更本就没更改任何内容。也就是说不可能是代码错误!只能是设置上的错误!
'利用RasEnumConnections函数,就可以得到所需拨号网络连接的句柄
'---------------------------------------------------------------
Private Declare Function RasEnumConnections Lib "RasApi32.DLL" Alias "RasEnumConnectionsA" (lprasconn As Any, lpcb As Long, lpcConnections As Long) As Long
Const RAS95_MaxEntryName = 256
Const RAS95_MaxDeviceName = 128
Const RAS_MaxDeviceType = 16
Private Type RASCONN95
dwSize As Long '设置 dWsize 值为 412
hRasConn As Long
szEntryName(RAS95_MaxEntryName) As Byte
szDeviceType(RAS_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End Type'------------------------------------------------------------------
'函数:用拨号网络的函数RasEnumConnections 检测是否正在连上Internet
'------------------------------------------------------------------
Private Function Fun_TestOnline() As Boolean
Dim lngRetCode As Long, lpcb As Long, lpcConnections As Long
Dim intArraySize As Integer, intLooper As Integer
ReDim lprasconn95(intArraySize) As RASCONN95 Fun_TestOnline = False
lprasconn95(0).dwSize = 412
lpcb = 256 * lprasconn95(0).dwSize
lngRetCode = RasEnumConnections(lprasconn95(0), lpcb, lpcConnections) If lngRetCode = 0 Then '检测是否正在连上Internet
If lpcConnections > 0 Then
Fun_TestOnline = True '已经连上网
Else
Fun_TestOnline = False '没有连上网
End If
End If
End FunctionPrivate Sub Command1_Click()
If Fun_TestOnline = True Then
MsgBox "你已经上了网"
Else
MsgBox "你还没有上网"
End If
End Sub
看了断VB代码看得不是太清楚!不懂VB呵呵!好像他可以检测是否连接网络呀!并支持ADSL
可以的话帮助翻译一下!谢谢!(加100分答谢各位!)
RASCONN lpRasConn =new RASCONN();
int lpcb =0;
int lpcConnections =0;
lpRasConn.dwSize = 412;
lpcb = lpRasConn.dwSize;
int nRet = RAS.API_RasEnumConnections(ref lpRasConn, ref lpcb, ref lpcConnections); if(nRet != 0) { Console.WriteLine("RasEnumConnections failed: Error = " +nRet.ToString()); }
else
{
for (int i = 0; i < lpcConnections; i++) { Console.WriteLine( lpcConnections.ToString()); } }
假如连接网络的话lpcConnections.ToString()这个应该不为0呀!可函数调用是成功了。这
lpcConnections在连接网络时也是0断开网络时还是0:(((((((((
修改这个:
lpRasConn.dwSize = System.Runtime.InteropServices.Marshal.SizeOf(lpRasConn);
假如这样的话就会报错呀!返回值是632我查了半天也不知是什么错误:(
我在2000高级服务器版下调用此API:)
另外我把分加到200吧!呵呵!谢谢各位兄弟参与!
请多进来UP一下!
RAS API是用于访问Dial-up Network的,而DSL根本不属于Dial-up Network(虽然可能会有个象模象样的拨号对话框)。DSL和LAN、Cable同属于一类。如果目的是测试当前是否有网络连接,那么建议使用下面两个API:
Bool IsNetworkAlive(
LPDWORD lpdwFlags // Specifies the type of network connection
);BOOL IsDestinationReachable(
LPCSTR lpszDestination, // Pointer to string specifying destination
LPQOCINFO lpQOCInfo // Pointer to Quality of Connection information
);
用Bool IsNetworkAlive(
LPDWORD lpdwFlags // Specifies the type of network connection
);
的话,当ADSL断线后5分钟它才能诊断出断线了,这也太夸张了点吧!:(
用BOOL IsDestinationReachable(
LPCSTR lpszDestination, // Pointer to string specifying destination
LPQOCINFO lpQOCInfo // Pointer to Quality of Connection information
);
的话不是太稳定!有时可以诊测到连接!有时诊测不到连接!
现在只能用
[DllImport("wininet.dll", CharSet=CharSet.Auto)]
public static extern bool InternetCheckConnection(string lpszUrl,[MarshalAs(UnmanagedType.U4)] int dwFlags,[MarshalAs(UnmanagedType.U4)] int dwReserved);这个API还相对稳定点!55555555555555555
各位兄弟还有什么更好的办法吗????
搞半天!可以说还是没有完全搞定!呵呵!
用PING假如服务器太繁忙的话返回值就会说没有连接网络!
难道有没有更好的办法了吗?
用Bool IsNetworkAlive(
LPDWORD lpdwFlags // Specifies the type of network connection
);
检测与LAN的通断,拔掉网线后约3-5分钟内仍为alive,不知有何办法?
http://expert.csdn.net/Expert/topic/1465/1465117.xml?temp=.5884668
InternetGetConnectedState()
DWORD WINAPI Offline(
RESID ResourceId
);这两个API断线后它还是返回TRUE呀!还不如用PING呢!呵呵!
1. 可以用WMI (Win2K & XP):
with Windows 2000 & XP, UNDOCUMENTED:
MSNdis_MediaConnectStatus - NdisMediaConnectStatus :
(0 = connected?, 1 = not connected?)with Windows XP:
Win32_NetworkAdapter - NetConnectionStatus :
using System.Management;static void ReportConnection2000()
{
ManagementClass mc = new ManagementClass( @"root\WMI", @"MSNdis_MediaConnectStatus", null );
ManagementObjectCollection moc = mc.GetInstances();
foreach( ManagementObject mo in moc )
{
string name = (string) mo["InstanceName"];
bool active = (bool) mo["Active"];
uint status = (uint) mo["NdisMediaConnectStatus"];
Console.WriteLine( " {0}\n\tActive:{1} Media Status:{2}", name, active, status );
}
}static void ReportConnectionXP()
{
ManagementClass mc = new ManagementClass( @"Win32_NetworkAdapter" );
ManagementObjectCollection moc = mc.GetInstances();
foreach( ManagementObject mo in moc )
{
string name = (string) mo["Name"];
object val = mo["NetConnectionStatus"];
if( val != null )
Console.WriteLine( " {0}\n\tConnection Status:{1}", name, (ushort) val );
else
Console.WriteLine( " {0}\n", name );
}
}
http://support.microsoft.com/?id=321381
或者:
NotifyAddrChange
NotifyRouteChange