#ifdef _WIN32

When writing processor-specific code, the _M_IX86, _M_AMD64 and _M_IA64 can be used for conditional compilation — so far, so good. But sometimes code is not exactly processor-specific but rather specific to the natural machine word length (i.e. 32 bit or 64 bit). Fur such situations, there are defines, too — however there is a little catch: For ancient 16 bit code, there is _WIN16. For 64 bit, the WDK build environment defines _WIN64 by default. Given these two macros, it is tempting to conclude that _WIN32 should only be defined for 32 bit builds — however this is not the case. As it turns out, _WIN32 is always defined, both for 32 and 64 bit builds.

And yes, this behaviour is documented on MSDN, but it is stupid anyway.

However, where _WIN32 can be of use is when writing code targeting multiple platforms — as _WIN32 is always defined, it can be used as an indicator that you compile for Windows, regardless of the compiler used (another option is to use _MSC_VER, but that is compiler-specific).

About these ads

5 Responses to “#ifdef _WIN32”


  1. 1 Roman Ryltsov May 2, 2011 at 1:04 pm

    Yeah it may look sick but perhaps it has quite some reasons behind the decision. _WIN32 is introduced quite some time ago and compared to 16-bit Windows it was a completely different world. Not so much difference now between 32-bit and 64-bit code and I am pretty sure they decided to keep _WIN32 defined for 64-bit code for compatibility and code migration reasons.

  2. 2 WndSks May 4, 2011 at 10:00 am

    It is not a big problem in practice since you often end up writing something like:

    #ifdef _WIN64
    SomeAPI()
    #else
    DisableRedirection()
    CallLegacySomeAPI()
    RestoreRedirection()
    #endif

    For cross platform builds, the fact that _WIN32 is defined for 32 and 64 also comes in handy:

    #ifdef _WIN32
    WSA*()
    #endif
    socket()

  3. 3 Somasundaram July 8, 2011 at 10:24 pm

    Hi,

    Is there a way we can avoid these #ifdef to make code compatible with both 32 bit and 64 bit?

    Thanks,
    Keerti

  4. 4 Chillout December 14, 2012 at 8:47 pm

    Why viewers still make use of to read news papers when in this technological world everything is
    presented on web?

  5. 5 video downloader March 22, 2013 at 8:15 am

    Please supplement charge before use when the battery has been kept for a long time.
    Hold your device on end to locate two small
    screws on either side of the dock. Both Intel and AMD contain information built in the processor to allow the processor
    to be used efficiently with mobile computing saving on battery life while giving the best performance.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Categories

Try Visual Assert, the unit testing add-in for Visual Studio (R)


NTrace: Function Boundary Tracing for Windows on IA-32

About me

Johannes Passing, M.Sc., living in Berlin, Germany.

Besides his consulting work, Johannes mainly focusses on Win32, COM, and NT kernel mode development, along with Java and .Net. He also is the author of cfix, a C/C++ unit testing framework for Win32 and NT kernel mode, Visual Assert, a Visual Studio Unit Testing-AddIn, and NTrace, a dynamic function boundary tracing toolkit for Windows NT/x86 kernel/user mode code.

Contact Johannes: jpassing (at) acm org

Johannes' GPG fingerprint is BBB1 1769 B82D CD07 D90A 57E8 9FE1 D441 F7A0 1BB1.

LinkedIn LinkedIn Profile
Xing Xing Profile
Twitter Follow me on Twitter (new)

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: