Programming Windows: Hello, Windows (Premium)

As you may recall, creating a simple hello, world application in the C programming language for a textual command line environment like MS-DOS (or the Windows Command Prompt) is quick, easy, and readable, even for non-programmers. It looks like so.
#include <stdio.h>

main()
{
   printf("hello, world\n");
}
But as you may also recall, creating even a basic Windows application using the C programming language was both expensive---you needed Microsoft C (or compatible) and the Windows Software Development Kit, both of which cost several hundreds dollars---and difficult. Windows applications didn’t work like MS-DOS applications, as they needed to coexist with other applications and windows in a multitasking GUI-based environment. The benefits of creating applications for this environment were many. But so, too, were the complexities.

Previously, I described the basic structure of a Windows application in simple terms: The developer needed to define and then create at least one window and then create a message loop that picked out the messages---mouse clicks, button presses, application termination, and so on---to which it would reply. At a high level, that really is all there is too it. But the “details,” as Windows programming guru Charles Petzold described the remainder of the task, were quite formidable, even for experienced developers.

Before getting to that, let me introduce a quick cheat: As Petzold explains in Programming Windows 5th edition---aimed at Windows 98, NT 4.0, and Windows 2000, and the last edition of the book to cover “classical” Windows application development using the Windows API---you can approximate the simplicity of the C-based hello, world command line application in Windows by bypassing the messiness of registering a window class, creating the window, and then running a message loop. Instead, you simply open a Message Box that displays the text Hello, Windows.

Petzold’s version from 1999 looks like so.
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    MessageBox (NULL, TEXT("Hello, Windows 98!"), TEXT("HelloMsg"), 0);
}
Bringing this code into the 21st century was straightforward. I created a new project in Visual Studio 2019 Community using the Windows Desktop Application project type and the C++ language.

This project type creates what today is considered a barebones desktop application with a simple menu, a custom icon, and an About box.

But it also requires a lot of code. So, I deleted all of the code and replaced it with the modern version of Petzold’s little cheat, which was unique to the 5th edition of Programming Windows. It looks like so:
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
    MessageBoxExW (NULL, TEXT("Hello, Windows"), TEXT("Hello, Windows"), 0, NULL);
}
Like the orig...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC