/−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
SCRNSIZE.C −− Displays screen size in a message box
(c) Charles Petzold, 1998
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−/
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
{
TCHAR szBuffer [1024] ;
va_list pArgList ;
// The va_start macro (defined in STDARG.H) is usually equivalent to:
// pArgList = (char *) &szFormat + sizeof (szFormat) ;
va_start (pArgList, szFormat) ;
// The last argument to wvsprintf points to the arguments
_vsntprintf (szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
szFormat, pArgList) ;
// The va_end macro just zeroes out pArgList for no good reason
va_end (pArgList) ;
return MessageBox (NULL, szBuffer, szCaption, 0) ;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
int cxScreen, cyScreen ;
cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
MessageBoxPrintf (TEXT ("ScrnSize"),
TEXT ("The screen is %i pixels wide by %i pixels high."),
cxScreen, cyScreen) ;
return 0 ;
}
explain the code above
Assistant
User
how does va_list, va_start, va_end work?
Assistant
The code above is a simple Windows application written in C that determines and displays the dimensions of the computer screen in a message box. Let's break down the code to understand its components and functionality:
Purpose: A utility function to display a message box with a formatted string.
Parameters:
szCaption: The title of the message box.
szFormat: A format string similar to printf function syntax.
...: A variable number of arguments to be formatted according to szFormat.
Functionality:
Declares a buffer szBuffer to hold the formatted message.
Declares a va_list variable pArgList to traverse the arguments.
Initializes pArgList using va_start.
Calls _vsntprintf to format the string with the variable arguments.
Ends argument traversal with va_end.
Calls MessageBox to display the formatted message.
Entry Point: WinMain
c
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
int cxScreen, cyScreen ;
cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
MessageBoxPrintf (TEXT ("ScrnSize"),
TEXT ("The screen is %i pixels wide by %i pixels high."),
cxScreen, cyScreen) ;
return 0 ;
}
WinMain: The entry point for a Win32 GUI application.
Parameters:
hInstance: Handle to the current instance of the application.
hPrevInstance: Handle to the previous instance of the application (unused, always NULL).
szCmdLine: Command line arguments as a string.
iCmdShow: Flag indicating how the application window should be shown.
Functionality:
Declares variables cxScreen and cyScreen to hold the screen width and height.
Uses GetSystemMetrics to obtain the screen's width SM_CXSCREEN and height SM_CYSCREEN.
Calls MessageBoxPrintf to show a message box with the screen dimensions.
Explanation
The program retrieves the screen dimensions and then formats and displays them in a message box. The MessageBoxPrintf function is a helper to facilitate formatted display using a message box, similar to how printf works in console applications.
Key Points:
GetSystemMetrics retrieves various system metrics, in this case, the screen width and height.
The formatted message is displayed using a message box with the title "ScrnSize".
The va_list, va_start, and va_end macros are part of the C standard library and are used to handle variable argument lists in functions. These macros are defined in the header file stdarg.h. Here's a detailed breakdown of what each of these macros does and how they work together:
va_list
Definition: va_list is a type that is used to declare a variable that will hold the information needed to retrieve the additional arguments.
Usage: You declare a variable of type va_list that will be used to iterate over each argument in the variable argument list.
va_start
Definition: va_start is a macro that initializes the va_list variable to point to the first argument in the variable argument list.
Syntax: va_start(va_list, last_fixed_param)
va_list: The va_list variable that will be initialized.
last_fixed_param: The last named parameter before the ellipsis (...). This is used to determine the start of the variable arguments.
How it works: Based on the position of the last named parameter, va_start sets up the va_list so that it points to the first unnamed argument.
va_arg
Definition: va_arg is a macro that retrieves the next argument in the variable argument list.
Syntax: va_arg(va_list, type)
va_list: The va_list variable that is tracking the position of arguments.
type: The type of the next argument to be retrieved.
How it works: va_arg reads the current argument and advances the va_list to point to the next argument in the list.
va_end
Definition: va_end is a macro that cleans up the va_list variable when all arguments have been retrieved.
Syntax: va_end(va_list)
va_list: The va_list variable that was used.
How it works: It performs any necessary cleanup operations. Typically, it just invalidates the va_list variable, but the exact behavior is implementation-dependent.
Example
Let's consider a simplified example to illustrate their usage:
c
#include <stdio.h>
#include <stdarg.h>
void printNumbers(int count, ...) {
va_list args; // Declare a va_list variable.
va_start(args, count); // Initialize the va_list variable to point to the first argument after 'count'.
for (int i = 0; i < count; i++) {
int number = va_arg(args, int); // Retrieve the next argument (of type int) in the list.
printf("%d ", number);
}
va_end(args); // Clean up the va_list variable.
printf("\n");
}
int main() {
printNumbers(3, 10, 20, 30); // Prints: 10 20 30
printNumbers(5, 1, 2, 3, 4, 5); // Prints: 1 2 3 4 5
return 0;
}
Step-by-Step Breakdown
Declaration: va_list args;. This declares args of type va_list which will store the information necessary for traversing the variable arguments.
Initialization: va_start(args, count);. This initializes args to point to the first argument after count.
Retrieval: va_arg(args, int);. This retrieves the next variable argument of type int and advances args to point to the subsequent argument in the list.
Cleanup: va_end(args);. This ends the traversal of the variable arguments and performs any necessary cleanup.
In the original code you posted, these macros are used to create a formatted string (similar to printf's behavior) with the supplied variable arguments, and then the string is displayed in a message box. The functions provide a way to handle any number of additional arguments, making the program flexible in terms of input handling.