4

Im using this tiny piece of code to get to know if my Windows is 32 or 64 bit:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

static int is64bitOS()
{
    SYSTEM_INFO si;
    GetSystemInfo(&si);

    if((si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_IA64)||(si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_AMD64)==64)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{
    printf("%d\n", is64bitOS());
    return 0;
}

I bought and installed 64 bit version of Windows 7 - however, the above code shows 0 meaning my os is 32 bit ... how to deal with it?

Ok, my other approaches, which basically do not work as well ... On my 64-bit Windows 7 I only see 32 bit ..

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>

int getArchType1()
{
    SYSTEM_INFO sysInfo;
    GetSystemInfo(&sysInfo);

    switch(sysInfo.wProcessorArchitecture)
    {
    case 6:
        return 64;
    case 9:
        return 64;
    case 0:
        return 32;
    default:
        return -1;
    }
}

static char *getArchType2()
{
    char *archType = malloc(sizeof(char) * (255));
    memset(archType, '\0', 255);

#if defined _WIN64
    strcpy(archType, "64-bit");
#else
    strcpy(archType, "32-bit");
#endif // defined

    return archType;
}

int main()
{
    char *arch = getArchType1();
    printf("%s\n", arch);
    free(arch);
    printf("%d\n", getArchType2());
    char c;
    scanf("%c", &c);
    return 0;
}
mirx
  • 606
  • 1
  • 9
  • 21
  • 1
    I take it you tried building this as a 64-bit executable? If you use a 32-bit executable, I expect it to return a 32-bit processor, as most apps would not know what to do if you tell them that they are not on a processor that they are compiled for (same applies to code asking what processor in Linux, 32-bit apps will be told they are on a 32-bit processor, not a 64-bit one) – Mats Petersson Apr 29 '14 at 21:37
  • Im building my code with Code::Blocks (single file) so I guess its by default 32 bits. How and where can I change it? – mirx Apr 29 '14 at 21:53
  • Yes, the wow64 emulator that allows 32-bit code to run on a 64-bit operating system does a very convincing job :) Trying to defeat it is a bit pointless, you ought to just target x64. You'll never have a problem on a 32-bit operating system, somebody else got *that* wrong. – Hans Passant Apr 29 '14 at 22:26
  • The result of a boolean OR operation is not going to equal 64. – David Schwartz Apr 30 '14 at 05:23

1 Answers1

11

From MSDN:

GetNativeSystemInfo function

Retrieves information about the current system to an application running under WOW64. If the function is called from a 64-bit application, it is equivalent to the GetSystemInfo function.

There is a hint right there in GetSystemInfo description:

To retrieve accurate information for an application running on WOW64, call the GetNativeSystemInfo function.

It basically explains that you were requesting information about environment you're on, rather than real system architecture.

And it gives you another hint as well, that on 64-bit Windows you can have both code executed: Win32 and x64, it is up to you what platform to target.

See also:

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • I came up with this solution: http://pastie.org/private/u6m3uqtcp6cswf0fkpa09a. Will it be cross-platform (appropriate among all compilers (Im interested mainly in GCC and VS) and Windows versions)? – mirx May 01 '14 at 09:33
  • No, this does not get you architecture of the OS, because you need to query OS, as mentioned above. – Roman R. May 06 '14 at 06:21
  • Huh, did another code: http://pastie.org/pastes/9146253/text?key=iq9rpcoqkz8fy7gtztretw but when compiled (I compile it on 32 bit OS and run the `*.exe` on 64 bit OS) it only shows 32 bit ... – mirx May 06 '14 at 16:17