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.
        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;
        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:

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.

        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;

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 ( . 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:

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


Popular posts from this blog

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 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.51EF 6.0.2VS2013
It seems that there …

GET call of REST API that contains '/'-slash character in the value of a parameter

Let’s assume that we have the following scenario: I have a public HTTP endpoint and I need to post some content using GET command. One of the parameters contains special characters like “\” and “/”. If the endpoint is an ApiController than you may have problems if you encode the parameter using the http encoder.
using (var httpClient = new HttpClient()) { httpClient.BaseAddress = baseUrl; Task<HttpResponseMessage> response = httpClient.GetAsync(string.Format("api/foo/{0}", "qwert/qwerqwer"))); response.Wait(); response.Result.EnsureSuccessStatusCode(); } One possible solution would be to encode the query parameter using UrlTokenEncode method of HttpServerUtility class and GetBytes method ofUTF8. In this way you would get the array of bytes of the parameter and encode them as a url token.
The following code show to you how you could write the encode and decode methods.

Entity Framework (EF) TransactionScope vs Database.BeginTransaction

In today blog post we will talk a little about a new feature that is available on EF6+ related to Transactions.
Until now, when we had to use transaction we used ‘TransactionScope’. It works great and I would say that is something that is now in our blood.
using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection("...")) { conn.Open(); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn; sqlCommand.CommandText = ... sqlCommand.ExecuteNonQuery(); ... } scope.Complete(); } Starting with EF6.0 we have a new way to work with transactions. The new approach is based on Database.BeginTransaction(), Database.Rollback(), Database.Commit(). Yes, no more TransactionScope.
In the followi…