{"title":"Pratik Anand","link":[{"@attributes":{"href":"https:\/\/pratikone.github.io\/atom.xml","rel":"self"}},{"@attributes":{"href":"https:\/\/pratikone.github.io\/"}}],"updated":"2021-09-02T22:42:23+00:00","id":"https:\/\/pratikone.github.io","author":{"name":"Pratik Anand","email":{}},"entry":[{"title":"Detective work on Reverse Engineering obscure 16-bit Windows game (Zombie Wars)","link":{"@attributes":{"href":"https:\/\/pratikone.github.io\/gaming\/2021\/09\/02\/reverse-engineer-zombiewars.html"}},"updated":"2021-09-02T08:30:00+00:00","id":"https:\/\/pratikone.github.io\/gaming\/2021\/09\/02\/reverse-engineer-zombiewars","content":"<p><img src=\"\/public\/images\/zw-strip.jpg\" alt=\"zwarsposter1\" title=\"Zombie Wars poster 1\" height=\"auto\" width=\"auto\" \/><\/p>\n\n<p>Zombie wars (also known as Halloween Harry 2) isn\u2019t exactly a household name in the video gaming world. Its origins are a confusing mess and despite being a pretty decent game for its time, its own claim to fame is that it is a sequel to Alien Carnage or Halloween Harry. Released in 1995 for Windows 3.1 and DOS, Zombie Wars is a straight action platformer with puzzle elements. LGR did a good <a href=\"https:\/\/www.youtube.com\/watch?v=at-zjB1KjM8\">video<\/a> reviewing the game. It is largely forgotten even by the retro gaming community, there are no fan clubs for it, or community keeping it alive. The company which created it  <a href=\"https:\/\/www.mobygames.com\/company\/gee-whiz-entertainment\">Gee Whiz<\/a> ceased to exist by the end of last millenium and the franchise is now abandonware category. That\u2019s a shame because it is a fun and fantastic game and I love it.  <br \/>\nHere is LGR\u2019s review of Zombie Wars\n<a href=\"https:\/\/www.youtube.com\/watch?v=at-zjB1KjM8\" height=\"auto\" width=\"50%\"><img src=\"https:\/\/img.youtube.com\/vi\/at-zjB1KjM8\/0.jpg\" alt=\"LGR youtube video on Zombie Wars\" \/><\/a><\/p>\n\n<h1 id=\"zombie-wars-in-my-life\">Zombie Wars in my life<\/h1>\n<p>I grew up playing this and even without the rose-tinted nostalgia glasses, there is a lot of fun to be found here, even 20 years later. As a kid growing up in the early 2000s in India in a tier-2 city (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Ranchi\">Ranchi, Jharkhand<\/a>), gaming avenues were limited for me. There were barely any stores which sold video games and those which did, the games were priced as much as family home\u2019s monthly rent. As a result, pirated game CDs were the only way to experience video games. As Gabe Newell has famously said : Piracy is an issue of service, not price.<\/p>\n\n<p>This game is special because it was my first legally bought game. My sister had gifted it to me on my birthday from her own pocket money, from a music store. It costed <code class=\"language-plaintext highlighter-rouge\">\u20b9 250<\/code> back in 2002 (<code class=\"language-plaintext highlighter-rouge\">\u20b9 843<\/code> in 2021, adjusted for inflation), costly but not as costly as other modern games of the time which costed around <code class=\"language-plaintext highlighter-rouge\">\u20b9 1500- 2000<\/code>. Once I got my hands on it, I loved it. I played it again and again, figured out its secrets, level designs etc. The artwork and music were top notch. The sewer levels, the 3d-esque sprites and the view of city skyline in all their pixel art glory, were memorable. I still have the original CD in my parents house though it no longer boots because of decay.<\/p>\n\n<p>I always wanted to revisit this game and know more about it, look under the hood and make it playable on modern systems. The lack of will and experience prevented me. In the last 2 years, I started contributing features (webassembly port, widescreen etc) to <a href=\"https:\/\/github.com\/lethal-guitar\/RigelEngine\">Rigel Engine<\/a> : an open source reimplementation of Duke Nukem II. It is done by <a href=\"https:\/\/twitter.com\/lethal_guitar\">@lethal_guitar<\/a> who did it after reverse engineering the <a href=\"https:\/\/lethalguitar.wordpress.com\/2019\/05\/\">game logic<\/a>. I wanted to explore something similar for Zombie Wars, find how it works and revive it for the modern times. As said earlier, there are no fanclubs for doing it , so I guess,  I could be that one fan who does it. \ud83d\ude01<\/p>\n\n<p><img src=\"\/public\/images\/zw-cv2.gif\" alt=\"zwarsposter2\" title=\"Another Zombie Wars poster\" height=\"auto\" width=\"50%\" \/><\/p>\n\n<h1 id=\"running-the-game\">Running the game<\/h1>\n<p>First step was to get the game to run on modern hardware and OS. It doesn\u2019t run on modern Windows 10 \/ Windows 11. The only suggestion I found on the internet was to use a Dos version of the game in Dosbox which was cumbersome. Also, not all game copies are created equal. I found multiple entries of the game on <a href=\"https:\/\/archive.org\">archive.org<\/a> but only a couple even ran. Others flat out refused to even run the installer. For those which ran, even garbled, provided a ray of hope. I needed to get it running on modern Windows with the least effort.<\/p>\n\n<p>I stumbled across otvdm or winevdm. <a href=\"http:\/\/www.columbia.edu\/~em36\/otvdm.html\">Otvdm<\/a> is a port of WINE on Windows. This is some next level inception stuff. WINE was created to run Windows software in Linux. And now, it is being used to run old Windows software in Windows itself. \ud83e\udd74  After days of trying to run the game through various means, following guides, the moment I found <code class=\"language-plaintext highlighter-rouge\">otvdm<\/code>, it booted the game up like magic. No configuration needed. The game ran perfectly, sounded perfectly and worked. I played 2 levels straightway and it was still fun. I\u2019ve updated <a href=\"https:\/\/www.pcgamingwiki.com\/wiki\/Zombie_Wars\">pcgamingwiki<\/a> entry for it.<\/p>\n\n<p><img src=\"\/public\/images\/zw-gplay1.png\" alt=\"zwarsgameplay1\" title=\"Zombie Wars running in Windows 11 thanks to otvdm\" height=\"auto\" width=\"50%\" \/><\/p>\n\n<p><img src=\"\/public\/images\/zw-gplay2.png\" alt=\"zwarsgameplay2\" title=\"Zombie Wars running in Windows 11 thanks to otvdm\" height=\"auto\" width=\"50%\" \/><\/p>\n\n<h1 id=\"historical-context\">Historical context<\/h1>\n<p><img src=\"\/public\/images\/gw_logo2.gif\" alt=\"geewhiz\" title=\"Gee Whiz logo\" height=\"auto\" width=\"50%\" \/><\/p>\n\n<p>Now that I had the game running, I wanted to look under the hood : how the game was coded, what kind of assets it has, where I can find the awesome game music etc. Since it is not a popular game, quick Google searches resulted in nothing important.   <br \/>\nThe first step I took was to gather as much historical context about the game as possible. When it was made, where it was made, what was the history of its developers, what previous games they made. A lot of good guesses can be made about the technology if you know the historical context.<\/p>\n\n<p><img src=\"\/public\/images\/zw-website.png\" alt=\"geewhiz-website\" title=\"Gee Whiz Zombie Wars website from 1997\" height=\"auto\" width=\"auto%\" \/><\/p>\n\n<p>Here, the <strong>Internet Archive Wayback Machine<\/strong> came handy. The game\u2019s <a href=\"https:\/\/web.archive.org\/web\/19970327033257\/http:\/\/www.geewhiz.com.au\/gwzombie.html\">website<\/a> was down even when I got the game in 2002 so I knew the website closed way before that.  I searched and found the <a href=\"https:\/\/web.archive.org\/web\/19970327033257\/http:\/\/www.geewhiz.com.au\/gwzombie.html\">website<\/a> existed till 1999. There was a lot of cool artwork for Zombie wars (which they called Halloween Harry 2 but never used that name officially). There were a lot of artwork for the planned <strong>Halloween Harry 3<\/strong> but it never came to fruition. The company also planned to do some new games in 3D in spirit of Monkey Island and Super Mario 64. <a href=\"(https:\/\/www.mobygames.com\/company\/gee-whiz-entertainment)\">Mobygames<\/a> provides a better insight there.<\/p>\n\n<p><img src=\"\/public\/images\/diane-hh3.jpg\" alt=\"diane\" title=\"Unreleased Diane artwork from Halloween Harry 3\" height=\"auto\" width=\"80%\" \/><\/p>\n\n<p>Few insights I got from this historical context (eventually, not immediately) :<\/p>\n\n<ol>\n  <li>\n    <p>Halloween Harry 1 (HH 1) was an old game from 80s which was updated and re-released as Alien Carnage in 1993 by Apogee. It is now freely distributed by 3D realms. It means that the tech for HH 2 might not be an extension of HH 1\u2019s tech because it was old which seemed to be true later as image formats don\u2019t match, neither do audio formats.<\/p>\n  <\/li>\n  <li>\n    <p>The game was developed by 1-2 developers only so expecting a high level language and esoteric formats - non standardized\nThe game was first released for DOS and then used Wing (Windows Graphics library, precursor to DirectX days) to bring it to <em>Windows 3.10<\/em>. Thus, I could expect a lot of DOS code and standard practices.\nThe sign on the asset file and their extension is <code class=\"language-plaintext highlighter-rouge\">sub0<\/code> : a nod to the previous name of the company which was Sub zero software (and maybe a nod to Street Fighter games ?). Thus, I didn\u2019t need to dig around the internet trying to find <code class=\"language-plaintext highlighter-rouge\">sub0<\/code> format because it was entirely custom.<\/p>\n  <\/li>\n  <li>\n    <p>Which music synthesizer was used to create the midi files - <a href=\"https:\/\/www.polynominal.com\/yamaha-opl3\/\">Yamaha OPL 3<\/a> from 1993<\/p>\n  <\/li>\n<\/ol>\n\n<h1 id=\"getting-started-with-reverse-engineering\">Getting started with reverse engineering<\/h1>\n<p>Armed with this knowledge, I started on the journey. I started focusing on tools for reverse engineering dos games. A lot of people have guides and tools for it. <a href=\"https:\/\/moddingwiki.shikadi.net\/wiki\/Main_Page\">Moddingwiki<\/a> is a good resource, so is <a href=\"https:\/\/www.reddit.com\/r\/ReverseEngineering\/\">r\/ReverseEngineering<\/a> subreddit.<\/p>\n\n<p>There are user-made tools which can \u201cunpack\u201d game resources or atleast find out what compression algorithms are used in packing those DOS games (like Universal Unpacker, UPX). In my case, they mostly didn\u2019t work or could find signatures of DOS era popular compression algorithms\/software like <code class=\"language-plaintext highlighter-rouge\">ASPack<\/code>, <code class=\"language-plaintext highlighter-rouge\">LZEXE<\/code>, <code class=\"language-plaintext highlighter-rouge\">PKLite<\/code>. Most popular RE tools (decompilers, debuggers, unpackers) don\u2019t support this niche of games - 16-bit Windows game, they either support later 32 bit games or older 16-bit DOS games. Thus, tools were not useful largely. The only hint I got was the format of the game binary through its signature : <em>NE<\/em> or <em>New Executable<\/em> i.e. it is a 16-bit Windows binary (which I already knew because of knowing its historical context). This is pure 16-bit Window 3.10 goodness.<\/p>\n\n<p>Then through some modding site, I stumbled across <a href=\"https:\/\/www.szevvy.com\/\">Wombat<\/a> - a multi game asset extractor tool which could extract music, graphics and other assets from a dozen games. It supported Alien Carnage and yes.. Zombie Wars too.  It listed partial support only but better than nothing.  Wombat underestimated its own capabilities : it actually was able to extract almost all the graphics of the game by unpacking GFX.SB0 and audio from SFX.SB0. It only had difficulty with certain sprites and level files (<code class=\"language-plaintext highlighter-rouge\">.h95<\/code>, maybe for Harry \u201895 ?) for the game. I knew SB0 was a custom file type but this helped me understand it even better. I got the list of images in <a href=\"http:\/\/fileformats.archiveteam.org\/wiki\/HSI_Raw\">HSI RAW format<\/a><\/p>\n\n<p>And music files in midi format and wave. I discovered the Yamaha OPL 3 FluidSynth SoundFront file to play it is from 1993 because of historical context. Crazy how a midi sound <a href=\"https:\/\/www.youtube.com\/watch?v=JiZOxUhQj10\">changes<\/a> depending on the audio hardware. A lot of fun.<\/p>\n\n<p>Listen this to the peppy <a href=\"https:\/\/archive.org\/details\/zombiewars\/GAME1.mp3\">main track \u25b6\ufe0f<\/a>.<\/p>\n\n<p>The game\u2019s assets are :<\/p>\n\n<ul>\n  <li>HSI RAW files for images used in cutscenes and dialogues, credits and menu<\/li>\n  <li>SPR sprite atlas files<\/li>\n  <li>WAV files for game\u2019s sound effects<\/li>\n  <li>Midi files for game\u2019s music<\/li>\n  <li>.H95 level files which it cannot decode<\/li>\n<\/ul>\n\n<p><img src=\"\/public\/images\/wombat1.png\" alt=\"wombat\" title=\"Wombat is a multi game asset extraction tool\" height=\"auto\" width=\"auto\" \/><\/p>\n\n<p>I was excited. I found the treasure of my childhood games which I could play on and on, especially the music. I wanted to know how to do it myself. Wombat is closed source and didn\u2019t provide anything on how it is doing what it is doing (though its script folder provided a lot of hints).<\/p>\n\n<h1 id=\"reverse-engineering-the-binary\">Reverse engineering the binary<\/h1>\n\n<p>I knew these packed files were custom and could be compressed, I started with .EXE file ZWARS.exe to see how it is extracting resources. The first step of RE is to find how to extract resources. <a href=\"https:\/\/docs.microsoft.com\/en-us\/sysinternals\/downloads\/strings\">String utility<\/a> came very handy. It just dumps out all strings it could find in a binary but that is important. Many compilers leave their name as a string in binary so I could get that information. And yes, I found it. Delphi 2.0  in the strings. Delphi !! That meant it uses Object Pascal with <a href=\"https:\/\/en.wikipedia.org\/wiki\/Delphi_(software)\">Delphi IDE<\/a>. Success \u2026<\/p>\n\n<p>My excitement dampened when  I found that none of the modern state of the art RE tools can reverse engineer 16-bit delphi code properly yet. It was kind of sad, I tried IDA Free, NSA Ghidra, Reko and few others to limited success. Even with these limitations, I was able to identify the main game loop. Given that my assembly and binary experience is rudimentary at best, I was very happy with myself.<\/p>\n\n<p>I gave up on trying to reverse engineer the binary and started to focus on the asset file. I kind of had the key to solving that mystery, the output from wombat. I just had to reverse engineer how Wombat was reverse engineering and extracting the asset files <code class=\"language-plaintext highlighter-rouge\">.SUB0<\/code>.  Wombat has a scripts folder to select a script for each game and its filetype. It had the learnings of the author on how to decode each file. I used it as a reference even though it was incomplete. \nThe game creator used a very simple technique to pack the game and reverse engineering it was a lot of fun. HxD is a good binary viewing tool and Wombat helped in verifying my assumptions.<\/p>\n\n<p><img src=\"\/public\/images\/zw-hex.png\" alt=\"hex\" title=\"Binary view\" height=\"auto\" width=\"auto%\" \/><\/p>\n\n<p>The asset binary file followed this pattern :<\/p>\n<ul>\n  <li>A signature (12 bytes)<\/li>\n  <li>A list comprising of file names (exact 12 bytes), its offset in memory to access it (4 bytes), its size (4 bytes), 1 bit of unknown data (i don\u2019t know what it is used for but can be safely ignored)<\/li>\n  <li>Ending signature (8 bytes)<\/li>\n  <li>Binary blob to be accessed by offset<\/li>\n<\/ul>\n\n<p>Example : if file names are <code class=\"language-plaintext highlighter-rouge\">harry.RAW<\/code>, offset 4, size 20 and <code class=\"language-plaintext highlighter-rouge\">diane.RAW<\/code>, offset 10, size 20 the file names need to be 12 bytes long. If a filename is shorter than 12 bytes, filler content is used. In this case, harry.raw becomes <code class=\"language-plaintext highlighter-rouge\">harry.RAWRAW<\/code> and <code class=\"language-plaintext highlighter-rouge\">diane.RAWRAW<\/code>. And stored like<\/p>\n<ul>\n  <li>\n    <p><code class=\"language-plaintext highlighter-rouge\">harry.RAWRAW<\/code> [12 in big endian] [20 in big endian] [1 bit of unknown data]<\/p>\n  <\/li>\n  <li>\n    <p><code class=\"language-plaintext highlighter-rouge\">diane.RAWRAW<\/code> [12 in big endian] [20 in big endian] [1 bit of unknown data]<\/p>\n  <\/li>\n  <li>\n    <p>[8 byte of signature to indicate end of the list]<\/p>\n  <\/li>\n  <li>\n    <p>Binary blog starting with contents of <code class=\"language-plaintext highlighter-rouge\">harry.RAWRAW<\/code> starting with signature <code class=\"language-plaintext highlighter-rouge\">mhwanh<\/code> because HSI RAW format start with that sign<\/p>\n  <\/li>\n<\/ul>\n\n<p>The offset as well as size were stored in big endian than usual little endian format. Maybe it was an obfuscation technique ?  It was a surprising discovery which was missing in scripts. Once found, I was quickly able to develop an algorithm to extract it. \nOnce the algorithm became clear, I wanted to test it out using my own script. I wrote a python script to extract all the files. Used <a href=\"https:\/\/entropymine.com\/deark\/\"><code class=\"language-plaintext highlighter-rouge\">deark<\/code><\/a> to turn RAW images to something else. These are non standard raw so more guesswork was needed but it was done.<\/p>\n\n<p><img src=\"\/public\/images\/zw-extracted.png\" alt=\"hex\" title=\"Binary view\" height=\"auto\" width=\"auto%\" \/><\/p>\n\n<p>You can access the python script here : <a href=\"https:\/\/gist.github.com\/pratikone\/f0fbd11c3e16a4e852e9c0bbef891b73\">https:\/\/gist.github.com\/pratikone\/f0fbd11c3e16a4e852e9c0bbef891b73<\/a><\/p>\n\n<blockquote>\n  <p>There are still image formats which I am unable to decode (<code class=\"language-plaintext highlighter-rouge\">.SPR<\/code>, <code class=\"language-plaintext highlighter-rouge\">.BIN<\/code>) as well as game level files (<code class=\"language-plaintext highlighter-rouge\">.H95<\/code>). If interested,  people are welcome to contribute to this open-source script and help in digital preservation of this game.<\/p>\n<\/blockquote>\n\n<h1 id=\"my-learnings-\">My learnings :<\/h1>\n<p>This whole project has been a super fun experience. This is my first foray into the world of reverse engineering retro games and I have gained a lot of knowledge. I became aware of specialized tools, modding communities and multiple video game archiving sites. I also got a good historical insight to the state of dos game development in early 90s. <br \/>\nOn technical side of things, my hex reading got stronger \ud83d\ude01. For the future, I will be increasing my assembly code understanding skills to mount another attempt at reverse engineering the game logic.<\/p>\n\n<p>I have uploaded all the files to <a href=\"https:\/\/archive.org\/details\/zombiewars\"><strong>Internet Archive<\/strong><\/a> for digital preservation, including original game files (as they are now abandonware software, you cannot buy them anywhere) as well as game assets, tools and the <a href=\"https:\/\/gist.github.com\/pratikone\/f0fbd11c3e16a4e852e9c0bbef891b73\"> asset extraction script<\/a> I have developed. Overall, this has been a good start and I plan to make future efforts to understand more of its game logic.<\/p>\n\n<h2 id=\"links\">Links<\/h2>\n\n<ul>\n  <li>My asset extraction script : <a href=\"https:\/\/gist.github.com\/pratikone\/f0fbd11c3e16a4e852e9c0bbef891b73\">https:\/\/gist.github.com\/pratikone\/f0fbd11c3e16a4e852e9c0bbef891b73<\/a><\/li>\n  <li>My Internet Archive page for Zombie Wars and extracted assets : <a href=\"https:\/\/archive.org\/details\/zombiewars\">https:\/\/archive.org\/details\/zombiewars<\/a><\/li>\n  <li><a href=\"http:\/\/blog.ssokolow.com\/archives\/2018\/12\/02\/\">http:\/\/blog.ssokolow.com\/archives\/2018\/12\/02\/resources-for-reverse-engineering-16-bit-applications\/<\/a><\/li>\n  <li>Modding wiki page : <a href=\"https:\/\/moddingwiki.shikadi.net\/wiki\/Main_Page\">https:\/\/moddingwiki.shikadi.net\/wiki\/Main_Page<\/a><\/li>\n  <li>PCGamingWiki : <a href=\"https:\/\/www.pcgamingwiki.com\/wiki\/Zombie_Wars\">https:\/\/www.pcgamingwiki.com\/wiki\/Zombie_Wars<\/a><\/li>\n  <li>Wayback machine to gee whiz website : <a href=\"https:\/\/web.archive.org\/web\/19970327033257\/http:\/\/www.geewhiz.com.au\/gwzombie.html\">https:\/\/web.archive.org\/web\/19970327033257\/http:\/\/www.geewhiz.com.au\/gwzombie.html<\/a><\/li>\n<\/ul>\n"},{"title":"Does C have a runtime ?","link":{"@attributes":{"href":"https:\/\/pratikone.github.io\/c\/2020\/12\/22\/does-C-have-a-runtime.html"}},"updated":"2020-12-22T21:29:37+00:00","id":"https:\/\/pratikone.github.io\/c\/2020\/12\/22\/does-C-have-a-runtime","content":"<p><img src=\"\/public\/images\/speech_bubble1.png\" alt=\"bubble\" title=\"Image source : https:\/\/pixabay.com\/illustrations\/speech-bubble-thinking-talk-5775045\/\" height=\"auto\" width=\"30%\" \/><\/p>\n\n<p>Java has a runtime. We have seen countless times to install or update JRE - Java Runtime environment. Python too has runtime. It is provided by the python executable. But what about C ? Does it have a runtime ? I have never come across a C runtime installer. How about C++ ?<br \/>\nThe short answer is <strong>YES<\/strong>. <br \/>\nC has a runtime, albeit a tiny one and C++ also has a runtime. We don\u2019t notice it because it is usually provided by the OS itself and with every new OS update, it gets updated. There is a small exception with that (VC runtime library on Windows) but we will talk about it later. Let\u2019s get to the basics.<\/p>\n\n<h2 id=\"what-is-c-runtime-crt-\">What is C runtime (CRT) ?<\/h2>\n<p>Whenever we refer to C runtime, we usually mean one or both of these things:<\/p>\n<ol>\n  <li><em>Bootstrap and Runtime environment<\/em>  : It sets up the necessary bootstrapping code which tells the OS how to run this binary code, followed by code for setting up the runtime environment for executing user code. It is included as inline assembly in the compiled binary itself.<\/li>\n  <li><em>Runtime library<\/em> : it contains functions to interact with the OS; functions to allocate memory, read and write to files, network etc.<\/li>\n<\/ol>\n\n<h3 id=\"bootstrap-and-c-runtime-environment\">Bootstrap and C runtime environment<\/h3>\n<p>As the name suggests, this is the code which sets up the execution point for the program for the OS to call. It defines the structure of functions, setup call stack and its calling convention (_stdcall, __cdecl ). This is the most important and necessary code to run our program in the OS.  It is specific to the OS version and doesn\u2019t usually change but gets updated by the OS itself during its update. Among those dozens of Windows update we receive regularly, some of them update the CRT too.<\/p>\n\n<p><img src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2018\/06\/img_5b2d86d471d92.png\" alt=\"winupdate\" title=\"Please wait while Windows is updating ...\" height=\"auto\" width=\"50%\" \/><\/p>\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Crt0\"><code class=\"language-plaintext highlighter-rouge\">crt0<\/code><\/a> is a special file which gets linked to every program during the linking phase of compilation. It has basic program loading routines which instruct the compiler on how to call this program\u2019s <code class=\"language-plaintext highlighter-rouge\">main()<\/code> function and even exit the running program. Like everything else in the C and C++ ecosystem, there is a flexibility of choice on including it. If one wants to write his\/her own startup code, they can avoid linking to crt0 altogether (<a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Link-Options.html\">nostartfiles<\/a> in GCC) and just provide their code in <code class=\"language-plaintext highlighter-rouge\">_start<\/code> label in assembly (<a href=\"https:\/\/stackoverflow.com\/questions\/29694564\/what-is-the-use-of-start-in-c\"><code class=\"language-plaintext highlighter-rouge\">_start<\/code> calls <code class=\"language-plaintext highlighter-rouge\">main()<\/code> of user code<\/a>). \nIn case of C++, <code class=\"language-plaintext highlighter-rouge\">crt1<\/code> can be linked alternatively,  which provides some extra functionality for constructor and destructors.<\/p>\n\n<h3 id=\"c-runtime-library\">C runtime library<\/h3>\n<p>The runtime library or standard library (<code class=\"language-plaintext highlighter-rouge\">stdlib<\/code>) contains a series of useful functions like <code class=\"language-plaintext highlighter-rouge\">malloc<\/code>, <code class=\"language-plaintext highlighter-rouge\">free<\/code>, <code class=\"language-plaintext highlighter-rouge\">printf<\/code>, <code class=\"language-plaintext highlighter-rouge\">memcpy<\/code> (<a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/c-runtime-library\/reference\/crt-alphabetical-function-reference?view=msvc-160\">MSVC<\/a>, <a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Other-Builtins.html#Other-Builtins\">GCC<\/a> ) for interacting with the OS which almost every program needs. This part can be statically or dynamically linked. Windows provides libcmt.lib for C and libcpmt.lib for C++. \nIf one chooses to link it statically, only the code called by the user\u2019s program gets added to its binary, increasing the size. Removing stdlib can significantly reduce the binary size, which could be as significant as 90% in tiny programs. If one has to create the C code binary with smallest size, this would be the key step in reducing binary size to ~ 1 kb.<\/p>\n\n<p>If one dynamically links to CRT, this size bump is not there (it is standard for programs to dynamically link to this library for multiple reasons like compatibility with other dll, apart from small size saving). Every OS provides an implementation. <code class=\"language-plaintext highlighter-rouge\">UCRT.dll<\/code> on Windows and <code class=\"language-plaintext highlighter-rouge\">libc.so<\/code> (GLibc) on Linux.<\/p>\n\n<blockquote>\n  <p>Normal dynamic loading of dll rule applies : If the program loads two different versions of CRT dll, they won\u2019t share global variables. Not taking care of this fact could cause very subtle bugs in multi-dll codebases,  in calling runtime functions which maintain any state.<\/p>\n<\/blockquote>\n\n<p>A user code cannot be faster than its runtime library\u2019s performance.\nRuntime libraries keep getting improvements through updates and one can receive those fixes as part of VC++Runtime updates on Windows or libc updates on Linux. Programs compiled with a specific CRT lib may expect that specific or compatible CRT dll. Thus, it is possible to keep side by side copies of different versions of CRT dlls (<a href=\"https:\/\/en.wikipedia.org\/wiki\/DLL_Hell\">DLL hell ?<\/a>).<\/p>\n\n<h2 id=\"all-you-need-is-c\">All you need is C<\/h2>\n<p>C runtime is an interesting piece of software - it depends not only on OS and architecture but also on compiler. To give an example, runtime code generated on 64 bit windows by GCC would be different from that generated by MSVC. \nIf you want to port C to a brand new OS,  all you need apart from the compiler is a working C runtime and that\u2019s it, C gets ported.<\/p>\n\n<p>Thanks for reading this far. For the last couple of months, I have been interested in inner workings of C and C++ ecosystem. This is an article in that direction. Tweet any suggestions or corrections to me at <a href=\"https:\/\/twitter.com\/pratikone\">@pratikone<\/a><\/p>\n\n<h2 id=\"references\">References<\/h2>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/2709998\/crt0-o-and-crt1-o-whats-the-difference\">https:\/\/stackoverflow.com\/questions\/2709998\/crt0-o-and-crt1-o-whats-the-difference<\/a><\/p>\n\n<p><a href=\"http:\/\/www.vishalchovatiya.com\/crt-run-time-before-starting-main\">http:\/\/www.vishalchovatiya.com\/crt-run-time-before-starting-main<\/a><\/p>\n\n<p><a href=\"https:\/\/blogs.oracle.com\/linux\/hello-from-a-libc-free-world-part-1-v2\">https:\/\/blogs.oracle.com\/linux\/hello-from-a-libc-free-world-part-1-v2<\/a><\/p>\n\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/c-runtime-library\/c-run-time-library-reference?view=msvc-160\">https:\/\/docs.microsoft.com\/en-us\/cpp\/c-runtime-library\/c-run-time-library-reference?view=msvc-160<\/a><\/p>\n\n<p><a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Other-Builtins.html#Other-Builtins\">https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Other-Builtins.html#Other-Builtins<\/a><\/p>\n"},{"title":"Anatomy of Windows Hello World program","link":{"@attributes":{"href":"https:\/\/pratikone.github.io\/c++\/2020\/06\/07\/anatomy-of-windows-hello-world.html"}},"updated":"2020-06-07T21:29:37+00:00","id":"https:\/\/pratikone.github.io\/c++\/2020\/06\/07\/anatomy-of-windows-hello-world","content":"<p>Windows programming is in state of constant change since the introduction of Win32 api - whether it is move to .NET, again to WPF, introduction of Modern Apps which then got changed to UWP and now finally <a href=\"https:\/\/github.com\/microsoft\/ProjectReunion\">Project Reunion<\/a>. One thing that hasn\u2019t changed at all in this is how to write a hello world program in Windows. It has remained the same since 1995 when Windows 95 was released. However, a lot has changed internally. Windows does a lot of work under the hood to make sure the simple hello world continues to work even after 25 years and large scale OS changes. This blog attempts to go deeper into the hello world to showcase the rich technical background of windows hello world with a bit of history.  <br \/>\n<img src=\"https:\/\/miro.medium.com\/max\/1235\/0*HDBMMdR2g2sWh_TU.jpg\" alt=\"logos of all Windows versions\" title=\"All windows versions\" \/><\/p>\n<h1 id=\"why-modern-windows-api-are-still-called-win32-\">Why modern windows api are still called Win32 ?<\/h1>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Windows_API\">Win32 api<\/a> are the system level api for programming Windows at the lowest level. When Windows transitioned to 32-bits system with Windows 95, they wanted a way to distinguish these new 32-bit apis from existing windows api which were 16-bit only. That\u2019s how the name Win32 was chosen for these new apis. Windows 95 was a mega hit and the lot of developers started using those apis to develop for Windows. More those api became popular, it became harder and harder to rename them. Changing to Win64 for 64-bit Windows or just Windows Api would have caused a whole lot more confusion and change of documents to remove 32 from the name. <em>The name Win32 got stuck.<\/em> Every <a href=\"https:\/\/en.wikipedia.org\/wiki\/Windows_API\">Windows api<\/a> is now Win32, whether it is 32-bit, 64-bit or any future foo-quantum-bit.<\/p>\n\n<h1 id=\"hello-world\">Hello World<\/h1>\n<p>The hello world code for Win32 has largely unchanged since its inception except when the <a href=\"http:\/\/www.charlespetzold.com\/blog\/2014\/12\/The-Infamous-Windows-Hello-World-Program.html\">infamous 3 pages long hello world program<\/a> got replaced with a shorter version because it was intimidating to beginners. This anatomy is not of that infamous code but the successor since Win95 days which is decent sized and captures a lot of Windows functionality in itself. Let\u2019s start by comparing this hello world with its console counterpart.<\/p>\n\n<h2 id=\"console-hello-world\">Console hello world<\/h2>\n\n<div class=\"language-c++ highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"cp\">#include &lt;iostream&gt;\n<\/span>\n<span class=\"kt\">int<\/span> <span class=\"nf\">main<\/span><span class=\"p\">()<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"n\">std<\/span><span class=\"o\">::<\/span><span class=\"n\">cout<\/span> <span class=\"o\">&lt;&lt;<\/span> <span class=\"s\">\"Hello World!<\/span><span class=\"se\">\\n<\/span><span class=\"s\">\"<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>That is pretty straight-forward. You have the IO header, entry point is main() and it calls cout in std namespace which prints hello world. Compare it to Win32 hello world. This code is directly from official <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/learnwin32\/windows-hello-world-sample\">MSDN Win32 hello world<\/a> tutorial series presented unchanged.<\/p>\n\n<h2 id=\"windows-hello-world\">Windows hello world<\/h2>\n\n<div class=\"language-c++ highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"cp\">#ifndef UNICODE\n#define UNICODE\n#endif\n<\/span>\n<span class=\"cp\">#include &lt;windows.h&gt;\n<\/span>\n<span class=\"n\">LRESULT<\/span> <span class=\"n\">CALLBACK<\/span> <span class=\"nf\">WindowProc<\/span><span class=\"p\">(<\/span><span class=\"n\">HWND<\/span> <span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"n\">UINT<\/span> <span class=\"n\">uMsg<\/span><span class=\"p\">,<\/span> <span class=\"n\">WPARAM<\/span> <span class=\"n\">wParam<\/span><span class=\"p\">,<\/span> <span class=\"n\">LPARAM<\/span> <span class=\"n\">lParam<\/span><span class=\"p\">);<\/span>\n\n<span class=\"kt\">int<\/span> <span class=\"n\">WINAPI<\/span> <span class=\"nf\">wWinMain<\/span><span class=\"p\">(<\/span><span class=\"n\">HINSTANCE<\/span> <span class=\"n\">hInstance<\/span><span class=\"p\">,<\/span> <span class=\"n\">HINSTANCE<\/span><span class=\"p\">,<\/span> <span class=\"n\">PWSTR<\/span> <span class=\"n\">pCmdLine<\/span><span class=\"p\">,<\/span> <span class=\"kt\">int<\/span> <span class=\"n\">nCmdShow<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"c1\">\/\/ Register the window class.<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">wchar_t<\/span> <span class=\"n\">CLASS_NAME<\/span><span class=\"p\">[]<\/span>  <span class=\"o\">=<\/span> <span class=\"s\">L\"Sample Window Class\"<\/span><span class=\"p\">;<\/span>\n    \n    <span class=\"n\">WNDCLASS<\/span> <span class=\"n\">wc<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">};<\/span>\n\n    <span class=\"n\">wc<\/span><span class=\"p\">.<\/span><span class=\"n\">lpfnWndProc<\/span>   <span class=\"o\">=<\/span> <span class=\"n\">WindowProc<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">wc<\/span><span class=\"p\">.<\/span><span class=\"n\">hInstance<\/span>     <span class=\"o\">=<\/span> <span class=\"n\">hInstance<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">wc<\/span><span class=\"p\">.<\/span><span class=\"n\">lpszClassName<\/span> <span class=\"o\">=<\/span> <span class=\"n\">CLASS_NAME<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"n\">RegisterClass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">wc<\/span><span class=\"p\">);<\/span>\n\n    <span class=\"c1\">\/\/ Create the window.<\/span>\n\n    <span class=\"n\">HWND<\/span> <span class=\"n\">hwnd<\/span> <span class=\"o\">=<\/span> <span class=\"n\">CreateWindowEx<\/span><span class=\"p\">(<\/span>\n        <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>                              <span class=\"c1\">\/\/ Optional window styles.<\/span>\n        <span class=\"n\">CLASS_NAME<\/span><span class=\"p\">,<\/span>                     <span class=\"c1\">\/\/ Window class<\/span>\n        <span class=\"s\">L\"Learn to Program Windows\"<\/span><span class=\"p\">,<\/span>    <span class=\"c1\">\/\/ Window text<\/span>\n        <span class=\"n\">WS_OVERLAPPEDWINDOW<\/span><span class=\"p\">,<\/span>            <span class=\"c1\">\/\/ Window style<\/span>\n\n        <span class=\"c1\">\/\/ Size and position<\/span>\n        <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span> <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span> <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span> <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span>\n\n        <span class=\"nb\">NULL<\/span><span class=\"p\">,<\/span>       <span class=\"c1\">\/\/ Parent window    <\/span>\n        <span class=\"nb\">NULL<\/span><span class=\"p\">,<\/span>       <span class=\"c1\">\/\/ Menu<\/span>\n        <span class=\"n\">hInstance<\/span><span class=\"p\">,<\/span>  <span class=\"c1\">\/\/ Instance handle<\/span>\n        <span class=\"nb\">NULL<\/span>        <span class=\"c1\">\/\/ Additional application data<\/span>\n        <span class=\"p\">);<\/span>\n\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span> <span class=\"o\">==<\/span> <span class=\"nb\">NULL<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"n\">ShowWindow<\/span><span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"n\">nCmdShow<\/span><span class=\"p\">);<\/span>\n\n    <span class=\"c1\">\/\/ Run the message loop.<\/span>\n\n    <span class=\"n\">MSG<\/span> <span class=\"n\">msg<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">};<\/span>\n    <span class=\"k\">while<\/span> <span class=\"p\">(<\/span><span class=\"n\">GetMessage<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">msg<\/span><span class=\"p\">,<\/span> <span class=\"nb\">NULL<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">))<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"n\">TranslateMessage<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">msg<\/span><span class=\"p\">);<\/span>\n        <span class=\"n\">DispatchMessage<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">msg<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"n\">LRESULT<\/span> <span class=\"n\">CALLBACK<\/span> <span class=\"nf\">WindowProc<\/span><span class=\"p\">(<\/span><span class=\"n\">HWND<\/span> <span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"n\">UINT<\/span> <span class=\"n\">uMsg<\/span><span class=\"p\">,<\/span> <span class=\"n\">WPARAM<\/span> <span class=\"n\">wParam<\/span><span class=\"p\">,<\/span> <span class=\"n\">LPARAM<\/span> <span class=\"n\">lParam<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"k\">switch<\/span> <span class=\"p\">(<\/span><span class=\"n\">uMsg<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n    <span class=\"k\">case<\/span> <span class=\"n\">WM_DESTROY<\/span><span class=\"p\">:<\/span>\n        <span class=\"n\">PostQuitMessage<\/span><span class=\"p\">(<\/span><span class=\"mi\">0<\/span><span class=\"p\">);<\/span>\n        <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"k\">case<\/span> <span class=\"n\">WM_PAINT<\/span><span class=\"p\">:<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"n\">PAINTSTRUCT<\/span> <span class=\"n\">ps<\/span><span class=\"p\">;<\/span>\n            <span class=\"n\">HDC<\/span> <span class=\"n\">hdc<\/span> <span class=\"o\">=<\/span> <span class=\"n\">BeginPaint<\/span><span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">ps<\/span><span class=\"p\">);<\/span>\n\n\n\n            <span class=\"n\">FillRect<\/span><span class=\"p\">(<\/span><span class=\"n\">hdc<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">ps<\/span><span class=\"p\">.<\/span><span class=\"n\">rcPaint<\/span><span class=\"p\">,<\/span> <span class=\"p\">(<\/span><span class=\"n\">HBRUSH<\/span><span class=\"p\">)<\/span> <span class=\"p\">(<\/span><span class=\"n\">COLOR_WINDOW<\/span><span class=\"o\">+<\/span><span class=\"mi\">1<\/span><span class=\"p\">));<\/span>\n\n            <span class=\"n\">EndPaint<\/span><span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">ps<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n        <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"p\">}<\/span>\n    <span class=\"k\">return<\/span> <span class=\"n\">DefWindowProc<\/span><span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"n\">uMsg<\/span><span class=\"p\">,<\/span> <span class=\"n\">wParam<\/span><span class=\"p\">,<\/span> <span class=\"n\">lParam<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n<p>On first glance, this looks way bigger and unnecessarily complicated. But it is showcasing a lot of functionalities for creating a GUI windows application. It launches a typical rectangular UI window filled with a background color. It has a functional UI, proper keyboard and mouse support and even event processing.  Unlike other hello world programs for other platforms which are of little use outside of tutorials and first programs, this hello world is the foundation of any Win32 giant codebase out there. It teaches you all the basic things needed to program on Windows. Whether it is Photoshop or Firefox for Windows, they will have this piece of code somewhere in their giant codebases.  <br \/>\nNow that, it is out of the way, let\u2019s follow this code block by block.<\/p>\n\n<h1 id=\"anatomy\">Anatomy<\/h1>\n<div class=\"language-c++ highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"cp\">#ifndef UNICODE\n#define UNICODE\n#endif   \n#include &lt;windows.h&gt;    \n<\/span><\/code><\/pre><\/div><\/div>\n<p>Over the years, Windows programming has undergone large changes, like move from 16-bit to 32-bit api in Win95, move to NT kernel with XP and later changes for Vista and Windows 8. Any app whether the one developed in 1995 or in 2008 which calls windows.h continues to work in the latest version of Windows (excellent backward compatibility). Windows.h and Win32 apis do a lot of heavy duty work to ensure that all these apps remain compatible even when they are all including the same header.  For example, if you are developing a new app which uses a legacy component (from Windows 95 era referring to windows.h header files of that era ),  it is possible that your new component and legacy component will consume the same windows.h (from latest Windows SDK) and yet, pick up the era-appropriate functions.<\/p>\n\n<p>It is a catch-all header for most common windows system calls. Windows.h, a small file on its own, carries forward declaration for multiple headers. For this hello world code, the apis are in winuser.h and linked using user32.dll. For any other functionality, some other header might be needed. Windows.h makes it easy by acting as a <strong>\u201crouter\u201d<\/strong> for all these headers. You only need to include windows.h header and you get all this functionality. As MSDN page mentions, you can carefully select a different or smaller subset of functionality by defining few global identifiers like UNICODE here which indicates to use unicode variant of specific apis, denoted by W. <code class=\"language-plaintext highlighter-rouge\">CreateFoo<\/code> resolves to <code class=\"language-plaintext highlighter-rouge\">CreateFooW<\/code> <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/winprog\/using-the-windows-headers\">More here<\/a>.<\/p>\n\n<h2 id=\"main-function\">Main function<\/h2>\n<div class=\"language-c++ highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kt\">int<\/span> <span class=\"n\">WINAPI<\/span> <span class=\"nf\">wWinMain<\/span><span class=\"p\">(<\/span><span class=\"n\">HINSTANCE<\/span> <span class=\"n\">hInstance<\/span><span class=\"p\">,<\/span> <span class=\"n\">HINSTANCE<\/span><span class=\"p\">,<\/span> <span class=\"n\">PWSTR<\/span> <span class=\"n\">pCmdLine<\/span><span class=\"p\">,<\/span> <span class=\"kt\">int<\/span> <span class=\"n\">nCmdShow<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"c1\">\/\/ Register the window class.<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">wchar_t<\/span> <span class=\"n\">CLASS_NAME<\/span><span class=\"p\">[]<\/span>  <span class=\"o\">=<\/span> <span class=\"s\">L\"Sample Window Class\"<\/span><span class=\"p\">;<\/span>\n    \n    <span class=\"n\">WNDCLASS<\/span> <span class=\"n\">wc<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">};<\/span>\n\n    <span class=\"n\">wc<\/span><span class=\"p\">.<\/span><span class=\"n\">lpfnWndProc<\/span>   <span class=\"o\">=<\/span> <span class=\"n\">WindowProc<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">wc<\/span><span class=\"p\">.<\/span><span class=\"n\">hInstance<\/span>     <span class=\"o\">=<\/span> <span class=\"n\">hInstance<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">wc<\/span><span class=\"p\">.<\/span><span class=\"n\">lpszClassName<\/span> <span class=\"o\">=<\/span> <span class=\"n\">CLASS_NAME<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"n\">RegisterClass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">wc<\/span><span class=\"p\">);<\/span>\n\n    <span class=\"c1\">\/\/ Create the window.<\/span>\n\n    <span class=\"n\">HWND<\/span> <span class=\"n\">hwnd<\/span> <span class=\"o\">=<\/span> <span class=\"n\">CreateWindowEx<\/span><span class=\"p\">(<\/span>\n        <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>                              <span class=\"c1\">\/\/ Optional window styles.<\/span>\n        <span class=\"n\">CLASS_NAME<\/span><span class=\"p\">,<\/span>                     <span class=\"c1\">\/\/ Window class<\/span>\n        <span class=\"s\">L\"Learn to Program Windows\"<\/span><span class=\"p\">,<\/span>    <span class=\"c1\">\/\/ Window text<\/span>\n        <span class=\"n\">WS_OVERLAPPEDWINDOW<\/span><span class=\"p\">,<\/span>            <span class=\"c1\">\/\/ Window style<\/span>\n\n        <span class=\"c1\">\/\/ Size and position<\/span>\n        <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span> <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span> <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span> <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span>\n\n        <span class=\"nb\">NULL<\/span><span class=\"p\">,<\/span>       <span class=\"c1\">\/\/ Parent window    <\/span>\n        <span class=\"nb\">NULL<\/span><span class=\"p\">,<\/span>       <span class=\"c1\">\/\/ Menu<\/span>\n        <span class=\"n\">hInstance<\/span><span class=\"p\">,<\/span>  <span class=\"c1\">\/\/ Instance handle<\/span>\n        <span class=\"nb\">NULL<\/span>        <span class=\"c1\">\/\/ Additional application data<\/span>\n        <span class=\"p\">);<\/span>\n\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span> <span class=\"o\">==<\/span> <span class=\"nb\">NULL<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"n\">ShowWindow<\/span><span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"n\">nCmdShow<\/span><span class=\"p\">);<\/span>\n\n    <span class=\"c1\">\/\/ Run the message loop.<\/span>\n\n    <span class=\"n\">MSG<\/span> <span class=\"n\">msg<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">};<\/span>\n    <span class=\"k\">while<\/span> <span class=\"p\">(<\/span><span class=\"n\">GetMessage<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">msg<\/span><span class=\"p\">,<\/span> <span class=\"nb\">NULL<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">))<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"n\">TranslateMessage<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">msg<\/span><span class=\"p\">);<\/span>\n        <span class=\"n\">DispatchMessage<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">msg<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n\n    <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n<p>It is the entry point to the program. Win32 allows for 4 different entrypoints to choose from, if needed, using <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/reference\/entry-entry-point-symbol?view=vs-2019\">\/entry<\/a> linker option. On a quick glance, there are a lot of 0s and NULLs passed as arguments in functions. Many of these functions have existed since the inception of Win32 apis and have changed in their behavior. Thus, many of the arguments no longer have any effect and are left for compatibility sake. These arguments are always NULL or 0. For the rest of the arguments, flags can be combined with logical OR |.<\/p>\n<blockquote>\n  <p>The code uses a now outdated <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hungarian_notation\">hungarian notation<\/a> for variable naming where type of variable is prefixed before the name. Variable <code class=\"language-plaintext highlighter-rouge\">bvalue<\/code> indicates that it is a variable of type bool. MSDN provides a handy <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/stg\/coding-style-conventions\">guide<\/a> to read and understand variable names with these notations. <code class=\"language-plaintext highlighter-rouge\">lpsz<\/code> in <code class=\"language-plaintext highlighter-rouge\">lpszClassName<\/code> stands for long pointer (historically, a 16-bit pointer but a normal 32-bit\/64-bit pointer in modern times) to string (ending in \/0). <code class=\"language-plaintext highlighter-rouge\">lpfn<\/code> in <code class=\"language-plaintext highlighter-rouge\">lpfnWndProc<\/code> stands for function pointer. Microsoft <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/design-guidelines\/general-naming-conventions\">advises against<\/a> using hungarian notation for modern Windows programming as it adds very little value and yet makes the code much harder to read. <a href=\"https:\/\/www.joelonsoftware.com\/2005\/05\/11\/making-wrong-code-look-wrong\/\">Joel On Software<\/a> also has a good article on it.<\/p>\n<\/blockquote>\n\n<p>Another interesting artifact of the past are wParam and lParam parameters. Before Win95, Windows was 16-bit OS. wParam was WORD param which was 16-bit and lparam was LONG param which was 32-bit. With Win95,Windows moved to 32-bit era and both WORD and LONG are now 32-bit (or 64-bit based on arch) so there is no difference between wParam and lParam.  <br \/>\nHINSTANCE is another once of these - handle to an instance. This <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050418-59\/?p=35873\">blogpost by Raymond Chen<\/a> does a good job of explaining the reasoning behind it.<\/p>\n\n<h2 id=\"window-of-windows---hwnd-pronounced-as-h-wind\">Window of Windows - Hwnd (pronounced as H-wind)<\/h2>\n\n<h3 id=\"what-is-an-hwnd-\">What is an Hwnd ?<\/h3>\n<p><img src=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/learnwin32\/images\/window01.png\" alt=\"picture of a window UI\" title=\"Hwnd\" \/><\/p>\n\n<p><strong>\u201cObviously, windows are central to Windows. They are so important that they named the operating system after them.\u201d<\/strong> - <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/learnwin32\/what-is-a-window-\">Official MSDN quote<\/a>  <br \/>\nA window is a rectangular area on the screen that receives user input and displays output in the form of text and graphics. In Win32 programming convention, a window is referenced by <em>HWND<\/em> - handle to a window. A handle is a numerical value associated with that window. In the kernel, an object is created for every window with an unique id. If a window is a person, hwnd is its name.\n<img src=\"https:\/\/i.imgur.com\/zHcxvkF.jpg\" alt=\"The room movie meme about oh hi hwnd\" title=\"I haven't seen this movie nor I am going to. I will be using it for making memes though\" \/><\/p>\n\n<h3 id=\"everything-is-an-hwnd\">Everything is an HWND<\/h3>\n<p>In a true spirit of the movie Inception, every UI element in a typical hwnd is a hwnd itself (think recursions). This is achieved using child windows\/owned windows. This leads to a lot of HWNDs in a moderately complex app.\n<img src=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/learnwin32\/images\/window04.png\" alt=\"parent child relation of hwnd and child hwnds\" title=\"&quot;)\nFrameworks like XAML(WinUI), WPF have found non-HWND ways to create UI elements but it is still pervasive enough. [Spy++](https:\/\/docs.microsoft.com\/en-us\/visualstudio\/debugger\/introducing-spy-increment?view=vs-2019), bundled with Visual Studio, is a good tool to visualize that.\n![picture of Spy++](https:\/\/d2.alternativeto.net\/dist\/s\/e36616b5-49b2-e011-9727-0025902c7e73_2_full.png?format=jpg&amp;width=1600&amp;height=1600&amp;mode=min&amp;upscale=false &quot;hello world code isn't the only thing that hasn't changed in a long time\" \/><\/p>\n\n<p>Modern Windows HWNDs are hardware accelerated i.e. take help of graphics pipelines like Direct2D to draw pixels faster, using a GPU. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Desktop_Window_Manager\">Desktop Window Manager (DWM)<\/a> handles drawing pixels on modern Windows.<\/p>\n\n<p>Hwnd registration and window messaging system are the few pieces of code here which are reminiscent of very 80s Object Oriented (OO) design. Microsoft was on-board OO train very early on, even when it was not totally accepted by the industry. It was when Microsoft had been using only C for programming Windows. OO in C required a lot of weird design choices in Windows and because of backward compatibility, a lot of that design has stayed to this day.<\/p>\n\n<h3 id=\"register-the-window-class-with-os\">Register the window class with OS<\/h3>\n<div class=\"language-c++ highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ Register the window class.<\/span>\n    <span class=\"k\">const<\/span> <span class=\"kt\">wchar_t<\/span> <span class=\"n\">CLASS_NAME<\/span><span class=\"p\">[]<\/span>  <span class=\"o\">=<\/span> <span class=\"s\">L\"Sample Window Class\"<\/span><span class=\"p\">;<\/span>\n    \n    <span class=\"n\">WNDCLASS<\/span> <span class=\"n\">wc<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">};<\/span>\n\n    <span class=\"n\">wc<\/span><span class=\"p\">.<\/span><span class=\"n\">lpfnWndProc<\/span>   <span class=\"o\">=<\/span> <span class=\"n\">WindowProc<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">wc<\/span><span class=\"p\">.<\/span><span class=\"n\">hInstance<\/span>     <span class=\"o\">=<\/span> <span class=\"n\">hInstance<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">wc<\/span><span class=\"p\">.<\/span><span class=\"n\">lpszClassName<\/span> <span class=\"o\">=<\/span> <span class=\"n\">CLASS_NAME<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"n\">RegisterClass<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">wc<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Windows has a weird way of doing inheritance. You will have to register your newly created hwnd object with OS for it to start communicating with it.<\/p>\n\n<h3 id=\"create-the-window\">Create the Window<\/h3>\n<div class=\"language-c++ highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ Create the window.<\/span>\n    <span class=\"n\">HWND<\/span> <span class=\"n\">hwnd<\/span> <span class=\"o\">=<\/span> <span class=\"n\">CreateWindowEx<\/span><span class=\"p\">(<\/span>\n        <span class=\"mi\">0<\/span><span class=\"p\">,<\/span>                              <span class=\"c1\">\/\/ Optional window styles.<\/span>\n        <span class=\"n\">CLASS_NAME<\/span><span class=\"p\">,<\/span>                     <span class=\"c1\">\/\/ Window class<\/span>\n        <span class=\"s\">L\"Learn to Program Windows\"<\/span><span class=\"p\">,<\/span>    <span class=\"c1\">\/\/ Window text<\/span>\n        <span class=\"n\">WS_OVERLAPPEDWINDOW<\/span><span class=\"p\">,<\/span>            <span class=\"c1\">\/\/ Window style<\/span>\n\n        <span class=\"c1\">\/\/ Size and position<\/span>\n        <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span> <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span> <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span> <span class=\"n\">CW_USEDEFAULT<\/span><span class=\"p\">,<\/span>\n\n        <span class=\"nb\">NULL<\/span><span class=\"p\">,<\/span>       <span class=\"c1\">\/\/ Parent window    <\/span>\n        <span class=\"nb\">NULL<\/span><span class=\"p\">,<\/span>       <span class=\"c1\">\/\/ Menu<\/span>\n        <span class=\"n\">hInstance<\/span><span class=\"p\">,<\/span>  <span class=\"c1\">\/\/ Instance handle<\/span>\n        <span class=\"nb\">NULL<\/span>        <span class=\"c1\">\/\/ Additional application data<\/span>\n        <span class=\"p\">);<\/span>\n\n    <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span> <span class=\"o\">==<\/span> <span class=\"nb\">NULL<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n    <span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n<p>This code creates the HWND. We provide the window classes (we just registered) to be associated with this hwnd. Window creation api is very <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/apiindex\/windows-api-list\">rich<\/a> and can create 100s windows with different configurations and behaviors. One good example is creation of child windows. Since everything is a window. A button within a UI window is a child to that hwnd. That means the parent window can handle processing of messages for that child window. This kind of hierarchy is extremely useful. It is an implementation of inheritance concept from the OO paradigm. In this way, you can add a new window as child window of parent window and you don\u2019t have to write any additional code for its basic operations like resize, maximize, close etc.<\/p>\n\n<h3 id=\"show-the-window\">Show the window<\/h3>\n<div class=\"language-c++ highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">ShowWindow<\/span><span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"n\">nCmdShow<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre><\/div><\/div>\n<p>Unsurprisingly, it shows the window on the screen. What is the point of explicitly calling show window ? There are many cases where a window is not shown directly. It can be created with the above command and updated using UpdateWindow and when ready, shown to the screen. There is also AnimateWindow for doing 90s PPT slide like transition animations which nobody should use in modern day and age.<\/p>\n\n<h2 id=\"windows-messaging-system\">Windows messaging system<\/h2>\n<p>Windows messaging system is a event-driven system achieved using polymorphism. The OS communicates to the program by passing messages and it will call a special function in your program to give you an option to deal with those messages. These messages can be keyboard, mouse, touch events generated on interacting with your program, or OS created events like when your program is minimized, maximized or closed. \nFor this message passing model, Windows creates a single message queue for a thread which handles all the messages for HWNDs created on that thread.<\/p>\n\n<h3 id=\"message-loop\">Message loop<\/h3>\n<div class=\"language-c++ highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c1\">\/\/ Run the message loop.<\/span>\n\n    <span class=\"n\">MSG<\/span> <span class=\"n\">msg<\/span> <span class=\"o\">=<\/span> <span class=\"p\">{<\/span> <span class=\"p\">};<\/span>\n    <span class=\"k\">while<\/span> <span class=\"p\">(<\/span><span class=\"n\">GetMessage<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">msg<\/span><span class=\"p\">,<\/span> <span class=\"nb\">NULL<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"mi\">0<\/span><span class=\"p\">))<\/span>\n    <span class=\"p\">{<\/span>\n        <span class=\"n\">TranslateMessage<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">msg<\/span><span class=\"p\">);<\/span>\n        <span class=\"n\">DispatchMessage<\/span><span class=\"p\">(<\/span><span class=\"o\">&amp;<\/span><span class=\"n\">msg<\/span><span class=\"p\">);<\/span>\n    <span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n\n<p>Message loop code is what is responsible for filing up this message queue.  There has to be only one message loop per thread. This message queue is hidden and not accessible by your code. It is handled entirely by the OS. All your code can do is to remove the topmost message from this queue using <code class=\"language-plaintext highlighter-rouge\">GetMessage()<\/code> api call. This message is then translated for keyboard input so that it can handle shortcut keys and do other keyboard input processing (<a href=\"https:\/\/stackoverflow.com\/questions\/12581889\/why-exactly-translatemessage\">Read more here on TranslateMessage<\/a>). After that, it is dispatched to the handler function WndProc (discussed below).  <code class=\"language-plaintext highlighter-rouge\">GetMessage<\/code> is a blocking function so it will wait if the loop is empty. But this doesn\u2019t mean your UI will be unresponsive. An alternative to that is <code class=\"language-plaintext highlighter-rouge\">PeekMessage<\/code> function which can peek and tell if there is a message on top of the queue. Since it won\u2019t block, it is good for certain scenarios to do a <em>\u201cPeek\u201d<\/em> before a <em>\u201cGet\u201d<\/em>.<\/p>\n\n<h3 id=\"wndproc---the-most-important-function-in-your-code\">WndProc - the most important function in your code<\/h3>\n<div class=\"language-c++ highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"n\">LRESULT<\/span> <span class=\"n\">CALLBACK<\/span> <span class=\"nf\">WindowProc<\/span><span class=\"p\">(<\/span><span class=\"n\">HWND<\/span> <span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"n\">UINT<\/span> <span class=\"n\">uMsg<\/span><span class=\"p\">,<\/span> <span class=\"n\">WPARAM<\/span> <span class=\"n\">wParam<\/span><span class=\"p\">,<\/span> <span class=\"n\">LPARAM<\/span> <span class=\"n\">lParam<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n    <span class=\"k\">switch<\/span> <span class=\"p\">(<\/span><span class=\"n\">uMsg<\/span><span class=\"p\">)<\/span>\n    <span class=\"p\">{<\/span>\n    <span class=\"k\">case<\/span> <span class=\"n\">WM_DESTROY<\/span><span class=\"p\">:<\/span>\n        <span class=\"n\">PostQuitMessage<\/span><span class=\"p\">(<\/span><span class=\"mi\">0<\/span><span class=\"p\">);<\/span>\n        <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"k\">case<\/span> <span class=\"n\">WM_PAINT<\/span><span class=\"p\">:<\/span>\n        <span class=\"p\">{<\/span>\n            <span class=\"n\">PAINTSTRUCT<\/span> <span class=\"n\">ps<\/span><span class=\"p\">;<\/span>\n            <span class=\"n\">HDC<\/span> <span class=\"n\">hdc<\/span> <span class=\"o\">=<\/span> <span class=\"n\">BeginPaint<\/span><span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">ps<\/span><span class=\"p\">);<\/span>\n\n\n\n            <span class=\"n\">FillRect<\/span><span class=\"p\">(<\/span><span class=\"n\">hdc<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">ps<\/span><span class=\"p\">.<\/span><span class=\"n\">rcPaint<\/span><span class=\"p\">,<\/span> <span class=\"p\">(<\/span><span class=\"n\">HBRUSH<\/span><span class=\"p\">)<\/span> <span class=\"p\">(<\/span><span class=\"n\">COLOR_WINDOW<\/span><span class=\"o\">+<\/span><span class=\"mi\">1<\/span><span class=\"p\">));<\/span>\n\n            <span class=\"n\">EndPaint<\/span><span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">ps<\/span><span class=\"p\">);<\/span>\n        <span class=\"p\">}<\/span>\n        <span class=\"k\">return<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span>\n\n    <span class=\"p\">}<\/span>\n    <span class=\"k\">return<\/span> <span class=\"n\">DefWindowProc<\/span><span class=\"p\">(<\/span><span class=\"n\">hwnd<\/span><span class=\"p\">,<\/span> <span class=\"n\">uMsg<\/span><span class=\"p\">,<\/span> <span class=\"n\">wParam<\/span><span class=\"p\">,<\/span> <span class=\"n\">lParam<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre><\/div><\/div>\n<p><code class=\"language-plaintext highlighter-rouge\">WndProc<\/code> is the special function which has to be present in every Win32 program code, either directly or indirectly.  It is the function which Windows OS calls whenever it has to communicate anything with your running code. \nIt usually has a giant switch statement for handling window messages like <code class=\"language-plaintext highlighter-rouge\">WM_DESTROY<\/code> (what to do when a user clicks on the small x on the top-right of the window). It can choose to ignore it and it won\u2019t close the window. Thankfully, there are other ways to close a window. This shows the level of control and flexibility Windows OS provides to developers which can be beneficial but can also be misused and the recent Windows programming model has evolved to counter that. \nPostQuitMessage adds a <code class=\"language-plaintext highlighter-rouge\">WM_QUIT<\/code> message to the message queue which causes <code class=\"language-plaintext highlighter-rouge\">GetMessage()<\/code> to false, exiting the loop and exiting the program.\nYour program doesn\u2019t have to handle all the messages. It can handle a few special messages of interest and then call <code class=\"language-plaintext highlighter-rouge\">DefWindowProc<\/code> - OS provided default handler to deal with the rest. <code class=\"language-plaintext highlighter-rouge\">WndProc<\/code> can choose to handle messages for all windows in a thread or it can defer them to respective WndProc to handle.  See <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/commctrl\/nf-commctrl-defsubclassproc\">Subclassing<\/a> as example of dynamic polymorphism to achieve that.<\/p>\n\n<h3 id=\"painting-the-window\">Painting the window<\/h3>\n<p>The code within <code class=\"language-plaintext highlighter-rouge\">case WM_PAINT<\/code>  is the boilerplate code for drawing anything in a window. Windows Graphics Driver Interface (GDI) is immediate mode (link to it). A lot of newer UI libraries like WPF and WinUI are retained mode GUI frameworks because of memory and performance reasons. MSDN\u2019s  Painting the Window - Win32 apps does a very good job of explaining this code.<\/p>\n\n<h2 id=\"compilation\">Compilation<\/h2>\n<p>MSVC is the compiler of choice. It can be compiled with Visual Studio or in terminal using msbuild. It needs kernel32.dll, user32.dll, gdi32.dll and \/SUBSYSTEM:WINDOWS. Yes, the concept of subsystem has existed since start of Windows NT (because Windows was supposed to run OS\/2 as subsystem which didn\u2019t happen). That was helpful when Windows introduced Windows Subsystem for Linux (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Windows_Subsystem_for_Linux\">WSL<\/a>) 30+ years later.<\/p>\n\n<h1 id=\"ending-notes\">Ending notes<\/h1>\n<p>Thank you for reaching this far. The idea of this post came while I started learning Win32 apis for my work. MSDN does a good job of explaining the api but outside of that, a very little number of articles and blogs exist. This article aims to improve on that.  <br \/>\nIf you find any mistakes on the blog or have any other suggestions, let me know on twitter.<\/p>\n\n<h2 id=\"references\">References<\/h2>\n<ul>\n  <li><a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/windows\/overview-of-windows-programming-in-cpp?view=vs-2019\">MSDN<\/a><\/li>\n  <li><a href=\"https:\/\/www.amazon.com\/Programming-Windows%C2%AE-Fifth-Developer-Reference\/dp\/157231995X\">Programming Windows by Charles Petzold 1998 edition<\/a><\/li>\n<\/ul>\n"}]}