This project is read-only.

Creating an application using DotRas for multiple Windows Versions

Mar 15, 2015 at 6:01 PM
Edited Mar 15, 2015 at 6:14 PM
Hello all

This is less a question than passing some Information. I had the requirement to provide a pre-customized VPN Client usable for multiple Windows versions. Additionally the application should be just one exe to copy,, no Installation. After doing some research I found an easy way to do this, which I want to pass along in case somebody else has the same requirements.

The basic idea was to but the DotRas dlls as embedded resources and load them based on the underlying operating System. In Visual Studio this can be done be adding a normal reference to the DotRas dll (does not matter what version) and then adding the different DotRas dlls additionally as existing files with the names: DotRas-VISTA.dll, DotRas-Win7.dll, DotRas-WIN9.dll, DotRas-XP.dll. In the properties for each dlls it should be set "Embedded Resource".

Image
Image

With the follwoing code in Program.cs the correct dll is then loaded on application start:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Runtime.CompilerServices;

namespace VPNConnectGUI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            AppDomain.CurrentDomain.AssemblyResolve += (sender,
              args) =>
              {
                  String osVersion = GetOSVersion();
                  String resourceName = "VPNConnectGUI." + new AssemblyName(args.Name).Name + osVersion + ".dll";
                  using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                  {
                      Byte[] assemblyData = new Byte[stream.Length];
                      stream.Read(assemblyData, 0, assemblyData.Length);
                      return Assembly.Load(assemblyData);
                  }
              };
            StartApp();
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static void StartApp()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        private static string GetOSVersion()
        {
            int _MajorVersion = Environment.OSVersion.Version.Major;

            switch (_MajorVersion)
            {
                case 5:
                    return "-XP";
                case 6:
                    switch (Environment.OSVersion.Version.Minor)
                    {
                        case 0:
                            return "-VISTA";
                        case 1:
                            return "-WIN7";
                        case 2:
                            return "-WIN8";
                        default:
                            return "-XP";
                    }
                    break;
                default:
                    return "-XP";
            }
        }
    }
}
When the application is build, the resulting exe contains all versions of the DotRas dll as embedded resource,, which on AssemblyResolve loades the correct dll for the underlying OS.

For me this worked quite good and the users can take the exe, copy it wherever they want, start it, enter the VPN credentials and connec to our internal network.

Regards
Philipp
Mar 15, 2015 at 10:37 PM
I'm not sure if you read the documentation, but you can use the same DLL on all the different operating systems as they're all forward compatible. The only issue comes in when you're trying to use features that are OS specific and support an earlier OS version.

For example: supporting IPv6 and Windows XP simultaneously.
Mar 16, 2015 at 9:11 AM
You may be right. However I had such a lot of compatibility problems on differen Windows versions (the application hanged every second time I established a connection), that I had to try out other things (I tried with different versions of DotRas and had different results on different Windows Versions). With this approach the application runns quite perfect. But it may also be, because of changs in DotRas since I downloaded the last version. The last time I worked on my application was in December last year. We were discussing back then, because I was not able to track down the issue with the application not being stable (specially in Windows 8.1). So far it is now ... :-)

Regards
Philipp