how to create an automatic dial up connection??

Jul 29, 2009 at 5:51 AM

hi!

i need to make a very simple application using c#.net.
i want my application to dial a connection automatically (without user involvement), send some data and then automatically hang up.

how can i achieve this using RAS?? what function/method should be used in this context?? 

Please suggest some solutions!!
thanks in advance!!

Coordinator
Jul 29, 2009 at 1:01 PM

The RasDialer component will handle dialing the connection for you once it has been established in the phonebook, you can also use its GetActiveConnections method to retrieve the connections and then hang up one of them when you're finished. The RasPhoneBook component will handle entry manipulation and creation if the entry you need to dial doesn't already exist in the phonebook.

RasDialer dialer = null;

try
{
    RasHandle handle = null;

    dialer = new RasDialer();
    dialer.EntryName = "Your connection name here";

    // You can use the overload or async dial method here depending what your needs are. If the credentials are stored you don't need to pass credentials into the method.
handle = dialer.Dial();

    // The entry should be connected at this point. Do what you need to with it.

    // The handle was stored earlier so it can be used to find entry that was dialed. You don't need to use the handle, you can also check the entry name of the connection.

    foreach (RasConnection connection in dialer.GetActiveConnections())
    {
        if (connection.Handle == handle) 
        {
            connection.HangUp();
            break;
        }
    }
}
catch (Exception ex)
{
    // Do something to log the exceptions that occur here.
}
finally
{
    if (dialer != null)
    {
        // Make sure you dispose of the dialer, otherwise if the application crashes the handle cannot be released by Windows and the machine will need to be restarted.
        dialer.Dispose();
    }
}
Aug 12, 2009 at 12:17 PM

I need to do the same thing but to do 

<font size="2" color="#0000ff">

int

</font>

ret = SafeNativeMethods.RasDial(lpRasDialExtensions, phoneBook, lpRasDialParams, NativeMethods.RasNotifierType.RasDialFunc2, callback, out handle);

ret = 120 and handle.IsInvalid = true

What is the problem??

 

Thanks

Coordinator
Aug 12, 2009 at 1:44 PM

Well, I haven't ever seen that error code before and had to go look it up. WinError.h indicates a 120 result code is ERROR_CALL_NOT_IMPLEMENTED: This function is not supported on this system.

Which operating system are you using? It looks like you're also using the source code directly, so which compilation symbol did you include (if any)?

Aug 13, 2009 at 7:30 AM

The operating systems with wicth I have working are: Windows Vista Entrerprise and Windows 2003 R2.

The problem is that I need modify the configuration of a connetion make in Connection Manager of Microsoft.

I've obtained to see the connetion with your dll (DotRas) but I cannot to do that appears the window to introduce the user and password. The error is 120 like I have commented.

Other option that I have thought is to create the connection normally but the parameters of the pbk do not coincide

It's the first time with this topic and  I am a bit lost

Thanks

Aug 13, 2009 at 10:08 AM

I have had to modify the line that I you have marked in red. I found the soución in http://www.codeproject.com/KB/IP/rasconn_632.aspx?display=Print

        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public static ReadOnlyCollection GetActiveConnections()
        {

            ReadOnlyCollection retval = null;

            int size = Marshal.SizeOf(typeof(NativeMethods.RASCONN));

            IntPtr lpCb = new IntPtr(size);
            IntPtr lpcConnections = IntPtr.Zero;

            bool retry = false;

            do
            {
                NativeMethods.RASCONN conn = new NativeMethods.RASCONN();
                conn.size = 0x53c;
Aug 13, 2009 at 11:38 AM

Once created the connection, how can I open the window of usuario/password?. I need to ask user and password

Coordinator
Aug 13, 2009 at 2:04 PM

You should not have to modify anything within the project, and you should definitely **NOT** hard code the size of the structures.

As for your supposed "fix" that would only take care of problems with an error code of 632, not 120. Not to mention hardcoding the structure sizes introduces more problems when you change the compilation symbol you're using to switch which platform you're targetting. This project has been working on every operating system I support on both 32 and 64 bit platforms. If you can't get something to work, the chance is high you're at fault.

With that said, back to diagnosing your problem....

You've told me which operating systems you're supporting, but you left out what compilation symbol you've used for DotRas when you compiled it. If you're trying to support both of those operating systems, you'll need to use the WINXP or WINXPSP2 build. Using the WIN2K8 symbol (which is used for Vista / 2008 server) on a Windows 2003 R2 server will result in a lot of problems with error code 632 being returned. I cannot fix that, that's your own fault for not reading the getting started section completely. If you want to prompt the user for their username and password, you don't have to use the RasDialer component. There are a few other components like RasDialDialog that are used to dial a connection and prompt the user for credentials. This is the standard dialing interface you see when you dial the connection through Windows.

I'll need all of the questions below answered if you want any kind of help from me to fix what's going on with the project:

) Which method did you call in DotRas that caused the exception with error code 120 to be thrown?

) Which compilation symbol were you using when the exception occurred?

) Which operating system were you on when the exception was thrown?

) Are you on 32 bit or 64 bit systems?

Aug 14, 2009 at 12:18 PM

) Which method did you call in DotRas that caused the exception with error code 120 to be thrown?

 

                foreach (RasConnection connection in dialer.GetActiveConnections())
                {
                    if (connection.Handle == handle)
                    {
                        connection.HangUp();
                        break;
                    }
                }

) Which compilation symbol were you using when the exception occurred?

WIN2K8

) Which operating system were you on when the exception was thrown?

Windows Vista Enterprise.

) Are you on 32 bit or 64 bit systems?

32 bit

I have could all, I've created the connection and I've could to show the window to get the credentials.

Thanks for all.

Coordinator
Aug 14, 2009 at 2:17 PM
Edited Aug 14, 2009 at 3:10 PM

There should be nothing wrong with the WIN2K8 build on Vista, that's the operating system I'm using for development. Though I'm using Vista Ultimate, not Enterprise. What happens if you use the WINXP or WINXPSP2 symbol? I can't imagine what'd be causing that error unless it's something related to the UAC. Did you request elevated privileges on the process before you tried calling the method? I keep UAC disabled on my machine since I don't need it on.

If you want your application to support features specific to Vista, you'll need to have two builds of your application... one with the WINXP/WINXPSP2 symbol and the other with WIN2K8 symbol. If you don't need any Vista specific features, it would be easier if you just used the WINXP / WINXPSP2 build so it'd be cross platform compatible without requiring a separate build.

Coordinator
Aug 15, 2009 at 2:22 AM

I just tested the WIN2K8 build on Vista and I didn't see any issues with it, with the UAC both on and off.

Aug 18, 2009 at 8:16 AM

Sorry, I haven't to reply you.

I have been using a user with admin privileges to run the application. I don't understand why is produced this error but I haven't continued trying because ultimately I do not need use the "hangup" method.

If I have time, I will put with it, now I have to try to deal each of the parameters on a connection, I have never worked with RAS.

Only I can say to you that the version of my Windows Vista is in Spanish and it has installed in a tablet pc.

Thanks

Coordinator
Aug 18, 2009 at 1:44 PM

So the hangup method is causing the problems? I asked you before and for whatever reason you decided to give me a block of code so I've been testing the wrong method. Having admin rights on Vista doesn't necessarily give you admin rights. You should try elevating the privileges used by your application and testing the HangUp method again.

Aug 18, 2009 at 2:19 PM
Edited Aug 18, 2009 at 2:23 PM
I recently published normal - ready debugged code, which allows all actions over VPN connections. I do not understand why he was removed, probably to ensure that there was no ready-made solutions at the forum:)

Now on the topic: This code:
handle = dialer.Dial();
is not good to use in the program, as it does not allow redial after the break the connection. I could be wrong, but faced with this.
To connect, I wrote the following method: (Do not pay attention to the Russian text in the reference line - I'm from Ukraine)
private void DialVpn(ref string errorMessage)
        {
            RegistryKey readKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Your Application Name");
            string loadString = (string)readKey.GetValue("connectionId");
            readKey.Close();

            RasPhoneBook phoneBook = new RasPhoneBook();
            
            try
            {
                dialer.EntryName = ConnectionName;
                phoneBook.Open();
                dialer.PhoneBookPath = phoneBook.Path;
                for (int index = 0; index < phoneBook.Entries.Count; index++)
                {
                    if(phoneBook.Entries[index].Id.ToString() == loadString)
                    {
                        if (phoneBook.Entries[index].Name == dialer.EntryName)
                        {
                            if (phoneBook.Entries[dialer.EntryName].GetCredentials() != UserCredential) //UserCredential - Example Class NetworkCredential (Set User Login and User Password)
                            {
                                phoneBook.Entries[dialer.EntryName].UpdateCredentials(UserCredential, SaveUserInfo);
                                dialer.Timeout = 60000;  //Read Coment 1(one)
                                dialer.DialAsync();
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                errorMessage = "Ошибка при установлении соединения с интернетом. Уровень User ULL. Метод: CreateVpnConnection(). Ошибка: dialer.DialAsync();. Уведомите дежурного администратора о данной проблеме. Текст сообщения: " + exc;
                dialer.Dispose();
            }
        }
Coment 1(one):This line sets the time of the connection. I sat two days and could not understand why after a connection is established, it was immediately torn automatically. By default, property = -1, ie TimeOut - indefinitely. But unfortunately it does not work.
Coordinator
Aug 18, 2009 at 2:56 PM
Edited Aug 18, 2009 at 4:09 PM

It was removed due to bad advice, a number of bad design decisions, and your example was incomplete. Which you're doing again in this example. Perhaps bolding the text will actually get you to pay attention...

) The GetCredentials method will NEVER return credentials that can be checked against any user input. The password retrieved by GetCredentials NEVER contains the actual password.

) Dial is a synchronous call. If an error occurs during the connection attempt an exception will be thrown, and if your application crashes the connection will hang requiring a machine restart until the connection can be released. This is why the SDK specifically states that you MUST dispose of the component properly to ensure any pending connection attempt is cancelled before the application terminates.

) Telling others to not use a method simply because it didn't fit your needs is rediculous. That's why I exposed both synchronous and asynchronous versions of the dial method.

As for the problem with timeouts, it should be fixed if you either get lastest or wait until the 1.1 build is released. If the System.Threading.Timeout.Inifinite value is used (which is the default value), the timer will no longer be created.

Another part of your code you're overwriting any stored credentials before you even know if they're valid. That should be handled inside of the DialCompleted event if the Connected flag is set to true to prevent the application overwriting valid credentials.

 

Edit: I do like the idea of allowing the dialer update the stored credentials used by an entry if the connection attempt was successful. I'm going to create a work item to add that as a feature for the 1.2 release.

Aug 20, 2009 at 11:27 AM

Yes, I do not argue that my example was not perfect. I then correct it and finished. After all, if you're not going to publish my finished version of my library. Where I saw that when you dial destroys Dialer; I understood what it needed and had to rewrite the method DialVPN (). Then I made a separate method parser configuration required me to connect. Yes, I noticed that the condition of comparison GetUserCridential and my copy is always true (they never are), so I am always updating UpdateCridential, because in my application users can enter, first under one login, then under another.

Aug 20, 2009 at 11:32 AM

I think that dialer.Dial () I have not worked with redial, just because I was not destroyed after the call dialer.

Coordinator
Aug 20, 2009 at 2:14 PM

That's exactly why I put that information in the documentation for the component. If your application crashes after initiating a connection, the machine will need to be restarted before it can release the connection. Whatever Windows does when connecting, it can't continue if the application that started the connection is not available.

People need to read the SDK so I don't have to keep repeating myself on here.