//-----------------------------------------//This program shows how to implement the MESSAGEBOXPRINTF function//This function can format output like printf//excerpt from: The fifth edition of Windows programming//-----------------------------------------#include <windows.h>#include<tchar.h>#include<stdio.h>intCDECL messageboxprintf (TCHAR * szcaption, TCHAR *Szformat, ...) {TCHAR szbuffer[1024x768]; Va_list parglist; Va_start (Parglist, Szformat); _vsntprintf_s (Szbuffer,sizeof(szbuffer)/sizeof(TCHAR),sizeof(szbuffer)/sizeof(TCHAR), Szformat, parglist); //----------------------------------------------------------------------------------------------------------- ---------------------- //The original text uses _vsntprintf (szbuffer, sizeof (szbuffer)/sizeof (TCHAR), Szformat, parglist); vs thinks this function has a security problem so I replaced the upper function.//----------------------------------------------------------------------------------------------------------- ----------------------va_end (parglist); returnMessageBox (Null,szbuffer,szcaption,0);}intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szCmdLine,inticmdshow) { intCxscreen; intCyscreen; Cxscreen=GetSystemMetrics (Sm_cxscreen); Cyscreen=GetSystemMetrics (Sm_cyscreen); messageboxprintf (TEXT ("Screen SIZE"), TEXT ("Your video monitor: \nwide:%i\nhigh:%i"), Cxscreen,cyscreen); return 0;}
This function uses variable parameters so there are some strange things that are explained below
CDECL
__cdecl __fastcall and __stdcall, all three are calling conventions (calling convention), which determine the following: 1) the stack order of the function arguments, 2) The arguments are popped by the caller or by the caller, 3) and the method that produces the function decorated name. 1. __stdcall calling convention: The function's parameters are passed from right to left through the stack, and the called function cleans the memory stack of the transfer parameters before returning. 2. _CDECL is the default calling method for C and C + + programs. Each function that calls it contains the code that empties the stack, so the resulting executable file size is larger than the call to the _stdcall function. The function uses a right-to-left compression stack. Note: For variable parameter member functions, always use the __cdecl conversion method. 3. __fastcall calling convention: it transmits the parameters via registers (in fact, it transmits the first two double words (DWORD) or smaller parameters with ECX and edx, the remaining parameters are still transferred from right to left, and the called function cleans up the memory stack of the transfer parameters before returning.
Va_start Va_end, etc.
Function name, the process of reading a variable parameter is actually in the stack, using pointers, traversing the parameter list in the stack segment, from the low address to the high address one by one to read the contents of the parameters of the process
#define VA_START (AP,V) (AP = (va_list) &v + _intsizeof (v))///First optional parameter address # define VA_ARG (Ap,t) (* (t *) (AP + = _intsizeof (t))-_intsizeof (t))//Next parameter Address # define VA_END (AP) (AP = (va_list) 0)//set pointer to invalid
This function can be a familiar experience for C + + programmers when learning Windows programming ....
Wndows Programming Books Knowledge and code excerpt-encapsulates a messagebox like printf