Solved

RasGetConnectStatus API

Posted on 2000-04-21
5
947 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now