home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / crtmbox.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  69 lines

  1. /***
  2. *crtmbox.c - CRT MessageBoxA wrapper.
  3. *
  4. *       Copyright (c) 1995-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Wrap MessageBoxA.
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _WIN32
  12.  
  13. #include <awint.h>
  14.  
  15. /***
  16. *__crtMessageBox - call MessageBoxA dynamically.
  17. *
  18. *Purpose:
  19. *       Avoid static link with user32.dll. Only load it when actually needed.
  20. *
  21. *Entry:
  22. *       see MessageBoxA docs.
  23. *
  24. *Exit:
  25. *       see MessageBoxA docs.
  26. *
  27. *Exceptions:
  28. *
  29. *******************************************************************************/
  30. int __cdecl __crtMessageBoxA(
  31.         LPCSTR lpText,
  32.         LPCSTR lpCaption,
  33.         UINT uType
  34.         )
  35. {
  36.         static int (APIENTRY *pfnMessageBoxA)(HWND, LPCSTR, LPCSTR, UINT) = NULL;
  37.         static HWND (APIENTRY *pfnGetActiveWindow)(void) = NULL;
  38.         static HWND (APIENTRY *pfnGetLastActivePopup)(HWND) = NULL;
  39.  
  40.         HWND hWndParent = NULL;
  41.  
  42.         if (NULL == pfnMessageBoxA)
  43.         {
  44.             HANDLE hlib = LoadLibrary("user32.dll");
  45.  
  46.             if (NULL == hlib || NULL == (pfnMessageBoxA =
  47.                         (int (APIENTRY *)(HWND, LPCSTR, LPCSTR, UINT))
  48.                         GetProcAddress(hlib, "MessageBoxA")))
  49.                 return 0;
  50.  
  51.             pfnGetActiveWindow = (HWND (APIENTRY *)(void))
  52.                         GetProcAddress(hlib, "GetActiveWindow");
  53.  
  54.             pfnGetLastActivePopup = (HWND (APIENTRY *)(HWND))
  55.                         GetProcAddress(hlib, "GetLastActivePopup");
  56.         }
  57.  
  58.         if (pfnGetActiveWindow)
  59.             hWndParent = (*pfnGetActiveWindow)();
  60.  
  61.         if (hWndParent != NULL && pfnGetLastActivePopup)
  62.             hWndParent = (*pfnGetLastActivePopup)(hWndParent);
  63.  
  64.         return (*pfnMessageBoxA)(hWndParent, lpText, lpCaption, uType);
  65. }
  66.  
  67. #endif  /* _WIN32 */
  68.  
  69.