Coze
Coze

Reputation: 83

Windows 64-bit and 32-bit incompatibilities

I know that 64-bit applications need 64-bit Windows.

Which c/c++ code will work only for 64-bit or 32-bit exclusively? Edit: I have found it here

Can I determine proccess word size on runtime: Like I will have 32-bit application which returns if OS is 32 or 64 bit and then runs sub/new proccess with right word size.

Upvotes: 0

Views: 312

Answers (2)

user1354557
user1354557

Reputation: 2503

You can find out if your system is 32-bit or 64-bit with GetNativeSystemInfo. For example, you could do something like this:

typedef void (WINAPI *GetNativeSystemInfo_t)(LPSYSTEM_INFO lpSystemInfo);

BOOL IsSystem64Bit()
{
    HANDLE kernel32 = LoadLibrary("kernel32.dll");
    SYSTEM_INFO si;

    GetNativeSystemInfo_t GetNativeSystemInfoPtr
        = (GetNativeSystemInfo_t)GetProcAddress(kernel32, "GetNativeSystemInfo");

    if (GetNativeSystemInfoPtr == NULL)
        return FALSE;

    GetNativeSystemInfoPtr(&si);
    return (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64);
}

The reason the function is resolved dynamically is because it doesn't exist on versions of Windows prior to XP. (And on those versions of windows, we already know that the system is not 64-bit)

Upvotes: 3

CallMeNorm
CallMeNorm

Reputation: 2605

I'm not sure about Windows, and so obviously this will be limited in helpfulness, but on Linux you can determine word size at runtime. A long int will be the word size. On 64-bit Linux long is 64-bits and 32-bits on 32-bit Linux.

So, this seems really stupid and inconsistent, but you could do something like

 char ws[3];
 sprintf(ws, "%d", sizeof(long));
 fprintf(stderr, "%s\n", ws);

You can then compare ws with different values to see what the word size is. I'm sure that Windows has a comparable basic type that can help you tell what the word size is.

Upvotes: -1

Related Questions