Click here to Skip to main content
Click here to Skip to main content

Calling RasHangUp without errors.

, 9 Jan 2000
Rate this:
Please Sign up or sign in to vote.
A way to call RasHangUp without hanging your applications or your modem
<!-- Article Starts -->


The RasHangUp function terminates a remote access connection. The connection is specified with a RAS connection handle. The function releases all RASAPI32.DLL resources associated with the handle. (MSDN)

Then, in remarks, it is mentioned that the application should sleep about 3 seconds, or until RasGetConnectStatus returns ERROR_INVALID_HANDLE. If you realy just call RasHangUp and exit you can "hang" both the modem and rnaapp (the application that implements Dial-Up Services). Furthermore, if you do everything as described in MSDN you may still receive the following error message:

RNAAPP caused an invalid page fault in module xxxx.


This problem was a besetting sin when I wrote my Dial-up dialer program, until I found a solution that works in my program:

DWORD dwRet;
ZeroMemory(&rStatus, sizeof(RASCONNSTATUS));
rStatus.dwSize = sizeof(RASCONNSTATUS);
dwRet = RasGetConnectStatus(hRasConn, &rStatus);
	RasHangUp(hRasConn);  // hRasConn - valid handle to the RAS connection
	MSG msg;
	while (dwRet != ERROR_INVALID_HANDLE)
		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		dwRet = RasGetConnectStatus(hRasConn, &rStatus);

Note: I removed some unimportant stuff so check this code before using...


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alexander Fedorov
Web Developer SEO
Russian Federation Russian Federation
AlexF's Blog in Russian
Owner Spy competition analysis
Rating Burner Rating of blogs

Comments and Discussions

GeneralWindows's dialer list Pinmemberphi-161816-Oct-07 22:49 
GeneralI've some question... PinsussLee Joung Soo22-May-05 10:01 
QuestionHow to use RasDial() WITHOUT phonebook entry? Pinmembersoniko5-Oct-03 5:10 
AnswerRe: How to use RasDial() WITHOUT phonebook entry? PinmemberRamkrishna Pawar14-May-04 4:29 
GeneralTelephone number PinmemberAnonymous5-Jun-01 2:30 
GeneralRe: Telephone number - and - Does anyone know how to get the user and password ? PinmemberRichard Broadhurst12-Sep-01 23:57 
GeneralRe: Telephone number - and - Does anyone know how to get the user and password ? PinmemberHackMan5-Dec-01 5:56 
QuestionThanks But ??? PinsussDavid Fields31-Mar-00 12:26 
Thank you for your submission.
There is just one problem. This only occurs under Win95/98
NT does not complain about this.
If you use RasDialFunc CALLBACK function under windows 95/98
you must call RasHangup not using this technique. in your RasDialFunc.
if you do not do this the program will hang.
Also, if you do automatic redial on non-Critical Errors, (dwError )
you have to be carefull on Redialing the number. if you do not do extra MessagePump and/or
checking the RASCONNSTATUS when it calls RasDial() it will report and error
Port is alread open
just before you call RasDial() this needs to be done on Windows95/98
NT does have this problem
if ( m_hRasConn != NULL )
// do some sort of MessagePump here
// and keep checking the RASCONNSTATUS



m_hRasConn = NULL

DWORD dwResult = (*m_pfnRasDial)( NULL, NULL, &rasDialParams, NULL, NULL, &m_hRasConn );
if ( dwResult != ERROR_SUCCESS )
// throw an exception for the error code dwResult


i.e) for the have to call RasHangup in RasDialFunc
void CALLBACK RasDialFunc( UINT, RASCONNSTATE rasconnstate, DWORD dwError )
........ other RasDialFunc stuff done here
if ( dwError != ERROR_SUCCESS )
Ras.Disconnect(true, 0 );
........ other RasDialFunc stuff done here

void MyRasClass::Disconnect( bool bForce /* = false*/, DWORD dwSleepMS /* = 3000 */ )
if ( bForce )
dwResult = (*m_pfnRasHangup)(m_hRasConn);
Sleep( dwSleepMS );
dwResult = SafeDisconnect(); // this method dwResult is the dwRet in your disconnect
if ( dwResult != ERROR_INVALID_HANDLE && dwResult != ERROR_SUCCESS )
// throw an Exception for the error code in dwResult

QuestionThanks But ??? PinsussDavid Fields31-Mar-00 12:25 
GeneralRE: PinsussMartijn Hoogendoorn6-Mar-00 11:44 
GeneralRe: RE: PinmemberAldamo3-May-02 20:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 10 Jan 2000
Article Copyright 2000 by Alexander Fedorov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid