Skip to main content

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post, I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM.

WIN32 Support
To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class.
        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEM_INFO
        {
            public uint dwOemId;
            public uint dwPageSize;
            public uint lpMinimumApplicationAddress;
            public uint lpMaximumApplicationAddress;
            public uint dwActiveProcessorMask;
            public uint dwNumberOfProcessors;
            public uint dwProcessorType;
            public uint dwAllocationGranularity;
            public uint dwProcessorLevel;
            public uint dwProcessorRevision;
        }
        ...
        [DllImport("kernel32")]
        static extern void GetSystemInfo(ref SYSTEM_INFO pSI);
        ...
        SYSTEM_INFO pSI = new SYSTEM_INFO();
        GetSystemInfo(ref pSI);


The sample can be found on Github and can be run easily using the following commands:
docker build -f Docker.Win32.Image.txt -t win32 .
docker run win32
As we can see, we were able to make with success a call to a WIN32 API.

Code sample: https://github.com/vunvulear/Stuff/tree/master/Azure/Docker.Win32

How is this possible?
Starting from Windows Server 2016 (and Windows 10 Anniversary Upgrade), Windows can run containers in two different ways.
The first mechanism is using Windows Containers, that runs on top of Host OS kernel, sharing the same kernel between containers. This means that the kernel, binary libraries and many more are shared between containers. On top of this, all the containers are affected and use the same kernel.
In contrast with Windows Containers, Hyper-V Containers runs directly on top of Hyper-V. Each container uses his own kernel image. This allows us to run multiple containers, each of them using a different version of the OS. We have more flexibility, but the footprint might be a little higher.

Docker for Windows is using Hyper-V containers. This means for each container that we run, we will have our own instance of kernel, that is different from the Host version. Having our own kernel instance, give us access to all information, including the Win32 API, as long as the Docker image that we use has that Win32 API available.

COM Support
The second topic is related to COM. The short response is yes, we can make COM calls. Things are more complex at setup level. We need to be sure that that COM is register and available in our image. In the sample provided on GitHub, I register directly from C# code the COM that I use.

Registration
        public static void RegistarDlls(string filePath)
        {
                string fileinfo = "/s" + " " + "\"" + filePath + "\"";
                Process reg = new Process();
                reg.StartInfo.FileName = "regsvr32.exe";
                reg.StartInfo.Arguments = fileinfo;
                reg.StartInfo.UseShellExecute = false;
                reg.StartInfo.CreateNoWindow = true;
                reg.StartInfo.RedirectStandardOutput = true;
                reg.Start();
                reg.WaitForExit();
                reg.Close();
        }
...
            RegistarDlls("Interop.NetFwTypeLib.dll");

COM call
            const string guidFWPolicy2 = "{E2B3C97F-6AE1-41AC-817A-F6F92166D7DD}";
            const string guidRWRule = "{2C5BC43E-3369-4C33-AB0C-BE9469677AF4}";
            Type typeFWPolicy2 = Type.GetTypeFromCLSID(new Guid(guidFWPolicy2));
            Type typeFWRule = Type.GetTypeFromCLSID(new Guid(guidRWRule));
            INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(typeFWPolicy2);
            INetFwRule newRule = (INetFwRule)Activator.CreateInstance(typeFWRule);
            newRule.Name = "MabuAsTcpLocker_OutBound_Rule";
            newRule.Description = "Block outbound traffic  over TCP port 80";
            newRule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
            newRule.RemotePorts = "8888";
            newRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
            newRule.Enabled = true;
            newRule.Profiles = 6;
            newRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;

As we can see above, we can make WIN32 and COM calls from Docker.

The last question that I received yesterday is related to ASP.NET Web Forms and if we can run them in Docker Containers. Microsoft created a Docker image for ASP.NET applications (https://hub.docker.com/r/microsoft/aspnet/) . Inside it, we can run any kind of ASP.NET applications the MVC to Web Forms or other kind of applications. The IIS is already configured, so we don’t need to configure any bindings to it.
The sample code for ASP.NET WebForms can be found on GitHub: https://github.com/vunvulear/Stuff/tree/master/Azure/Docker.WebForms

In comparison with the previous samples we used ‘microsoft/aspnet’ Docker image. Don’t forget to specify in the browser the IP of the container, when you want to access the web application. The IP of the Docker container can be obtained using the following command:
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" webforms

In this post we validated that we can do the following things in a Docker Container:

  • Make Win32 calls
  • Use COM Interop
  • Run a ASP.NET WebForms application


Comments

Popular posts from this blog

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see