The RasHangUp function is used to terminate a remote access connection.  It releases all rasapi32.dll resources associated with the connection.

You should call RasHangUp for any connection that you initiated with the RasDial function if the returned connection handle is non zero, even if the dial operation failed.

You can also use connection handles from the RasEnumConnections function.


The RasHangUp function looks like this:


Declare Function RasHangUp _
         Lib "rasapi32.dll" Alias "RasHangUpA" _
        (ByVal hrasconn As Long) As Long


Note:  The RasHangUp function returns immediately, however it may take some time for Ras to actually disconnect and release resources.  It's up to yourself as to how you decide to handle this, but you should wait for Ras to complete it's hangup before closing your app or calling the RasDial or RasHangUp again.  Eg:

Dim rtn as long, lngError as Long
  rtn = RasHangUp(hRasConn)
  'If rtn <> 0 Then Debug.Print VBRasErrorHandler(rtn)
   Sleep 0&
   lngError = VBRasGetConnectStatus(hRasConn, myConnStatus)
  Loop While lngError <> ERROR_INVALID_HANDLE


The above code will loop until the the handle becomes invalid meaning that the connection has been disconnected.   For the sample code above you will need these declarations:

Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

If you are using asynchronous dialling you can call the RasHangUp function at any time even before the connection is established.  If your RasDailFunc1 callback procedure is still in scope (connection has not been established or disconnected yet) then you should NOT use a loop as shown above as this would block your RasDialFunc1 procedure, instead you should monitor the rasConnectionState parameter of your RasDailFunc1 function for it to be equal to  RASCS_disconnected.


See Also: Contents, IntroductionRasDial, RasEnumConnections, RasGetConnectStatus, RASCONNSTATE, RasErrorHandler