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.
#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.
#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:
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
- Programming Windows, fifth edition by Charles Petzold in 1998
- Inconsistent annotation for 'WinMain'
- Inconsistent annotation for 'WinMain'
- Annotating Function Parameters and Return Values
- Using SAL Annotations to Reduce C/C++ Code Defects
- Annotating Function Behavior
- Annotating Structs and Classes
- Understanding SAL
History
- 01-30-2020: Initial post
- 02-01-2020: Added code snippet and demo project