mirx
mirx

Reputation: 626

Detect Windows architecture (32 or 64 bit) with C++ - wProcessorArchitecture shows wrong results

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;
}

Upvotes: 4

Views: 10815

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

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:

Upvotes: 11

Related Questions