Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C++
Article

MessageBoxTimeout API

Rate me:
Please Sign up or sign in to vote.
4.84/5 (31 votes)
6 Aug 2004CPOL1 min read 199.3K   1.6K   50   38
An article on using the non documented MessageBoxTimeout API.

Introduction

For reasons unknown, Microsoft has never documented the MessageBoxTimeout API located in user32.dll, so here it is for those seeking a Message Box that times out and auto closes if the user does not respond to it first.

Using the code

The code is usable for both UNICODE and MBCS, simply add the following to a source (cpp) file:

#include <windows.h>
#include <tchar.h>

//Functions & other definitions required-->
typedef int (__stdcall *MSGBOXAAPI)(IN HWND hWnd, 
        IN LPCSTR lpText, IN LPCSTR lpCaption, 
        IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);
typedef int (__stdcall *MSGBOXWAPI)(IN HWND hWnd, 
        IN LPCWSTR lpText, IN LPCWSTR lpCaption, 
        IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);

int MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText, 
    IN LPCSTR lpCaption, IN UINT uType, 
    IN WORD wLanguageId, IN DWORD dwMilliseconds);
int MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText, 
    IN LPCWSTR lpCaption, IN UINT uType, 
    IN WORD wLanguageId, IN DWORD dwMilliseconds);

#ifdef UNICODE
    #define MessageBoxTimeout MessageBoxTimeoutW
#else
    #define MessageBoxTimeout MessageBoxTimeoutA
#endif 

#define MB_TIMEDOUT 32000

int MessageBoxTimeoutA(HWND hWnd, LPCSTR lpText, 
    LPCSTR lpCaption, UINT uType, WORD wLanguageId, 
    DWORD dwMilliseconds)
{
    static MSGBOXAAPI MsgBoxTOA = NULL;

    if (!MsgBoxTOA)
    {
        HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));
        if (hUser32)
        {
            MsgBoxTOA = (MSGBOXAAPI)GetProcAddress(hUser32, 
                                      "MessageBoxTimeoutA");
            //fall through to 'if (MsgBoxTOA)...'
        }
        else
        {
            //stuff happened, add code to handle it here 
            //(possibly just call MessageBox())
            return 0;
        }
    }

    if (MsgBoxTOA)
    {
        return MsgBoxTOA(hWnd, lpText, lpCaption, 
              uType, wLanguageId, dwMilliseconds);
    }

    return 0;
}

int MessageBoxTimeoutW(HWND hWnd, LPCWSTR lpText, 
    LPCWSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds)
{
    static MSGBOXWAPI MsgBoxTOW = NULL;

    if (!MsgBoxTOW)
    {
        HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));
        if (hUser32)
        {
            MsgBoxTOW = (MSGBOXWAPI)GetProcAddress(hUser32, 
                                      "MessageBoxTimeoutW");
            //fall through to 'if (MsgBoxTOW)...'
        }
        else
        {
            //stuff happened, add code to handle it here 
            //(possibly just call MessageBox())
            return 0;
        }
    }

    if (MsgBoxTOW)
    {
        return MsgBoxTOW(hWnd, lpText, lpCaption, 
               uType, wLanguageId, dwMilliseconds);
    }

    return 0;
}
//End required definitions <--

Call the function as follows:

//you must load user32.dll before calling the function
HMODULE hUser32 = LoadLibrary(_T("user32.dll"));
    
if (hUser32)
{
    int iRet = 0;
    UINT uiFlags = MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;
    
    iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 2 seconds."), 
                      _T("MessageBoxTimeout Test"), uiFlags, 0, 2000);
    //iRet will = 1

    uiFlags = MB_YESNO|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;

    iRet = MessageBoxTimeout(NULL, _T("Test a timeout of 5 seconds."), 
                      _T("MessageBoxTimeout Test"), uiFlags, 0, 5000);
    //iRet will = MB_TIMEDOUT if no buttons pressed, button values otherwise
        
    //only unload user32.dll when you have no further need 
    //for the MessageBoxTimeout function
    FreeLibrary(hUser32);
}

The function will return an integer value of either a MB_TIMEDOUT value indicating the timeout period was reached and the Message Box auto closed, or a value representing the button the user clicked. Note that when a Message Box with only an OK button (MB_OK Flag) is used, the return value is always 1.

The sample code provided is for Visual Studio .NET 2003 but can easily be used in other compilers.

Points of Interest

You may be thinking what reassurances exist that someday Microsoft may remove this function. In all honesty, there are none, however, it's interesting to note that internally, all the documented MessageBox* functions call the MessageBoxTimeout API and simply pass 0xFFFFFFFF as the timeout period (a very long time), so the probability of it being removed is minimal.

License

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


Written By
Software Developer Versoworks Pty Ltd
Australia Australia
A self taught C++ programmer.

Author of Ahoihoi and Speckie

Comments and Discussions

 
Generalgood job! Pin
lee dong hoon from Hanyang17-Dec-13 14:38
lee dong hoon from Hanyang17-Dec-13 14:38 
GeneralWindows 2000 Pin
nice_fox25-Jan-10 13:01
nice_fox25-Jan-10 13:01 
GeneralSimpler yet... PinPopular
olecoder19-Mar-09 9:38
olecoder19-Mar-09 9:38 
Generalmy 5 Pin
Alexandre GRANVAUD28-Jan-09 20:27
Alexandre GRANVAUD28-Jan-09 20:27 
GeneralFailed giving right output on Windows 2000... Pin
Jigar Mehta20-Aug-04 1:12
Jigar Mehta20-Aug-04 1:12 
Hye,
Using your code to give messagebox with timeout period of 2000 (ms).. But following code's debugging gives...

if (!MsgBoxTOA)
{
HMODULE hUser32 = GetModuleHandle(_T("user32.dll"));
if (hUser32)
{
MsgBoxTOA = (MSGBOXAAPI)GetProcAddress(hUser32, "MessageBoxTimeoutA");
//fall through to 'if (MsgBoxTOA)...'
}
else
{
//Stuff happened, add code to handle it here (possibly just call MessageBox())
return 0;
}
}

if (MsgBoxTOA)
{
return MsgBoxTOA(hWnd, lpText, lpCaption, uType, wLanguageId, dwMilliseconds);
}

value of MsgBoxTOA as 0x00000000

So, what can be the problem...

And another thing, Is it possible to give messagebox without any button on it... Like without OK any messagebox??? just for showing status for some seconds??? Is it possible by setting some combination of flags????

Thanks for giving such a nice code...

Rose | [Rose]

Jigar Mehta
(jigarmehta@gatescorp.com)

Software Developer
Gates Information Systems
GeneralRe: Failed giving right output on Windows 2000... Pin
Maurizio Pisano20-Aug-04 1:21
Maurizio Pisano20-Aug-04 1:21 
GeneralGood job! Pin
Zuoliu Ding14-Aug-04 15:27
Zuoliu Ding14-Aug-04 15:27 
General3 Pin
Ryan Binns10-Aug-04 17:10
Ryan Binns10-Aug-04 17:10 
GeneralRe: 3 Pin
Emilio Garavaglia11-Aug-04 6:14
Emilio Garavaglia11-Aug-04 6:14 
GeneralPlease no profanity Pin
billgatest9-Aug-04 8:44
billgatest9-Aug-04 8:44 
GeneralRe: Please no profanity Pin
Maurizio Pisano9-Aug-04 11:45
Maurizio Pisano9-Aug-04 11:45 
GeneralRe: Please no profanity Pin
billgatest10-Aug-04 5:00
billgatest10-Aug-04 5:00 
GeneralRe: Please no profanity Pin
Maurizio Pisano10-Aug-04 10:57
Maurizio Pisano10-Aug-04 10:57 
GeneralRe: Please no profanity Pin
Miguel Lopes10-Aug-04 1:05
Miguel Lopes10-Aug-04 1:05 
GeneralRe: Please no profanity Pin
billgatest10-Aug-04 4:57
billgatest10-Aug-04 4:57 
GeneralRe: Please no profanity Pin
Miguel Lopes10-Aug-04 5:37
Miguel Lopes10-Aug-04 5:37 
GeneralRe: Please no profanity Pin
Ashaman10-Aug-04 8:00
Ashaman10-Aug-04 8:00 
GeneralRe: Please no profanity Pin
kewball10-Aug-04 8:45
kewball10-Aug-04 8:45 
GeneralRe: Please no profanity Pin
billgatest10-Aug-04 10:03
billgatest10-Aug-04 10:03 
GeneralRe: Please no profanity Pin
Emilio Garavaglia11-Aug-04 6:26
Emilio Garavaglia11-Aug-04 6:26 
GeneralRe: Please no profanity Pin
Bill S28-Sep-04 1:27
professionalBill S28-Sep-04 1:27 
GeneralRe: Please no profanity Pin
billgatest10-Aug-04 10:03
billgatest10-Aug-04 10:03 
GeneralRe: Please no profanity Pin
Miguel Lopes10-Aug-04 12:55
Miguel Lopes10-Aug-04 12:55 
GeneralRe: Please no profanity Pin
raiden73310-Aug-04 22:11
raiden73310-Aug-04 22:11 
GeneralRe: Please no profanity Pin
killaw29-Sep-04 18:54
killaw29-Sep-04 18:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.