This project is read-only.

enumerate all RAS/VPN connections

Jul 16, 2009 at 9:05 AM

Does this library provide a method to enumerate all RAS connections no matter if they are presently connected (active)?

I looked at the examples using RasDialer::GetActiveConnections() and this works great for listing the connected instances, but I also need to enumerate all possbile RAS/VPN network connections.

Thank You! 

Jul 16, 2009 at 2:35 PM

Use the RasPhoneBook class for the entries listed in phone books. Keep in mind, Windows has 2 phone books in use even though you only see one.

RasPhoneBook pbk = new RasPhoneBook();

pbk.Open(); // This method determines which phonebook you're opening.

 

Once you have the phonebook opened, you can use the pbk.Entries property to look at the entries in the phone book.

Jul 16, 2009 at 2:44 PM

Thanks for the tip!  I have it working now.  

I had tried enumerating the entries in the RasPhoneBook but I did not realize there were separate phone books for "Users" and "AllUsers" .  Of course by default the system phone book was opened and I did not have any entries defined for the system, only for the user.  All better now.

Thanks again!

 

Jun 17, 2010 at 3:19 AM
Edited Jun 17, 2010 at 5:11 AM

Hi guys, I also have to enumerate the entries.  I'm using Vista 64.  The 2K and 2K8 version both give me the same result.

If I do the following:

Dim pbk as RasPhoneBook = New RasPhoneBook
pbk.Open(True)

I get an error:

Argument exception occured

'name' cannot be a null reference or empty string.  Parameter name: name

If I do the following:

Dim pbk as RasPhoneBook = New RasPhoneBook
pbk.Open()

Then I get no entries at all.

I seem to be following the syntax correctly, but this has me stumped.

Dave

 

Edit:  I have created a RAS entry and saved it for All Users.  Now I get the error regardless if I pass Null, True or False to pbk.Open().

Jun 17, 2010 at 5:16 AM

That could mean a couple things, but I'm betting it's the problem with the device when it's being loaded during initialization of the phonebook. Just grab the 1.2 copy from the source code tab and compile it as shown in the documentation tab.

Let me know if that fixes your problem.

Jun 17, 2010 at 5:17 AM

Ok, I think I understand what is happening. 

My PC doesn't physically have a modem attached to it, and thus the RAS Entry doesn't have a device or a device name.  The error is being raised because the rasHelper class is trying to create a Device with a null name.

rasHelper line # 940

retval.Device = RasDevice.Create(entry.deviceName, entry.deviceType);



I will see if I can put a Try Catch around this to see if it will continue without further error, and post my results back here.

Dave

Jun 17, 2010 at 5:19 AM
jeff_winn wrote:

That could mean a couple things, but I'm betting it's the problem with the device when it's being loaded during initialization of the phonebook. Just grab the 1.2 copy from the source code tab and compile it as shown in the documentation tab.

Let me know if that fixes your problem.

 Thanks Jeff.  What do you think of my suggestion?

(I normally don't expect answers the same day I post them due to the timezone differences.  I am in Australia  :))

 

Dave

Jun 17, 2010 at 5:25 AM

The 1.2 SDK already has it fixed, so you don't need to fix it yourself. As for your suggestion I typically avoid use try/catch blocks where I can validate an argument before passing them around the code rather than allowing the app to break and then trying to handle it.

Jun 17, 2010 at 5:36 AM
jeff_winn wrote:

The 1.2 SDK already has it fixed, so you don't need to fix it yourself. As for your suggestion I typically avoid use try/catch blocks where I can validate an argument before passing them around the code rather than allowing the app to break and then trying to handle it.

 Thanks Jeff.  I will do that.  I agree with your statement around the Try Catch.  My 'fix' was going to be temporary until the issue was fixed by yourself (which it now is  :))

Thanks again.

Dave

Jun 17, 2010 at 5:39 AM

If you have any other problems or questions, feel free to ask.

- Jeff

Jun 17, 2010 at 6:00 AM

Jeff, I have build 1.1 59809.  Is that the one you meant?  If so, the issue still exists.

Dave

Jun 17, 2010 at 6:16 AM
Edited Jun 17, 2010 at 6:28 AM

Changeset 59809 is the 1.2 version under development, the device issue should have been solved a while ago. Are you getting the same error? If you posted a stack trace, that would help diagnose the issue further.

Edit: I've got to get to bed, this will have to wait until tomorrow

Jun 17, 2010 at 7:04 AM
Edited Jun 18, 2010 at 1:05 AM

Jeff, the problem is easy to replicate.  Get a PC that doesn't have a modem, create a RAS entry (ignoring the Windows error that states there isn't a device - i.e. tell it to create the entry anyway), then try and enumerate the ras entries.

Bam!

I have fixed it with this:

if (!(entry.deviceName == string.Empty)) 
{ 
    retval.Device = RasDevice.Create(entry.deviceName, entry.deviceType);
}

Edit: 

I am calling this with the following:

    For Each Entry As RasEntry In pbk.Entries
            Console.WriteLine(Entry.Name)
            Dim RASEntry As New Connection ' // A custom type I use

            RASEntry.Name = Entry.Name & " (" & Entry.PhoneNumber & ")"
            RASEntry.InterfaceType = NetworkInterfaceType.GenericModem
            If Entry.Device Is Nothing Then
                RASEntry.Description = "[No device]"
            Else
                RASEntry.Description = Entry.Device.Name
            End If

            RASEntry.Speed = -1
            m_RASConnections.Add(RASEntry)
        Next

 I didn't want to do the whole "If Entry.Device Is Nothing" bit, but couldn't see a way around it.  Without that check there is an exception because Entry.Device = null (i.e. Is Nothing in VB speak).

 

Jun 17, 2010 at 2:48 PM

Unfortunately I can't use that fix, there are PPPoE adapter devices that do have an empty device name but are still a valid device with a device type. I also see you removed the validation on the deviceType before attempting to create the device. A valid device would need to have both parts, otherwise it would be unusable by the operating system to dialing an entry. That's why the validation was setup the way it was in that changeset.

After reading that last line you wrote, I'm not exactly sure if the app was still throwing an error or if you just claimed it wasn't working because you didn't want to check if the device was null. The device property on the entry was intended to be null in the scenario that the device name was a null reference and a device type was not provided.

Can you please clarify what you meant when you said "Jeff, I have build 1.1 59809.  Is that the one you meant?  If so, the issue still exists." for me? I'd like to make sure an actual exception still being thrown.

I'm going to run the same scenario you described on one of my virtual machines and see if I can replicate the problem.

Jun 17, 2010 at 3:02 PM

"Unfortunately I can't use that fix, there are PPPoE adapter devices that do have an empty device name but are still a valid device with a device type."

Umm well i just want to enumerate entries including ones not connected/associated to a device.

"I also see you removed the validation on the deviceType before attempting to create the device."

I thought the only thing I did was to remove the validation on the name.

I just want to enumerate all the RAS connections on my machine, regardless of whether I have an actual device or not.  I don't want to create a device, and i also don't want to only enumerate active entries.

What I am actually trying to do is:

1. Enumerate all network connections, including RAS

2. Look for connection events for specific types.  I can specify WiFi, RAS, PPPoE, ATM, Ethernet etc.

3. Run defined process(es) when the specified event occurs.

I can already see the non-RAS devices connecting and disconnecting.  It's the RAS devices that have me stumped.

The worst bit is that all of this is only half the story. I also want to enumerate and create events for incoming RAS connections.  To do this though, I think I will have to use something like the vspe (use Google) and monitor the CD line.  Tthat is also my fallback if this doesn't work.

As for throwing an error, yes it certainly does.  You have the Throw .... code in there which is being run when I try to get properties (name) of a null device.

I'm at home now so won't have access to the code for another ummm 10 hours or so.

I do appreciate the help so far.

Jun 18, 2010 at 4:28 AM

Well I ran a test trying to replicate the problem you described by creating a connection which did not have a modem after I uninstalled the modem on my computer and made the connection anyway, and could not replicate the error. When the phonebook opened, I had a device whose name was empty, and the device type of "modem" which did create a RasDevice object as expected.

Which operating system are you experiencing the problem on? I was using Windows 7 when I performed the test. Also, I will need a stack trace if I'm going to proceed along with the exception message you're seeing.

Jun 18, 2010 at 4:49 AM

Thanks Mike.

I am using Vista 64.  For me to generate the Stack trace I have to put your code back to what it was (is).

I will be setting it to:

retval.Device = RasDevice.Create(entry.deviceName, entry.deviceType);

I will post the results shortly.

Jun 18, 2010 at 4:53 AM
Dangerous wrote:

Thanks Mike.

I am using Vista 64.  For me to generate the Stack trace I have to put your code back to what it was (is).

I will be setting it to:

retval.Device = RasDevice.Create(entry.deviceName, entry.deviceType);

I will post the results shortly.

 > DotRas.dll!DotRas.ThrowHelper.ThrowArgumentException(string argumentName = "name", string resource = "'{0}' cannot be a null reference or empty string.", object[] args = {object[0]}) Line 68 + 0x67 bytes C#
  DotRas.dll!DotRas.ThrowHelper.ThrowArgumentException(string argumentName = "name", string resource = "'{0}' cannot be a null reference or empty string.") Line 54 + 0x37 bytes C#
  DotRas.dll!DotRas.RasDevice.Create(string name = "", string deviceType = "modem") Line 120 + 0x2b bytes C#
  DotRas.dll!DotRas.RasHelper.GetEntryProperties(DotRas.RasPhoneBook phoneBook = {DotRas.RasPhoneBook}, string entryName = "Not a Dial-up Connection") Line 940 + 0x18 bytes C#
  DotRas.dll!DotRas.RasEntryCollection.Load() Line 133 + 0x23 bytes C#
  DotRas.dll!DotRas.RasPhoneBook.Open(string phoneBookPath = "C:\\ProgramData\\Microsoft\\Network\\Connections\\Pbk\\rasphone.pbk") Line 241 + 0x2d bytes C#
  NetworkState.dll!NetworkState.Functions.GetRASPhonebookEntries(DotRas.RasPhoneBookType PhonebookType = DotRas.RasPhoneBookType.AllUsers) Line 251 + 0x14 bytes Basic
  NetworkState.dll!NetworkState.Functions.EnumerateRASConnections() Line 219 + 0x29 bytes Basic
  NetworkState.dll!NetworkState.Functions.EnumerateAllConnections() Line 186 + 0x12 bytes Basic
  NetworkState.dll!NetworkState.Functions.StartMonitoring() Line 135 + 0x18 bytes Basic
  NetChangeState.exe!NetChangeState.frmMain.Form1_Load(Object sender = {NetChangeState.frmMain}, System.EventArgs e = {System.EventArgs}) Line 45 + 0x41 bytes Basic
  [External Code] 

Jun 18, 2010 at 5:00 AM
Edited Jun 18, 2010 at 8:26 AM

Here's an idea... why don't you create an overloaded version of GetEntryProperties that doesn't create the device.  Then I can call that if I don't want a device created.

If it was VB I would do this:

Public Shared Function GetEntryProperties(ByVal phoneBook as RasPhoneBook, entryName as string, Optional ByVal CreateDevice as Boolean = True) as RasEntry

Edit:  Changed False to True above 

Jun 18, 2010 at 6:13 AM
Edited Jun 18, 2010 at 6:23 AM

I'm looking at the code and the name parameter of the RasDevice.Create static method only throws an ArgumentNullException if the device name is a null reference, it couldn't produce that stack trace if you're using the 59809 changeset.

Edit: I'm not looking to hack a workaround in to fix the problem, from what I can see you're getting errors that don't exist in the current changeset. Implementing your suggestion would produce a corrupted phonebook in memory that when persisted back to the file system would remove the device attached to the entry making it unusable. I'd suggest downloading a fresh copy of the 59809 changeset or latest version from source control and trying again. If the error does occur again after downloading a fresh copy, I'll need another stack trace which produced that exception.