Click here to Skip to main content
11,479,034 members (60,716 online)
Click here to Skip to main content

MessageBoxTimeout API

, 6 Aug 2004 CPOL 122.7K 1.1K 47
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Maurizio Pisano
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 15:38
grouplee dong hoon from Hanyang17-Dec-13 15:38 
GeneralWindows 2000 Pin
nice_fox25-Jan-10 14:01
membernice_fox25-Jan-10 14:01 
GeneralSimpler yet... Pin
olecoder19-Mar-09 10:38
memberolecoder19-Mar-09 10:38 
Generalmy 5 Pin
Alexandre GRANVAUD28-Jan-09 21:27
memberAlexandre GRANVAUD28-Jan-09 21:27 
GeneralFailed giving right output on Windows 2000... Pin
Jigar Mehta20-Aug-04 2:12
memberJigar Mehta20-Aug-04 2: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 2:21
memberMaurizio Pisano20-Aug-04 2:21 
GeneralGood job! Pin
Zuoliu Ding14-Aug-04 16:27
memberZuoliu Ding14-Aug-04 16:27 
General3 Pin
Ryan Binns10-Aug-04 18:10
memberRyan Binns10-Aug-04 18:10 
GeneralRe: 3 Pin
emilio_grv11-Aug-04 7:14
memberemilio_grv11-Aug-04 7:14 
GeneralPlease no profanity Pin
billgatest9-Aug-04 9:44
memberbillgatest9-Aug-04 9:44 
GeneralRe: Please no profanity Pin
Maurizio Pisano9-Aug-04 12:45
memberMaurizio Pisano9-Aug-04 12:45 
GeneralRe: Please no profanity Pin
billgatest10-Aug-04 6:00
memberbillgatest10-Aug-04 6:00 
GeneralRe: Please no profanity Pin
Maurizio Pisano10-Aug-04 11:57
memberMaurizio Pisano10-Aug-04 11:57 
GeneralRe: Please no profanity Pin
Miguel Lopes10-Aug-04 2:05
memberMiguel Lopes10-Aug-04 2:05 
GeneralRe: Please no profanity Pin
billgatest10-Aug-04 5:57
memberbillgatest10-Aug-04 5:57 
GeneralRe: Please no profanity Pin
Miguel Lopes10-Aug-04 6:37
memberMiguel Lopes10-Aug-04 6:37 
GeneralRe: Please no profanity Pin
Ashaman10-Aug-04 9:00
memberAshaman10-Aug-04 9:00 
GeneralRe: Please no profanity Pin
kewball10-Aug-04 9:45
memberkewball10-Aug-04 9:45 
GeneralRe: Please no profanity Pin
billgatest10-Aug-04 11:03
memberbillgatest10-Aug-04 11:03 
GeneralRe: Please no profanity Pin
emilio_grv11-Aug-04 7:26
memberemilio_grv11-Aug-04 7:26 
GeneralRe: Please no profanity Pin
Bill S28-Sep-04 2:27
memberBill S28-Sep-04 2:27 
GeneralRe: Please no profanity Pin
billgatest10-Aug-04 11:03
memberbillgatest10-Aug-04 11:03 
GeneralRe: Please no profanity Pin
Miguel Lopes10-Aug-04 13:55
memberMiguel Lopes10-Aug-04 13:55 
GeneralRe: Please no profanity Pin
raiden73310-Aug-04 23:11
memberraiden73310-Aug-04 23:11 
GeneralRe: Please no profanity Pin
killaw29-Sep-04 19:54
memberkillaw29-Sep-04 19:54 
GeneralRe: Please no profanity Pin
Mark_VC27-Jul-07 8:29
memberMark_VC27-Jul-07 8:29 
GeneralRe: Please no profanity Pin
MrGoodly6-Jun-08 15:48
memberMrGoodly6-Jun-08 15:48 
GeneralRe: Please no profanity Pin
ZIPmagic29-Aug-14 3:11
memberZIPmagic29-Aug-14 3:11 
GeneralMissing MessageBoxTimeoutA Pin
Hannu Lahtela9-Aug-04 3:57
memberHannu Lahtela9-Aug-04 3:57 
GeneralRe: Missing MessageBoxTimeoutA Pin
Maurizio Pisano9-Aug-04 12:44
memberMaurizio Pisano9-Aug-04 12:44 
GeneralRe: Missing MessageBoxTimeoutA Pin
Hannu Lahtela9-Aug-04 20:50
memberHannu Lahtela9-Aug-04 20:50 
GeneralRe: Missing MessageBoxTimeoutA Pin
Machibest20-Sep-05 19:29
sussMachibest20-Sep-05 19:29 
GeneralRe: Missing MessageBoxTimeoutA Pin
Machibest20-Sep-05 19:49
sussMachibest20-Sep-05 19:49 
GeneralRe: Missing MessageBoxTimeoutA Pin
Richard Jones10-Aug-04 3:13
memberRichard Jones10-Aug-04 3:13 
GeneralRe: Missing MessageBoxTimeoutA Pin
Philippe Lhoste19-Aug-04 0:27
memberPhilippe Lhoste19-Aug-04 0:27 
GeneralWow, that's cool! Pin
Nishant S8-Aug-04 8:15
staffNishant S8-Aug-04 8:15 
GeneralRe: Wow, that's cool! Pin
Maurizio Pisano8-Aug-04 12:05
memberMaurizio Pisano8-Aug-04 12:05 
GeneralNice Stuff ! Pin
Sondre7-Aug-04 23:40
memberSondre7-Aug-04 23:40 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 7 Aug 2004
Article Copyright 2004 by Maurizio Pisano
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid