0

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.

Coze
  • 83
  • 9
  • It is not called "bittage" but "word size"... And you did compile your C (or C++) code for 32 or 64 bits word size... – Basile Starynkevitch Mar 22 '13 at 18:52
  • Usually the decision to run a 32 bit or 64 bit version of your program is made at installation time, you install whichever is appropriate and leave the other out. No need to make the determination at run time. – Mark Ransom Mar 22 '13 at 18:58
  • I want to know if it can be done and I am mostly interested in if there are some incompatible parts – Coze Mar 22 '13 at 19:05
  • @MarkRansom: There is a difference between the bitness of the program and the bitness of the system. A 64-bit system can run a 32-bit program through WOW64 emulation. For example, Process Explorer, a 32-bit program, detects if the system is 64-bit and launches a 64-bit version of itself when appropriate. – user1354557 Mar 22 '13 at 19:31

2 Answers2

3

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)

user1354557
  • 2,413
  • 19
  • 29
-1

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.

CallMeNorm
  • 2,299
  • 1
  • 16
  • 23
  • Size of `long` isn't bound to the OS type, so it doesn't have to be 8 bytes on 64-bit Linux and 4 bytes on 32-bit Linux: http://stackoverflow.com/questions/10040123/long-type-64bit-linux – SomeWittyUsername Mar 22 '13 at 19:14
  • Your answer is not true on all platforms. In fact `sizeof(int*)` is more correct – phuclv Dec 18 '13 at 12:31