2

On my 32 bit system (Windows 10), I created a very simple Windows Forms (.NET FW 3.5) application:

        bool x86 = IntPtr.Size == 4;

        if (x86)
        {
            label1.Text = "OS: 32 bit";
        }
        else // IntPtr.Size == 8
        {
            label1.Text = "OS: 64 bit";

            // For following method, see:
            // http://stackoverflow.com/a/336729/360840
            if (InternalCheckIsWow64())
            {
                label1.Text += "; 32 bit program running under WoW64";
            }
        }

In Visual Studio 2015, under Properties -> Build, I set Platform Target as x86.

I took this executable to a 64 bit Windows Server 2012 Datacenter edition, and ran it. I fully expected to see it report itself as running under WoW64, but was surprised to find out that wasn't the case; it only reports that the architecture is 64 bit, but does not display the "; 32 bit program running under WoW64" part.

Why is this, and what is going on?

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • Wait. The label is actually set to "OS: 64 bit" with a build target set to x86? – Milster May 11 '17 at 04:50
  • Yes, 32 bit apps can run on 64 bit systems. – Sabuncu May 11 '17 at 04:56
  • 2
    That's not my question. As far as I can remember, setting the build to x86, should force IntPtr.Size to 4 (As the process will be a 32 bit process (Which is exactly the behavior I'm watching in a test console app right now on my PC)). This is why I asked, if in your case the label is really set to "OS: 64 bit" in this case. – Milster May 11 '17 at 04:58
  • Hmm, you're right. Platform: Any CPU, Platform target: x86, and on the 64 bit server, it says "OS: 32 bit". This is a whole new twist. My basic understanding, which is shaky anyways, is shot now. – Sabuncu May 11 '17 at 05:15
  • I had the if statement backwards, fixed it, and it works properly now. Please post your original comment as an answer so I can mark it as the answer. Thanks so much. – Sabuncu May 11 '17 at 05:19

1 Answers1

1

IntPtr.Size is set by the process itself (or better said, the build target), not by the OS.

A program build with an x86 target in c# will always have IntPtr.Size = 4, while x64 will always have IntPtr.Size = 8. This is in fact a check for the process, not the OS.

Edit: The answer in the linked topic states exactly the same.

Milster
  • 652
  • 4
  • 13