1

I created a DialUp connection using the RASDIAL or RASPHONE exe. I want my application to monitor the established connection and if the connection gets disconnected my application should exit. How to manage this programatically? Please suggest me..

2vision2
  • 4,933
  • 16
  • 83
  • 164

3 Answers3

2

I suggest you use a thread to periodically check the status of the connection.

This can be done using RasEnumConnections from rasapi32.dll. This call returns the list of active connections, which you can then iterate over to check whether the one you opened is among them.

Andreas
  • 1,751
  • 2
  • 14
  • 25
  • Thanks for your suggestion Andreas. +1. Any other possible solutions are there for this than creating a thread? which is more efficient like registerdevicenotification which notifies the windows on device removal? Any APIS does the job for RASDIAL. – 2vision2 Jul 24 '12 at 06:04
1

The correct Win32 API for receiving event based notifications regarding RAS connections and disconnections is RasConnectionNotification.

Here's a link: http://msdn.microsoft.com/en-us/library/aa376726(VS.85).aspx

If you're using the DotRas SDK on CodePlex the RasConnectionWatcher handles these notifications using the above mentioned API.

using DotRas;

RasConnectionWatcher watcher = new RasConnectionWatcher();
watcher.Connected += (sender, e) => 
    { 
        // Connected! 
    };
watcher.Disconnected += (sender, e) => 
    { 
        // Disconnected! 
    };

You can also specify a handle returned from RasDial to receive events specific to that handle by setting the Handle property.

watcher.Handle = handle;
watcher.EnableRaisingEvents = true;

Here's a link to DotRas if you're interested: http://dotras.codeplex.com

Edit: I just wanted to add, if you do not specify a handle to the either the DotRas or Win32 APIs, you'll receive events from all connects, disconnects, and bandwidth added/removed on the machine.

Jeff Winn
  • 804
  • 8
  • 14
0

lpvNotifier of the RadDial function allows you to pass a RasDialFunc1 which will receive notification events regarding the lifetime of the connection. (RasGetConnectStatus is also available for polling)

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Thanks for your reply Alex. I am using Rasphone.exe to make the Dial up connection. So from that point of view how can i achieve this. – 2vision2 Jul 24 '12 at 10:28
  • Oh I thought you meant you were using the RASDIAL API not the command line – Alex K. Jul 24 '12 at 10:30
  • But still is there a way to do this? That is to monitor the connection and exit my app once the user disconnected the connection manually? – 2vision2 Jul 24 '12 at 10:41
  • If your doing it that way RaeEnum as suggested by Andreas, if you want fine control don't use an external program. – Alex K. Jul 24 '12 at 10:51
  • 1
    For clarification, the RasDialFunc1 callback used by RasDial only receives notifications while a connection attempt is in progress. Once the connection attempt finishes, you will no longer get updates. So if the connection succeeds, that's where it stops.. that callback will not be notified if the connection is terminated outside of a connection attempt. – Jeff Winn Jul 24 '12 at 17:07