home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / sdk / win32s / ut / samples / utsample / utsamp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-11  |  8.5 KB  |  229 lines

  1. /********************************************************************\
  2. *  UTSamp.c -- Sample program demonstrating Universal Thunks under   *
  3. *              Win32s, including calling APIs unsupported by         *
  4. *              Win32s.                                               *
  5. *                                                                    *
  6. *  Lee Hart                                                          *
  7. *  Microsoft Developer Support                                       *
  8. *  Copyright (c) 1993-1994 Microsoft Corporation                     *
  9. *                                                                    *
  10. *  Comments:                                                         *
  11. *                                                                    *
  12. *  Shows how to use Universal Thunks to call APIs that are not       *
  13. *  available directly through Win32s.                                *
  14. *                                                                    *
  15. *  Modifications made by Julie Solon and Lee Hart after initial      *
  16. *  sample release to demonstrate the callback from the 16-bit side.  *
  17. *                                                                    *
  18. *  Functions:                                                        *
  19. *                                                                    *
  20. *  WinMain()         - Initializes Application                       *
  21. *  MainWndProc()     - Processes Application Messages                *
  22. *  AboutDlgProc()    - Processes "About" Dialog Box Messages         *
  23. *                                                                    *
  24. *                                                                    *
  25. \********************************************************************/
  26.  
  27.  
  28. /*********************  Header Files  *********************/
  29.  
  30. #include <windows.h>
  31. #include "utsamp.h"
  32.  
  33. /**********************  Prototypes  **********************/
  34.  
  35. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
  36. LRESULT CALLBACK AboutDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
  37.  
  38. /*******************  Global Variables  *******************/
  39.  
  40. HANDLE ghInstance;
  41.  
  42. /********************************************************************\
  43. *  Function: int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)    *
  44. *                                                                    *
  45. *   Purpose: Initializes Application, sets up Universal Thunk if     *
  46. *            running under Win32s (as determined by the high bit of  *
  47. *            GetVersion().                                           *
  48. *                                                                    *
  49. \********************************************************************/
  50.  
  51.  
  52. int WINAPI WinMain( HINSTANCE hInstance,
  53.                     HINSTANCE hPrevInstance,
  54.                     LPSTR lpszCmdLine,
  55.                     int nCmdShow )
  56. {
  57.    WNDCLASS wc;
  58.    MSG msg;
  59.    HWND hWnd;
  60.  
  61.    if( !hPrevInstance )
  62.    {
  63.       wc.lpszClassName = "UTSampClass";
  64.       wc.lpfnWndProc = MainWndProc;
  65.       wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
  66.       wc.hInstance = hInstance;
  67.       wc.hIcon = LoadIcon( hInstance, "UTSampIcon" );
  68.       wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  69.       wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
  70.       wc.lpszMenuName = "UTSampMenu";
  71.       wc.cbClsExtra = 0;
  72.       wc.cbWndExtra = 0;
  73.  
  74.       RegisterClass( &wc );
  75.    }
  76.  
  77.    ghInstance = hInstance;
  78.  
  79.    hWnd = CreateWindow( "UTSampClass",
  80.                         "Universal Thunks Sample",
  81.                         WS_OVERLAPPEDWINDOW,
  82.                         0,
  83.                         0,
  84.                         CW_USEDEFAULT,
  85.                         CW_USEDEFAULT,
  86.                         NULL,
  87.                         NULL,
  88.                         hInstance,
  89.                         NULL);
  90.  
  91.    ShowWindow( hWnd, nCmdShow );
  92.  
  93.    while( GetMessage( &msg, NULL, 0, 0 ) )
  94.    {
  95.       TranslateMessage( &msg );
  96.       DispatchMessage( &msg );
  97.    }
  98.  
  99.    return msg.wParam;
  100. } // WinMain()
  101.  
  102.  
  103. /********************************************************************\
  104. * Function: LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) *
  105. *                                                                    *
  106. *  Purpose: Processes Application Messages                           *
  107. *                                                                    *
  108. * Comments: The following messages are processed                     *
  109. *                                                                    *
  110. *           WM_COMMAND                                               *
  111. *           WM_DESTROY                                               *
  112. *                                                                    *
  113. *                                                                    *
  114. \********************************************************************/
  115.  
  116.  
  117. LRESULT CALLBACK MainWndProc( HWND hWnd,
  118.                               UINT msg,
  119.                               WPARAM wParam,
  120.                               LPARAM lParam )
  121. {
  122.    switch( msg )
  123.    {
  124.  
  125. /********************************************************************\
  126. *     WM_COMMAND: Handle menu selections for Exit, GetFreeSpace,     *
  127. *        WNetGetUser, and Help About                                 *
  128. \********************************************************************/
  129.  
  130.       case WM_COMMAND:
  131.          switch( wParam )
  132.          {
  133.             case IDM_EXIT:
  134.                SendMessage( hWnd, WM_CLOSE, 0, 0 );
  135.                break;
  136.  
  137.             case IDM_GETUSER:
  138.             {
  139.                CHAR buf1[255], buf2[255];
  140.            UINT bufsize = 255;
  141.  
  142.                MyWNetGetUser( NULL, buf1, &bufsize );
  143.                wsprintf( buf2, "User is %s", buf1 );
  144.                MessageBox( hWnd, buf2, "UTSamp", MB_OK );
  145.                break;
  146.             }
  147.  
  148.             case IDM_GETMEM:
  149.             {
  150.                CHAR buf[255];
  151.  
  152.                wsprintf( buf, "Free Ram = 0x%x", MyGetFreeSpace() );
  153.                MessageBox( hWnd, buf, "UTSamp", MB_OK );
  154.                break;
  155.             }
  156.  
  157.             case IDM_CALLBACK1:
  158.                GenerateCallback( 1 );
  159.                break;
  160.  
  161.             case IDM_CALLBACK2:
  162.                GenerateCallback( 2 );
  163.                break;
  164.  
  165.             case IDM_ABOUT:
  166.                DialogBox( ghInstance,
  167.                           "AboutDlg",
  168.                           hWnd,
  169.                           (DLGPROC) AboutDlgProc );
  170.                break;
  171.          }
  172.          break;
  173.  
  174. /**************************************************************\
  175. *     WM_DESTROY: PostQuitMessage() is called                  *
  176. \**************************************************************/
  177.  
  178.       case WM_DESTROY:
  179.          PostQuitMessage( 0 );
  180.          break;
  181.  
  182. /**************************************************************\
  183. *     Let the default window proc handle all other messages    *
  184. \**************************************************************/
  185.  
  186.       default:
  187.          return( DefWindowProc( hWnd, msg, wParam, lParam ) );
  188.    }
  189.  
  190.    return 0;
  191. } // MainWndProc()
  192.  
  193. /********************************************************************\
  194. * Function: LRESULT CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM)*
  195. *                                                                    *
  196. *  Purpose: Processes "About" Dialog Box Messages                    *
  197. *                                                                    *
  198. * Comments: The Dialog Box is displayed when the user selects        *
  199. *           Help.About.  The following messages are processed:       *
  200. *                                                                    *
  201. *           WM_INITDIALOG                                            *
  202. *           WM_COMMAND                                               *
  203. *                                                                    *
  204. \********************************************************************/
  205.  
  206.  
  207. LRESULT CALLBACK AboutDlgProc( HWND hDlg,
  208.                                UINT uMsg,
  209.                                WPARAM wParam,
  210.                                LPARAM lParam )
  211. {
  212.    switch( uMsg )
  213.    {
  214.       case WM_INITDIALOG:
  215.          return TRUE;
  216.  
  217.       case WM_COMMAND:
  218.          switch( wParam )
  219.          {
  220.             case IDOK:
  221.                EndDialog( hDlg, TRUE );
  222.                return TRUE;
  223.          }
  224.          break;
  225.    }
  226.  
  227.    return FALSE;
  228. } //AboutDlgProc()
  229.