Listing 3 The MessageBox_printf() function
int MessageBox_printfA( HINSTANCE hinst, HWND hwnd, LPCSTR lpszFmt, LPCSTR lpszCaption, UINT uType, ...) { #ifndef NUM_ELEMENTS #define NUM_ELEMENTS(x) (sizeof(x) / sizeof((x)[0])) #endif /* NUM_ELEMENTS */ char szFmt[768]; char szText[1024]; char szCaption[128]; va_list args; int iRet; va_start(args, uType); /* lpszFmt and/or lpszCaption can be a resource id, so will * need a valid hinst. */ if( HIWORD(lpszFmt) == 0 || HIWORD(lpszCaption) == 0) { if(hinst == NULL) { /* Assume the instance handle of the window, if * non-NULL, or get the process module handle. */ if(hwnd != NULL) { hinst = (HINSTANCE)GetWindowLongA(hwnd, GWL_HINSTANCE); } else { hinst = GetModuleHandleA(NULL); } } } /* Load the format string, if required. */ if(HIWORD(lpszFmt) == 0) { LoadStringA(hinst, LOWORD((DWORD)lpszFmt), szFmt, NUM_ELEMENTS(szFmt)); lpszFmt = szFmt; } /* sprintf the format & arguments (if any) into the text. */ wvsprintfA(szText, lpszFmt, args); lpszFmt = szText; /* Load the title string, if required. */ if(HIWORD(lpszCaption) == 0) { LoadStringA(hinst, LOWORD((DWORD)lpszCaption), szCaption, NUM_ELEMENTS(szCaption)); lpszCaption = szCaption; } /* Pop the message box. */ iRet = MessageBoxA(hwnd, lpszFmt, lpszCaption, uType); va_end(args); return iRet; }