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

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills)

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills) The idea that moving to the cloud reduces the costs is a common misconception. The cloud infrastructure provides flexibility, scalability, and better CAPEX, but it does not guarantee lower costs without proper optimisation and management of the cloud services and infrastructure. Idle and unused resources, overprovisioning, oversize databases, and unnecessary data transfer can increase running costs. The regional pricing mode, multi-cloud complexity, and cost variety add extra complexity to the cost function. Cloud adoption without a cost governance strategy can result in unexpected expenses. Improper usage, combined with a pay-as-you-go model, can result in a nightmare for business stakeholders who cannot track and manage the monthly costs. Cloud-native services such as AI services, managed databases, and analytics platforms are powerful, provide out-of-the-shelve capabilities, and increase business agility and innovation. H...