public void RasGetConnectStatusAsExpected() { var hRasConn = new IntPtr(1); var lpRasConnStatus = new RASCONNSTATUS(); api.Setup(o => o.RasGetConnectStatus(hRasConn, ref It.Ref <RASCONNSTATUS> .IsAny)).Returns(SUCCESS); eventLoggingPolicy.Setup(o => o.LogEvent(It.IsAny <EventLevel>(), It.IsAny <PInvokeInt32CallCompletedTraceEvent>())).Callback(new LogEventCallback( (level, o1) => { Assert.AreEqual(EventLevel.Verbose, level); var eventData = (PInvokeInt32CallCompletedTraceEvent)o1; Assert.True(eventData.Args.ContainsKey(nameof(hRasConn))); Assert.True(eventData.OutArgs.ContainsKey(nameof(lpRasConnStatus))); Assert.True(eventData.Duration > TimeSpan.Zero); Assert.AreEqual(SUCCESS, eventData.Result); })).Verifiable(); var target = new RasApi32LoggingAdvice(api.Object, eventLoggingPolicy.Object); var result = target.RasGetConnectStatus(hRasConn, ref lpRasConnStatus); eventLoggingPolicy.Verify(); Assert.AreEqual(SUCCESS, result); }
public bool IsConnected() { RASCONN[] rasStructs = GetRasConnections(); RASCONNSTATUS rasConnStatus = new RASCONNSTATUS(); rasConnStatus.dwSize = Marshal.SizeOf(typeof(RASCONNSTATUS)); for (int i = 0; i < rasConnectionsAmount; ++i) { // Pobranie pojedynczej struktury RASCONN rStruct = rasStructs[i]; int statusResult = RasGetConnectStatus(rStruct.hrasconn, ref rasConnStatus); if (statusResult != 0) { throw new Win32Exception(statusResult); } if (rasConnStatus.rasconnstate == RAS_Connected) { return(true); } } return(false); }
public int GetRasStatus() { RASCONNSTATUS RASConnStatus = new RASCONNSTATUS(); RASConnStatus.dwSize = Marshal.SizeOf(typeof(RASCONNSTATUS)); uint lResult = RasGetConnectStatus(rasSession, ref RASConnStatus); return((int)lResult); }
/// <summary> /// 计时器事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TimerEvent(object sender, ElapsedEventArgs e) { RASCONNSTATUS structure = new RASCONNSTATUS(); int lpcb = 0; int lpcConnections = 0; structure.dwSize = Marshal.SizeOf(structure); int nErrorValue = RasEnumConnections(this.Rasconn, ref lpcb, ref lpcConnections); switch (nErrorValue) { case 0: break; case 0x25b: this.Rasconn = new RASCONN[lpcConnections]; lpcb = this.Rasconn[0].dwSize = Marshal.SizeOf(this.Rasconn[0]); nErrorValue = RasEnumConnections(this.Rasconn, ref lpcb, ref lpcConnections); break; default: this.ConnectNotify(this.GetErrorString(nErrorValue), 3); return; } if (nErrorValue != 0) { this.ConnectNotify(this.GetErrorString(nErrorValue), 3); } else if ((lpcConnections < 1) && this.bConnected) { this.bConnected = false; this.ConnectNotify("连接中断.", 0); } else { for (int i = 0; i < lpcConnections; i++) { // nErrorValue = RasGetConnectStatus(this.Rasconn.hrasconn, ref structure); if (nErrorValue != 0) { this.ConnectNotify(this.GetErrorString(nErrorValue), 3); return; } if ((structure.rasconnstate == RASCONNSTATE.RASCS_Connected) && !this.bConnected) { this.bConnected = true; // this.ConnectNotify("成功连接到" + this.Rasconn.szEntryName + '.', 2); } if ((structure.rasconnstate == RASCONNSTATE.RASCS_Disconnected) && this.bConnected) { this.bConnected = false; this.ConnectNotify("连接中断.", 0); } } } }
public static string GetIP(string strEntryName) { RASCONN[] Rasconn = new RASCONN[1]; Rasconn[0].dwSize = Marshal.SizeOf(Rasconn[0]); RASCONNSTATUS structure = new RASCONNSTATUS(); int lpcb = 0; int lpcConnections = 0; structure.dwSize = Marshal.SizeOf(structure); int nErrorValue = RasEnumConnections(Rasconn, ref lpcb, ref lpcConnections); switch (nErrorValue) { case 0: break; case 0x25b: Rasconn = new RASCONN[lpcConnections]; lpcb = Rasconn[0].dwSize = Marshal.SizeOf(Rasconn[0]); nErrorValue = RasEnumConnections(Rasconn, ref lpcb, ref lpcConnections); break; //default: //ConnectNotify(this.GetErrorString(nErrorValue), 3); //return; } int lphrsaConn = 0; foreach (RASCONN rasconn in Rasconn.Where(rasconn => rasconn.szEntryName == strEntryName)) { if (rasconn.hrasconn != 0) { lphrsaConn = rasconn.hrasconn; } } if (lphrsaConn == 0) { return(""); } RASPPPIP structProjection = new RASPPPIP(); uint uiProjSize = (uint)structProjection.dwSize; try { uint errorCode = RasGetProjectionInfo(lphrsaConn, RASPROJECTION.RASP_PppIp, structProjection, ref uiProjSize); return(structProjection.szIpAddress); } catch { return(""); } }
/* * m_connected = true; * * RAS lpras = new RAS(); * * RASCONN lprasConn = new RASCONN(); * * lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN)); * lprasConn.hrasconn = IntPtr.Zero; * * int lpcb = 0; * int lpcConnections = 0; * int nRet = 0; * lpcb = Marshal.SizeOf(typeof(RASCONN)); * * * nRet = RAS.RasEnumConnections(ref lprasConn, ref lpcb, ref lpcConnections); * * * if (nRet != 0) * { * m_connected = false; * return; * * } * * if (lpcConnections > 0) * { * * * //for (int i = 0; i < lpcConnections; i++) * * //{ * RasStats stats = new RasStats(); * * m_ConnectedRasHandle = lprasConn.hrasconn; * RAS.RasGetConnectionStatistics(lprasConn.hrasconn, stats); * * * m_ConnectionName = lprasConn.szEntryName; * * int Hours = 0; * int Minutes = 0; * int Seconds = 0; * * Hours = ((stats.dwConnectionDuration / 1000) / 3600); * Minutes = ((stats.dwConnectionDuration / 1000) / 60) - (Hours * 60); * Seconds = ((stats.dwConnectionDuration / 1000)) - (Minutes * 60) - (Hours * 3600); * * * m_duration = Hours + " hours " + Minutes + " minutes " + Seconds + " secs"; * m_TX = stats.dwBytesXmited; * m_RX = stats.dwBytesRcved; * * * //} * * * } * else * { * m_connected = false; * * } */ public bool HangUp(string strEntryName, out string strError) { strError = ""; RASCONN[] Rasconn = new RASCONN[1]; Rasconn[0].dwSize = Marshal.SizeOf(Rasconn[0]); RASCONNSTATUS structure = new RASCONNSTATUS(); int lpcb = 0; int lpcConnections = 0; structure.dwSize = Marshal.SizeOf(structure); lpcb = 0x500; int nErrorValue = RasEnumConnections(Rasconn, ref lpcb, ref lpcConnections); switch (nErrorValue) { case 0: break; case 0x25b: Rasconn = new RASCONN[lpcConnections]; lpcb = Rasconn[0].dwSize = Marshal.SizeOf(Rasconn[0]); nErrorValue = RasEnumConnections(Rasconn, ref lpcb, ref lpcConnections); break; //default: //ConnectNotify(this.GetErrorString(nErrorValue), 3); //return; } foreach (RASCONN rasconn in Rasconn.Where(rasconn => rasconn.szEntryName == strEntryName)) { if (rasconn.hrasconn != 0) { int num2 = RasHangUp(rasconn.hrasconn); if (num2 != 0) { //strError = this.GetErrorString(num2); //this.ConnectNotify(strError, 0); return(false); } } } strError = null; return(true); }
public int RasGetConnectStatus(IntPtr hRasConn, ref RASCONNSTATUS lpRasConnStatus) { var stopwatch = Stopwatch.StartNew(); var result = AttachedObject.RasGetConnectStatus(hRasConn, ref lpRasConnStatus); stopwatch.Stop(); var callEvent = new PInvokeInt32CallCompletedTraceEvent { DllName = RasApi32Dll, Duration = stopwatch.Elapsed, MethodName = nameof(RasGetConnectStatus), Result = result, }; callEvent.Args.Add(nameof(hRasConn), hRasConn); callEvent.OutArgs.Add(nameof(lpRasConnStatus), lpRasConnStatus); LogVerbose(callEvent); return(result); }
private RasDevice CreateDevice(RASCONNSTATUS rasConnStatus) { return(deviceTypeFactory.Create(rasConnStatus.szDeviceName, rasConnStatus.szDeviceType)); }
internal static string GetCurrentConnectoid() { uint dwSize = (uint) Marshal.SizeOf(typeof(RASCONN)); GlobalLog.Print("RasHelper::GetCurrentConnectoid() using struct size dwSize:" + dwSize); if (!s_RasSupported) { // if RAS is not supported, behave as if no dial-up/VPN connection is in use // (which is actually the case, since without RAS dial-up/VPN doesn't work) return null; } uint count = 4; uint statusCode = 0; RASCONN[] connections = null; while (true) { uint cb = checked(dwSize * count); connections = new RASCONN[count]; connections[0].dwSize = dwSize; statusCode = RasEnumConnections(connections, ref cb, ref count); GlobalLog.Print("RasHelper::GetCurrentConnectoid() called RasEnumConnections() count:" + count + " statusCode: " + statusCode + " cb:" + cb); if (statusCode != ERROR_BUFFER_TOO_SMALL) { break; } count = checked(cb + dwSize - 1) / dwSize; } if (count == 0 || statusCode != 0) { return null; } for (uint i=0; i < count; i++) { GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "]"); GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].dwSize: " + connections[i].dwSize); GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].hrasconn: " + connections[i].hrasconn); GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].szEntryName: " + connections[i].szEntryName); GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].szDeviceType: " + connections[i].szDeviceType); GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].szDeviceName: " + connections[i].szDeviceName); RASCONNSTATUS connectionStatus = new RASCONNSTATUS(); connectionStatus.dwSize = (uint)Marshal.SizeOf(connectionStatus); // RELIABILITY: // the 'hrasconn' field is an IntPtr because it's defined as a handle // that said, its use is that of a opaque ID, so we're not // allocating anything that needs to be released for reliability. statusCode = RasGetConnectStatus(connections[i].hrasconn, ref connectionStatus); GlobalLog.Print("RasHelper::GetCurrentConnectoid() called RasGetConnectStatus() statusCode: " + statusCode + " dwSize: " + connectionStatus.dwSize); if (statusCode==0) { GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.dwSize: " + connectionStatus.dwSize); GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.rasconnstate: " + connectionStatus.rasconnstate); GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.dwError: " + connectionStatus.dwError); GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.szDeviceType: " + connectionStatus.szDeviceType); GlobalLog.Print("RasHelper::GetCurrentConnectoid() RASCONN[" + i + "].RASCONNSTATUS.szDeviceName: " + connectionStatus.szDeviceName); if (connectionStatus.rasconnstate==RASCONNSTATE.RASCS_Connected) { return connections[i].szEntryName; } } } return null; }
public int RasGetConnectStatus(IntPtr hRasConn, ref RASCONNSTATUS lpRasConnStatus) { return(SafeNativeMethods.RasGetConnectStatus(hRasConn, ref lpRasConnStatus)); }
private static extern int RasGetConnectStatus(int hrasconn, ref RASCONNSTATUS lprasconnstatus);
private static extern int RasGetConnectStatus(int hrasconn, ref RASCONNSTATUS lprasconnstatus);
internal static string GetCurrentConnectoid() { uint num = (uint) Marshal.SizeOf(typeof(RASCONN)); if (s_RasSupported) { uint lpcConnections = 4; uint num3 = 0; RASCONN[] lprasconn = null; while (true) { uint lpcb = num * lpcConnections; lprasconn = new RASCONN[lpcConnections]; lprasconn[0].dwSize = num; num3 = RasEnumConnections(lprasconn, ref lpcb, ref lpcConnections); if (num3 != 0x25b) { break; } lpcConnections = ((lpcb + num) - 1) / num; } if ((lpcConnections != 0) && (num3 == 0)) { for (uint i = 0; i < lpcConnections; i++) { RASCONNSTATUS rasconnstatus; rasconnstatus = new RASCONNSTATUS { dwSize = (uint) Marshal.SizeOf(rasconnstatus) }; if ((RasGetConnectStatus(lprasconn[i].hrasconn, ref rasconnstatus) == 0) && (rasconnstatus.rasconnstate == RASCONNSTATE.RASCS_Connected)) { return lprasconn[i].szEntryName; } } } } return null; }
public static extern int RasGetConnectStatus(IntPtr hrasconn, [In, Out] RASCONNSTATUS lprasconnstatus);
private IPAddress CreateLocalIPAddress(RASCONNSTATUS rasConnStatus) { return(ipAddressConverter.ConvertFromEndpoint(rasConnStatus.localEndpoint)); }
private IPAddress CreateRemoteIPAddress(RASCONNSTATUS rasConnStatus) { return(ipAddressConverter.ConvertFromEndpoint(rasConnStatus.remoteEndpoint)); }
static extern int RasGetConnectStatus( IntPtr hrasconn, ref RASCONNSTATUS lprasconnstatus);
/// <summary> /// 计时器事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TimerEvent(object sender, ElapsedEventArgs e) { RASCONNSTATUS structure = new RASCONNSTATUS(); int lpcb = 0; int lpcConnections = 0; structure.dwSize = Marshal.SizeOf(structure); int nErrorValue = RasEnumConnections(this.Rasconn, ref lpcb, ref lpcConnections); switch (nErrorValue) { case 0: break; case 0x25b: this.Rasconn = new RASCONN[lpcConnections]; lpcb = this.Rasconn[0].dwSize = Marshal.SizeOf(this.Rasconn[0]); nErrorValue = RasEnumConnections(this.Rasconn, ref lpcb, ref lpcConnections); break; default: this.ConnectNotify(this.GetErrorString(nErrorValue), 3); return; } if (nErrorValue != 0) { this.ConnectNotify(this.GetErrorString(nErrorValue), 3); } else if ((lpcConnections < 1) && this.bConnected) { this.bConnected = false; this.ConnectNotify("连接中断.", 0); } else { for (int i = 0; i < lpcConnections; i++) { nErrorValue = RasGetConnectStatus(this.Rasconn[i].hrasconn, ref structure); if (nErrorValue != 0) { this.ConnectNotify(this.GetErrorString(nErrorValue), 3); return; } if ((structure.rasconnstate == RASCONNSTATE.RASCS_Connected) && !this.bConnected) { this.bConnected = true; this.ConnectNotify("成功连接到" + this.Rasconn[i].szEntryName + '.', 2); } if ((structure.rasconnstate == RASCONNSTATE.RASCS_Disconnected) && this.bConnected) { this.bConnected = false; this.ConnectNotify("连接中断.", 0); } } } }
static void Main(string[] args) { //make sure that RAS is installed int flags = 0; InternetGetConnectedState(ref flags, 0); if (!((flags & INTERNET_RAS_INSTALLED) == INTERNET_RAS_INSTALLED)) { throw new NotSupportedException(); } //create array of structures to pass to API int ret; int conns = 0; RASCONN[] rarr = new RASCONN[256]; rarr.Initialize(); rarr[0].dwSize = Marshal.SizeOf(typeof(RASCONN)); int lr = rarr[0].dwSize * rarr.Length; //call RasEnumConnections to loop all RAS connections ret = RasEnumConnections(rarr, ref lr, ref conns); if (ret != 0) { throw new Win32Exception(ret); } //loop through each RASCONN struct for (int i = 0; i < conns; i++) { //retrieve RASCONN struct RASCONN r = rarr[i]; //if connection bad, handle will be 0 if (r.hrasconn == IntPtr.Zero) { continue; } //get status of RAS connection RASCONNSTATUS rcs = new RASCONNSTATUS(); rcs.dwSize = Marshal.SizeOf(typeof(RASCONNSTATUS)); ret = RasGetConnectStatus(r.hrasconn, ref rcs); if (ret != 0) { throw new Win32Exception(ret); } //print useful information to the console for each RAS connection Console.WriteLine("RAS CONNECTION {0}:", i + 1); Console.WriteLine("\tDevice Name : {0}", r.szDeviceName); Console.WriteLine("\tDevice Type : {0}", r.szDeviceType); Console.WriteLine("\tPhone Number: {0}", rcs.szPhoneNumber); Console.WriteLine("\tConnected? : {0}", rcs.rasconnstate == RAS_Connected); Console.WriteLine("\tEntry Name : {0}", r.szEntryName); Console.WriteLine("\tPhone Book : {0}", r.szPhonebook); Console.WriteLine(); } Console.ReadLine(); }
public static extern int RasGetConnectStatus( IntPtr hRasConn, [In, Out] ref RASCONNSTATUS lpRasConnStatus);
private static extern int RasGetConnectStatus(int hRasConn, ref RASCONNSTATUS lpRasConnStatus);