Solved

RasGetConnectStatus API

Posted on 2000-04-21
5
988 Views
Last Modified: 2008-03-03
I need to be able to determine when a dial-up network connection has gone to completion, and see that the RasGetConnectStatus API does this. However, I desperately need someone to get this to work in VB6, preferably with a working example of the code. I've taken a look at the MSDN docs but translating from C++ examples to VB isn't my forte. Any help would be greatly appreciated, as well as example projects if possible. Thanks.
0
Comment
Question by:peterchamberlin
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Ruchi
ID: 2738040
From http://hjem.get2net.dk/vcoders/cm/tips/internet_checkconnect.htm


Put the following code in the module.

'Declare
Public Const RAS95_MaxEntryName = 256
Public Const RAS_MaxDeviceType = 16
Public Const RAS95_MaxDeviceName = 128
Public Const RASCS_DONE = &H2000&

Public Type RASCONN95
  'set dwsize to 412
  dwSize As Long
  hRasConn As Long
  szEntryName(RAS95_MaxEntryName) As Byte
  szDeviceType(RAS_MaxDeviceType) As Byte
  szDeviceName(RAS95_MaxDeviceName) As Byte
End Type

Public Type RASCONNSTATUS95
  'set dwsize to 160
  dwSize As Long
  RasConnState As Long
  dwError As Long
  szDeviceType(RAS_MaxDeviceType) As Byte
  szDeviceName(RAS95_MaxDeviceName) As Byte
End Type

Public Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lprasconn As Any, lpcb As Long, lpcConnections As Long) As Long
Public Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" (ByVal hRasConn As Long, lpRASCONNSTATUS As Any) As Long



Put the code in the form.
'Code
Public Function IsRASConnected() As Boolean
Dim TRasCon(255) As RASCONN95
Dim lg As Long
Dim lpcon As Long
Dim lReturn As Long
Dim Tstatus As RASCONNSTATUS95

  TRasCon(0).dwSize = 412
  lg = 256 * TRasCon(0).dwSize
  lReturn = RasEnumConnections(TRasCon(0), lg, lpcon)
 
  If lReturn <> 0 Then
    MsgBox "ERROR"
    Exit Function
  End If
 
  Tstatus.dwSize = 160
  lReturn = RasGetConnectStatus(TRasCon(0).hRasConn, Tstatus)
 
  If Tstatus.RasConnState = RASCS_DONE Then
    IsRASConnected = True
      Else
        IsRASConnected = False
  End If
 
End Function

0
 
LVL 44

Expert Comment

by:bruintje
ID: 2738053
Hi,

A sample can be downloaded here, it contains the RAS api calls in a sample project.....with source of course

http://www.coderoom.com/code/internet/file26.zip

HTH:O)Bruintje
0
 
LVL 9

Accepted Solution

by:
Ruchi earned 100 total points
ID: 2738070
From http://hjem.get2net.dk/vcoders/cm/tips/internet_checkconnect.htm 

'Add this code to a Module:
Option Explicit

Declare Function RasEnumConnections Lib "RasApi32.dll" Alias _
        "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, _
        lpcConnections As Long) As Long

Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias _
        "RasGetConnectStatusA" (ByVal hRasCon As Long, _
        lpStatus As Any) As Long

Public Const RAS95_MaxEntryName = 256
Public Const RAS95_MaxDeviceType = 16
Public Const RAS95_MaxDeviceName = 32

Public Type RASCONN95
   dwSize As Long
   hRasCon As Long
   szEntryName(RAS95_MaxEntryName) As Byte
   szDeviceType(RAS95_MaxDeviceType) As Byte
   szDeviceName(RAS95_MaxDeviceName) As Byte
End Type

Public Type RASCONNSTATUS95
   dwSize As Long
   RasConnState As Long
   dwError As Long
   szDeviceType(RAS95_MaxDeviceType) As Byte
   szDeviceName(RAS95_MaxDeviceName) As Byte
End Type

Public Function IsConnected() As Boolean

   Dim TRasCon(255) As RASCONN95
   Dim lg As Long
   Dim lpcon As Long
   Dim RetCDec As Long
   Dim Tstatus As RASCONNSTATUS95

   TRasCon(0).dwSize = 412
   lg = 256 * TRasCon(0).dwSize

   RetCDec = RasEnumConnections(TRasCon(0), lg, lpcon)
   Tstatus.dwSize = 160
   RetCDec = RasGetConnectStatus(TRasCon(0).hRasCon, Tstatus)

   If Tstatus.RasConnState = &H2000 Then
      IsConnected = True
   Else
      IsConnected = False
   End If

End Function


'From anywhere in code use this to check if there is an active connection:

Dim cActive As Boolean
cActive = IsConnected
'-- End --'
0
 

Author Comment

by:peterchamberlin
ID: 2738092
Thanks Ruchi, that's exactly what I was looking for. Thanks also bruinje. Hopefully the combined info will get everything sorted. I was just about to attempt to use the M$ example, but hopefully won't have to now :)
0
 

Author Comment

by:peterchamberlin
ID: 2743987
Hi again,

After trialing the code, it works fine in Win95 (as you'd expect), however it's been giving errors in Win98 and Win2000. I suspect this is probably to do with the structures. Is there a way in which a RAS Status can be determined to be at RACS_DONE in Win95 upwards? I've got myself some code to determine the Windows OS version (Win95=1 etc.) so that I can apply the working Win95 RAS stuff where necessary, and it may also come in useful if separate code is needed for different Win OS'.

Let me know if there's a way around this. I also noticed that some examples give &H2000 as RACS_Done, whereas others give &H2000&. Would this make a difference in Win98/2000?

Thanks.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question