Advertisement
2002ASP Internet/ HTML #11

Detect if there is a Dial up network connection

Here is how I detect if there is a DUN (ISP) connection. You want to take a look at the Remote Access Services (RAS) APIs. They are fully documented at the Microsoft site. "J Gerard Olszowiec" entity@ns.sympatico.ca

AI

AI Samenvatting: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Broncode
original-source
Public Function Connected_To_ISP() As Boolean
  
Dim hKey As Long
Dim lpSubKey As String
Dim phkResult As Long
Dim lpValueName As String
Dim lpReserved As Long
Dim lpType As Long
Dim lpData As Long
Dim lpcbData As Long
  Connected_To_ISP = False
  
  lpSubKey = "System\CurrentControlSet\Services\RemoteAccess"
  ReturnCode = RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, phkResult)
  
  If ReturnCode = ERROR_SUCCESS Then
    hKey = phkResult
    lpValueName = "Remote Connection"
    lpReserved = APINULL
    lpType = APINULL
    lpData = APINULL
    lpcbData = APINULL
    ReturnCode = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType,
ByVal lpData, lpcbData)
    
    lpcbData = Len(lpData)
    ReturnCode = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType,
lpData, lpcbData)
    If ReturnCode = ERROR_SUCCESS Then
      If lpData = 0 Then
        ' Not Connected
      Else
        ' Connected
        Connected_To_ISP = True
      End If
    End If
    RegCloseKey (hKey)
  End If
End Function
> 2) Once I determine that I'd like to disconnect, How do I do 
> that? It seems like I need some interface to DUN to do it.
Use RasHangUp. In this example I display a splash screen (frmHangupSplash)
while the hangup is in progress. You'll want to set gstrISPName =
Get_ISP_Name() before calling HangUp(), or better yet modify HangUP and
pass the DUN connection name (the ISP) as a parameter..
Public Sub HangUp()
Dim i As Long
Dim lpRasConn(255) As RasConn
Dim lpcb As Long
Dim lpcConnections As Long
Dim hRasConn As Long
  
  frmHangupSplash.Show
  frmHangupSplash.Refresh
  
  lpRasConn(0).dwSize = RAS_RASCONNSIZE
  lpcb = RAS_MAXENTRYNAME * lpRasConn(0).dwSize
  lpcConnections = 0
  
  ReturnCode = RasEnumConnections(lpRasConn(0), lpcb, lpcConnections)
  ' Drop ALL the connections that match the currect
  ' connections name.
  
  If ReturnCode = ERROR_SUCCESS Then
    For i = 0 To lpcConnections - 1
      If Trim(ByteToString(lpRasConn(i).szEntryName)) =
Trim(gstrISPName) Then
        hRasConn = lpRasConn(i).hRasConn
        ReturnCode = RasHangUp(ByVal hRasConn)
      End If
    Next i
  End If
  
  ' It takes about 3 seconds to drop the connection.
  
  Wait (3)
  
  While Connected_To_ISP
    Wait (1)
  Wend
  
  Unload frmHangupSplash
  
End Sub

Public Sub Wait(sngSeconds As Single)
Dim sngEndTime As Single
  sngEndTime = Timer + sngSeconds
  
  While Timer < sngEndTime
    DoEvents
  Wend
  
End Sub

Public Function Get_ISP_Name() As String
Dim hKey As Long
Dim lpSubKey As String
Dim phkResult As Long
Dim lpValueName As String
Dim lpReserved As Long
Dim lpType As Long
Dim lpData As String
Dim lpcbData As Long
  Get_ISP_Name = ""
  
  If gblnConnectedToISP Then
    lpSubKey = "RemoteAccess"
    ReturnCode = RegOpenKey(HKEY_CURRENT_USER, lpSubKey, phkResult)
    If ReturnCode = ERROR_SUCCESS Then
      hKey = phkResult
      lpValueName = "Default"
      lpReserved = APINULL
      lpType = APINULL
      lpData = APINULL
      lpcbData = APINULL
      ReturnCode = RegQueryValueEx(hKey, lpValueName, lpReserved,
lpType, ByVal lpData, lpcbData)
      
      lpData = String(lpcbData, 0)
      ReturnCode = RegQueryValueEx(hKey, lpValueName, lpReserved,
lpType, ByVal lpData, lpcbData)
    
      If ReturnCode = ERROR_SUCCESS Then
        ' Chop off the end-of-string character.
        Get_ISP_Name = Left(lpData, lpcbData - 1)
      End If
      RegCloseKey (hKey)
    End If
  End If
End Function
'***************************************************************************
' Name: ByteToString
'     ' Description:* * * THIS IS A FOLLOWUP SUBMISSION * * *
Purpose: Convert a string in byte format (usually from a DLL call) to a string of text.
PLEASE POST THIS AS A FOLLOWUP OR ADD TO THE CODE SAMPLE TITLED "Detect if there is a Dial up network connection" attributed to me J Gerard Olszowiec, entity@ns.sympatico.ca. The newsgroup post that you captured had a followup post that included the ByteToString code. I've been receiving requets for this functions code. Much Thanx. - Gerard

' By: Entity Software
'
' Inputs:None
' Returns:None
' Assumes:None
' Side Effects:None
'
'Code provided by Planet Source Code(tm) 'as is', without
'     warranties as to performance, fitness, merchantability,
'     and any other warranty (whether expressed or implied).
'***************************************************************************


Public Function ByteToString(bytString() As Byte) As String

       '     ' Convert a string in byte format (usually from a DLL call)
       '     ' to a string of text.
       Dim i As Integer
       ByteToString = ""
       i = 0

              While bytString(i)  0&
                     ByteToString = ByteToString & Chr(bytString(i))
                     i = i + 1
              Wend

End Function


#include <iostream.h>
class Person
{
	public:
		void SetAge(int Age); // Set the age
		void SetName(char* chrName); // Set the name
		char* GetName(); // get the name
		
		int GetAge(); // get the age
		
		void Greeting1();// Say "Hello!"
		void SayName(); // Say "My name is <My Name>"
		void Goodbye(); // Say "Good bye!
		void SayAge(); // Say " I am <X> years old!"
	private:
		char* pName;
		unsigned int pAge;
};
void Person::SetAge(int Age)
{
	pAge = Age;
}
void Person::SetName(char* chrName)
{
	pName = chrName;
}
char* Person::GetName()
{
	return pName;
}
int Person::GetAge()
{
	return pAge;
}
void Person::Greeting1()
{
	cout << "Hello!\n";
}
void Person::SayAge()
{
	// Check Person age
	if (pAge < 2)
			// Person is 1 or less years old
			cout << "I am a baby!\n";
	else
			// Person is over 1 years old
			cout << "I am " << pAge << " years old!\n";

}
void Person::SayName()
{
	cout << "My name is " << pName << "!\n";
}
void Person::Goodbye()
{
	cout << "Good-bye!!\n";
}
int main()
{
	// Define our perosn
	Person Gabe;
	// Set up Gabe's age
	Gabe.SetAge(13);
	// Give Gabe's full name
	Gabe.SetName("Gabriel Halland");
	
	// Make Gabe greet the user
	Gabe.Greeting1();
	// Make Gabe introduce himself
	Gabe.SayName();
	// Make Gabe tell us how old he is
	Gabe.SayAge();
	// Make Gabe dismiss himself
	Gabe.Goodbye();
	// end out
	return 0;
}
Originele reacties (3)
Hersteld van de Wayback Machine