Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

How to Compile Legacy Win32 Programs in Visual Studio 2019?

5.00/5 (7 votes)
4 Feb 2020CPOL2 min read 41.1K   262  
Some tips on how to compile legacy Win32 programs in Visual Studio 2019 - may save you few hours to figure out why

Introduction

Last weekend, I got an old book on Programming Windows by Charles Petzold published in 1998. It comes with a CD-Rom loaded with source code. These examples were compiled in Visual C++ 5.0 projects. So will these examples work in Visual Studio 2019? If not, how can we make them work? Here are some tips I gleaned through the internet and tested. Here I aggregate them as an article to share here. If you read through this post, it may save you a few hours trying to figure out how to do it.

I will continuously add more contents into this post and hope to resolve more compilation issues and warnings.

Background

Although Windows has evolved over many versions, Win32 programming still holds its ground. Let's dig some old legacy Win32 programs written in 1998 and see if they still work now.

Win32 Program of Hello World from the Book

I am using the first example in the book[1] and will walk through the process. Let's see if we can make it work in Visual Studio 2019.

Step 1

Create an empty project by this sequence in Visual Studio 2019: file->create new project->C++->Console application -> empty project.

Step 2

Add a C language source code file such as main.c and copy the following code from Charles Petzold's book[1] into this main.c.

C++
/*--------------------------------------------------------------
   HelloMsg.c -- Displays "Hello, Windows 98!" in a message box
                 (c) Charles Petzold, 1998
  --------------------------------------------------------------*/

#include <windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0) ;

     return 0 ;
}

Step 3

Go to menu -> build ->build solution. Build this project.

Step 4

Fix errors and warnings.

Error and Warnnig Messages

We get the following error messages:

Error	LNK2019	unresolved external symbol _main referenced in function 
"int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)	
Hello5	C:\Demos\Hello5\MSVCRTD.lib(exe_main.obj)	1	
Error	LNK1120	1 unresolved externals	Hello5	C:\Demos\Hello5\Debug\Hello5.exe

We have one warning message:

Warning	C28251	Inconsistent annotation for 'WinMain': this instance has no annotations. 
See c:\program files (x86)\windows kits\10\include\10.0.18362.0\um\winbase.h(933). 	
Hello5	C:\DEMOS\HELLO5\MAIN.C	8	

Here are the fixes. I did some research on the internet and got enlightened. Here, I integrated these thoughts together and list some key reference here.

Tips to Make It Work

We have several steps to fix these errors and warnings.

Step 1. Go to project property->Linker->System->select Windows

Step 2. Set up _In_ or _In_opt_ to these input parameters to WinMain function.

Step 3. Build this project again. errors and warnings are cleared.

C++
    /*--------------------------------------------------------------
   HelloMsg.c -- Displays "Hello, Windows 98!" in a message box
                 (c) Charles Petzold, 1998
  --------------------------------------------------------------*/
#include <windows.h>

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
    _In_ PSTR szCmdLine, _In_ int iCmdShow)
{
    MessageBox(NULL, TEXT("Hello, Windows 98!"), TEXT("HelloMsg"), 0);

    return 0;
}

After these fixes, we can see it compiles smoothly. All errors and warnings are gone.

Run this application and we will see a message box pop up:

Image 1

Points of Interest

Through building this legacy projects written in 1998 with VC++ 5.0, I still learned something new, especially SAL stuff from Microsoft.

It seems like with SAL equipped, legacy Win32 program can still be useable in future business world scenarios.

If you have any good tips, tricks and similar experiences, please drop some comments here.

References

  1. Programming Windows, fifth edition by Charles Petzold in 1998
  2. Inconsistent annotation for 'WinMain'
  3. Inconsistent annotation for 'WinMain'
  4. Annotating Function Parameters and Return Values
  5. Using SAL Annotations to Reduce C/C++ Code Defects
  6. Annotating Function Behavior
  7. Annotating Structs and Classes
  8. Understanding SAL

History

  • 01-30-2020: Initial post
  • 02-01-2020: Added code snippet and demo project

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)