    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: QM Bites: Understand Windows Operating-System Identification Preprocessor Macros</title>
        <link>https://members.accu.org/index.php/articles/2621</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #149 - February 2019</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c395/">o149</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-395/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+395/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;QM Bites: Understand Windows Operating-System Identification Preprocessor Macros</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 04 February 2019 17:12:30 +00:00 or Mon, 04 February 2019 17:12:30 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Quality matters and bite sized articles help. Matthew Wilson returns with a QM Bites.</p>
<p><strong>Body:</strong>&nbsp;<h2>TL;DR</h2>

<p>Compiler defines <code>_WIN32</code> and <code>_WIN64</code>; you define <code>WIN32</code> or <code>WIN64</code>. Carefully discriminate.</p>

<h2>Bite</h2>

<p>When compiling for Windows 32 and 64-bit architectures, there are four main preprocessor object-like macro definitions for discriminating operating system (not architecture) that one may encounter:</p>

<ul>
	<li><code>_WIN32</code></li>
	<li><code>_WIN64</code></li>
	<li><code>WIN32</code></li>
	<li><code>WIN64</code></li>
</ul>

<p>You must take care that you understand the origins and meanings of these.</p>

<h3>_WIN32 and _WIN64</h3>

<p>The symbol <code>_WIN32</code> is defined <em>by the compiler</em> to indicate that this is a (32-bit) Windows compilation. Unfortunately, for historical reasons, it is also defined for 64-bit compilation.</p>
<p>The symbol <code>_WIN64</code> is defined <em>by the compiler</em> to indicate that this is a 64-bit Windows compilation.</p>

<p>Thus:</p>

<p>To identify unambiguously whether the compilation is 64-bit Windows one tests only <code>_WIN64</code> as in:</p>

<pre class="programlisting">
  #if defined(_WIN64)
  /* Is Windows 64-bit */
  #else
  /* Is not Windows 64-bit */
  #endif</pre>

<p>To identify unambiguously whether the compilation is 32-bit Windows one tests both <code>_WIN32</code> and <code>_WIN64</code> as in:</p>

<pre class="programlisting">
  #if defined(_WIN32) &amp;&amp; \
     !defined(_WIN64)
  /* Is Windows 32-bit */
  #else
  /* Is not Windows 32-bit */
  #endif</pre>
  
<p>To identify unambiguously whether the compilation is one or the other form of Windows one tests both <code>_WIN32</code> and <code>_WIN64</code> as in:</p>

<pre class="programlisting">
  #if defined(_WIN64)
  /* Is Windows 64-bit */
  #elif defined(_WIN32)
  /* Is Windows 32-bit */
  #else
  /* Not Windows */
  #endif</pre>

<h3>WIN32 and WIN64</h3>

<p>The symbol <code>WIN32</code> is defined by the user to indicate whatever the user chooses it to indicate. By convention, the definition of this symbol indicates a 32-bit Windows compilation, and nothing else! Microsoft (and other) tools generate projects with this symbol defined.</p>

<p>The symbol <code>WIN64</code> is defined by the user to indicate whatever the user choose it to indicate. By convention, the definition of this symbol indicates a 64-bit Windows compilation, and nothing else!</p>

<p>When properly defined, these symbols can be used to indicate unambiguously the 32- and 64-bit Windows compilation contexts.</p>

<h3>Caution with WIN32 / WIN64</h3>

<p>Unfortunately, when duplicating a <strong>Win32</strong> project to <strong>x64</strong>, the Microsoft Visual Studio wizards do not translate <code>WIN32</code> to <code>WIN64</code>. You must remember to do this yourself, in order for the inferences given above to hold. Do not add a separate <code>WIN64</code> to the x64 configuration settings â€“ replace the existing <code>WIN32</code> with <code>WIN64</code>. (All of this can be dealt with much better by use of props files, but thatâ€™s a long article â€¦)</p>

<h3>Why bother with WIN32 / WIN64 (and not simply rely on _WIN32 / _WIN64)?</h3>

<p>There are doubtless many reasons. The reasons I adhere strictly to this are:</p>
<ul>
	<li>it is a widely adopted and meaningful convention, so adheres to the principle of least surprise [<a href="#[Raymond03]">Raymond03</a>];</li>
	<li>it facilitates the emulation of (parts of) other operating systems (e.g. UNIX [<a href="#[UNIXem]">UNIXem</a>]) while on Windows, which can be tremendously helpful when porting code.</li>
</ul>

<h2>References</h2>

<p class="bibliomixed"><a id="[Raymond03]"></a>[Raymond03] Eric S. Raymond (2003) <em>The Art of UNIX Programming</em> Addison-Wesley, 2003</p>

<p class="bibliomixed"><a id="[UNIXem]"></a>[UNIXem] UNIXem is a simple, limited UNIX-API emulation library for Windows. See <a href="http://synesis.com.au/software/unixem.html">http://synesis.com.au/software/unixem.html</a></p>

<p class="bio"><span class="author"><b>Matthew Wilson</b></span> Matthew is a software development consultant and trainer for Synesis Software who helps clients to build high-performance software that does not break, and an author of articles and books that attempt to do the same.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
