home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / rpc / whello / whelloc.c < prev    next >
C/C++ Source or Header  |  1996-06-11  |  25KB  |  724 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                         whello Example
  5.  
  6.     FILE:       whelloc.c
  7.  
  8.     PURPOSE:    RPC sample Windows client
  9.                 Based on Win 3.x SDK Generic template for Windows applications
  10.  
  11.     FUNCTIONS:  WinMain()         - call initialization function,
  12.                                     process message loop
  13.                 InitApplication() - initializ window data and register window
  14.                 InitInstance()    - save instance handle and create main window
  15.                 MainWndProc()     - process messages
  16.  
  17.                 About()    - process messages for "About" dialog box
  18.                 Server()   - process messages for "Server" dialog box
  19.                 Endpoint() - process messages for "Endpoint" dialog box
  20.                 Send()     - process messages for "Send" dialog box;
  21.                              sends on OK
  22.  
  23.                 midl_user_allocate() - memory allocation function needed by RPC
  24.                 midl_user_free()     - memory free function needed by RPC
  25.  
  26.     COMMENTS:   Windows version of the "Hello, world" example.
  27.  
  28.                 Windows can have several copies of your application
  29.                 running at the same time.  The variable hInst keeps
  30.                 track of which instance the application is so that
  31.                 processing will be to the correct window.
  32.  
  33. ****************************************************************************/
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <windows.h>
  38. #include <windowsx.h>       // select between win16 or win32
  39. #include <string.h>
  40. #include "whello.h"         // header file generated by MIDL compiler
  41. #include "whelloc.h"        // client-specific header file
  42.  
  43. /* global data */
  44.  
  45. unsigned char   pszProtocolSequence[MAXPROTSEQ];
  46. unsigned char   pszNetworkAddress[UNCLEN+1];
  47. unsigned char   pszEndpoint[PATHLEN+1];
  48. unsigned char * pszUuid            = NULL;
  49. unsigned char * pszOptions         = NULL;
  50. unsigned char * pszStringBinding   = NULL;
  51. unsigned char   pszString[MSGLEN];
  52.  
  53. int fBound = FALSE;     // flag indicates whether client bound to server
  54.  
  55. HANDLE hInst;           // current instance
  56. HCURSOR hHourGlass;     // during calls to RPC API functions
  57.  
  58.  
  59. /****************************************************************************
  60.  
  61.     FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  62.  
  63.     PURPOSE:  calls initialization function, processes message loop
  64.  
  65.     COMMENTS: Windows recognizes this function by name as the initial
  66.               entry point for the program.  This function calls the
  67.               application initialization routine, if no other instance
  68.               of the program is running, and always calls the instance
  69.               initialization routine.  It then executes a message
  70.               retrieval and dispatch loop that is the top-level control
  71.               structure for the remainder of execution.  The loop is
  72.               terminated when a WM_QUIT message is received, at which
  73.               time this function exits the application instance by
  74.               returning the value passed by PostQuitMessage().
  75.  
  76.               If this function must abort before entering the message
  77.               loop, it returns the conventional value NULL.
  78.  
  79. ****************************************************************************/
  80.  
  81. int WINAPI WinMain(HINSTANCE hInstance,     // current instance
  82.                    HINSTANCE hPrevInstance, // previous instance
  83.                    LPSTR     lpCmdLine,     // command line
  84.                    int       nCmdShow)      // show-window type (open/icon)
  85. {
  86.     MSG msg;
  87.  
  88.     UNREFERENCED_PARAMETER(lpCmdLine);
  89.  
  90.     if (!hPrevInstance)                   // Other instances of app running?
  91.         if (!InitApplication(hInstance))  // Initialize shared things
  92.             return(FALSE);               // Exits if unable to initialize
  93.  
  94.     /* Perform initializations that apply to a specific instance */
  95.     if (!InitInstance(hInstance, nCmdShow))
  96.         return(FALSE);
  97.  
  98.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  99.     while (GetMessage(&msg,        // message structure
  100.                       (HWND)NULL,  // handle of window receiving the message
  101.                       0,           // lowest message to examine
  102.                       0))          // highest message to examine
  103.     {
  104.         TranslateMessage(&msg);    // Translates virtual key codes
  105.         DispatchMessage(&msg);     // Dispatches message to window
  106.     }
  107.  
  108.     return(msg.wParam);            // Returns the value from PostQuitMessage
  109. }
  110.  
  111.  
  112. /****************************************************************************
  113.  
  114.     FUNCTION: InitApplication(HANDLE)
  115.  
  116.     PURPOSE:  Initializes window data and registers window class
  117.  
  118.     COMMENTS: This function is called at initialization time only if
  119.               no other instances of the application are running.  This
  120.               function performs initialization tasks that can be done
  121.               once for any number of running instances.
  122.  
  123.               In this case, we initialize a window class by filling out
  124.               a data structure of type WNDCLASS and calling the Windows
  125.               RegisterClass() function.  Since all instances of this
  126.               application use the same window class, we only need to do
  127.               this when the first instance is initialized.
  128.  
  129. ****************************************************************************/
  130.  
  131. BOOL InitApplication(HANDLE hInstance)    // current instance
  132. {
  133.     WNDCLASS  wc;
  134.  
  135.     /* Fill in window class structure with parameters that */
  136.     /* describe the main window.                           */
  137.     wc.style = 0;
  138.     wc.lpfnWndProc = (WNDPROC)MainWndProc;
  139.     wc.cbClsExtra = 0;
  140.     wc.cbWndExtra = 0;
  141.     wc.hInstance = hInstance;
  142.     wc.hIcon = LoadIcon(hInstance, "HelloIcon");
  143.     wc.hCursor = LoadCursor(0, IDC_ARROW);
  144.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  145.     wc.lpszMenuName =  "GenericMenu";
  146.     wc.lpszClassName = "GenericWClass";
  147.  
  148.     /* Register the window class and return success/failure code. */
  149.     return(RegisterClass(&wc));
  150. }
  151.  
  152.  
  153. /****************************************************************************
  154.  
  155.     FUNCTION: InitInstance(HANDLE, int)
  156.  
  157.     PURPOSE:  Saves instance handle and creates main window
  158.  
  159.     COMMENTS: This function is called at initialization time for every
  160.               instance of this application.  This function performs
  161.               initialization tasks that cannot be shared by multiple
  162.               instances.
  163.  
  164.               In this case, we save the instance handle in a static
  165.               variable and create and display the main program window.
  166.  
  167. ****************************************************************************/
  168.  
  169. BOOL InitInstance(HANDLE hInstance,    // Current instance identifier.
  170.                   int    nCmdShow)     // Param for first ShowWindow() call.
  171. {
  172.     HWND hWnd;    // Main window handle.
  173.  
  174.     /* Save the instance handle in static variable, which will be used in  */
  175.     /* many subsequence calls from this application to Windows.            */
  176.     hInst = hInstance;
  177.     hHourGlass = LoadCursor(0, IDC_WAIT);
  178.  
  179.     /* Create a main window for this application instance.  */
  180.     hWnd = CreateWindow("GenericWClass",           // See RegisterClass() call.
  181.                         "RPC Sample Application",  // Text for window title bar.
  182.                         WS_OVERLAPPEDWINDOW,       // Window style.
  183.                         CW_USEDEFAULT,    // Default horizontal position.
  184.                         CW_USEDEFAULT,    // Default vertical position.
  185.                         CW_USEDEFAULT,    // Default width.
  186.                         CW_USEDEFAULT,    // Default height.
  187.                         (HWND) NULL,      // Overlapped windows have no parent.
  188.                         (HMENU) NULL,     // Use the window class menu.
  189.                         hInstance,        // This instance owns this window.
  190.                         (LPVOID) NULL     // Pointer not needed.
  191.                         );
  192.  
  193.     /* If window could not be created, return "failure" */
  194.     if (!hWnd)
  195.         return(FALSE);
  196.  
  197.     /* Initialize RPC binding data */
  198.     strcpy(pszProtocolSequence, DEFAULT_PROT_SEQ);
  199.     strcpy(pszEndpoint, DEFAULT_ENDPOINT);
  200.     pszNetworkAddress[0] = '\0';
  201.     strcpy(pszString, DEFAULT_MESSAGE);
  202.  
  203.     /* Make the window visible; update its client area; and return "success" */
  204.     ShowWindow(hWnd, nCmdShow);  // Show the window
  205.     UpdateWindow(hWnd);          // Send WM_PAINT message
  206.  
  207.     return(TRUE);                // Return the value from PostQuitMessage
  208. }
  209.  
  210.  
  211. /****************************************************************************
  212.  
  213.     FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
  214.  
  215.     PURPOSE:  Processes messages
  216.  
  217.     MESSAGES: WM_COMMAND  - application menu (About dialog box)
  218.               WM_DESTROY  - destroy window
  219.  
  220.     COMMENTS: To process the IDM_ABOUT message, call MakeProcInstance()
  221.               to get the current instance address of the About() function.
  222.               Then call Dialog box which will create the box according to
  223.               the information in your generic.rc file and turn control
  224.               over to the About() function.  When it returns, free the
  225.               intance address.
  226.  
  227. ****************************************************************************/
  228.  
  229. long APIENTRY MainWndProc(HWND   hWnd,       // window handle
  230.                           UINT   message,    // type of message
  231.                           WPARAM wParam,     // additional information
  232.                           LPARAM lParam)     // additional information
  233. {
  234.     DLGPROC lpProc;     // pointer to the dialog box function
  235.  
  236.     switch (message) {
  237.  
  238.     case WM_CREATE:
  239.  
  240. #ifdef WIN16
  241.         RpcWinSetYieldInfo (hWnd, FALSE, 0, 0L); // To make TCP/IP happy
  242. #else
  243.         PostMessage(hWnd, WM_COMMAND, IDM_BIND, 0L);    // bind to server
  244. #endif
  245.         break;
  246.  
  247.     case WM_COMMAND:    // message: command from application menu
  248.         switch (wParam) {
  249.  
  250.         case IDM_BIND:
  251.             if (Bind(hWnd) != RPC_S_OK)
  252.                 PostMessage(hWnd, WM_DESTROY, 0, 0L);
  253.             break;
  254.  
  255.         case IDM_ABOUT:
  256.             lpProc = MakeProcInstance(About, hInst);
  257.             DialogBox(hInst,        // current instance
  258.                       "AboutBox",   // resource to use
  259.                       hWnd,         // parent handle
  260.                       lpProc);      // About() instance address
  261.             FreeProcInstance(lpProc);
  262.             break;
  263.  
  264.         case IDM_PROTSEQ:
  265.             lpProc = MakeProcInstance(Protseq, hInst);
  266.             DialogBox(hInst,        // current instance
  267.                       "ProtseqBox", // resource to use
  268.                       hWnd,         // parent handle
  269.                       lpProc);      // Server instance address
  270.             FreeProcInstance(lpProc);
  271.             break;
  272.  
  273.         case IDM_SERVER:
  274.             lpProc = MakeProcInstance(Server, hInst);
  275.             DialogBox(hInst,        // current instance
  276.                       "ServerBox",  // resource to use
  277.                       hWnd,         // parent handle
  278.                       lpProc);      // Server instance address
  279.             FreeProcInstance(lpProc);
  280.             break;
  281.  
  282.         case IDM_ENDPOINT:
  283.             lpProc = MakeProcInstance(Endpoint, hInst);
  284.             DialogBox(hInst,        // current instance
  285.                       "EndpointBox",// resource to use
  286.                       hWnd,         // parent handle
  287.                       lpProc);      // Server instance address
  288.             FreeProcInstance(lpProc);
  289.             break;
  290.  
  291.         case IDM_SEND:
  292.             lpProc = MakeProcInstance(Send, hInst);
  293.             DialogBox(hInst,        // current instance
  294.                       "SendBox",    // resource to use
  295.                       hWnd,         // parent handle
  296.                       lpProc);      // Server instance address
  297.             FreeProcInstance(lpProc);
  298.             break;
  299.  
  300.         case IDM_EXIT:
  301.             DestroyWindow(hWnd);
  302.             if (fBound == TRUE) {
  303.                 RpcTryExcept {
  304.                     Shutdown();     // shut down the server
  305.                 }
  306.                 RpcExcept(1) {
  307.                     MessageBox(hWnd,
  308.                                EXCEPT_MSG,
  309.                                "Remote Procedure Call",
  310.                                MB_ICONINFORMATION);
  311.                 }
  312.                 RpcEndExcept
  313.             }
  314.             break;
  315.  
  316.         default:        // Let Windows process it
  317.             return(DefWindowProc(hWnd, message, wParam, lParam));
  318.  
  319.         }
  320.         break;
  321.  
  322.     case WM_DESTROY:    // message: window being destroyed
  323.         PostQuitMessage(0);
  324.         break;
  325.  
  326.     default:            // Passes it on if unprocessed
  327.         return(DefWindowProc(hWnd, message, wParam, lParam));
  328.  
  329.     }
  330.  
  331.     return(0);
  332. }
  333.  
  334.  
  335. /****************************************************************************
  336.  
  337.     FUNCTION: Protseq(HWND, unsigned, WORD, LONG)
  338.  
  339.     PURPOSE:  Processes messages for "Protseq" dialog box
  340.  
  341.     MESSAGES: WM_INITDIALOG - initialize dialog box
  342.               WM_COMMAND    - Input received
  343.  
  344.     COMMENTS: No initialization is needed for this particular dialog box,
  345.               but TRUE must be returned to Windows.
  346.  
  347.               Wait for user to click on "Ok" button, then close the dialog box.
  348.  
  349. ****************************************************************************/
  350. BOOL APIENTRY Protseq(HWND hDlg,       // window handle of the dialog box
  351.                      UINT message,     // type of message
  352.                      UINT wParam,      // message-specific information
  353.                      LONG lParam)
  354. {
  355.  
  356.     UNREFERENCED_PARAMETER(lParam);
  357.  
  358.     switch (message) {
  359.  
  360.     case WM_INITDIALOG:    // message: initialize dialog box
  361.         SetDlgItemText((HANDLE)hDlg, IDD_PROTSEQNAME, pszProtocolSequence);
  362.         return(TRUE);
  363.  
  364.     case WM_COMMAND:       // message: received a command
  365.         switch(wParam) {
  366.  
  367.         case IDCANCEL:     // System menu close command?
  368.             EndDialog(hDlg, FALSE);
  369.             return(TRUE);
  370.  
  371.         case IDOK:         // "OK" box selected?
  372.             GetDlgItemText(hDlg, IDD_PROTSEQNAME, pszProtocolSequence, MAXPROTSEQ);
  373.  
  374.             if (Bind(hDlg) != RPC_S_OK) {
  375.                 EndDialog(hDlg, FALSE);
  376.                 return(FALSE);
  377.             }
  378.  
  379.             EndDialog(hDlg, TRUE);
  380.             return(TRUE);
  381.  
  382.         }
  383.  
  384.     }
  385.  
  386.     return(FALSE);  // Didn't process a message
  387. }
  388.  
  389.  
  390. /****************************************************************************
  391.  
  392.     FUNCTION: Server(HWND, unsigned, WORD, LONG)
  393.  
  394.     PURPOSE:  Processes messages for "Server" dialog box
  395.  
  396.     MESSAGES: WM_INITDIALOG - initialize dialog box
  397.               WM_COMMAND    - Input received
  398.  
  399.     COMMENTS: No initialization is needed for this particular dialog box,
  400.               but TRUE must be returned to Windows.
  401.  
  402.               Wait for user to click on "Ok" button, then close the dialog box.
  403.  
  404. ****************************************************************************/
  405.  
  406. BOOL APIENTRY Server(HWND hDlg,        // window handle of the dialog box
  407.                      UINT message,     // type of message
  408.                      UINT wParam,      // message-specific information
  409.                      LONG lParam)
  410. {
  411.     HCURSOR hOld;
  412.  
  413.     UNREFERENCED_PARAMETER(lParam);
  414.  
  415.     switch (message) {
  416.  
  417.     case WM_INITDIALOG:    // message: initialize dialog box
  418.         SetDlgItemText((HANDLE)hDlg, IDD_SERVERNAME, pszNetworkAddress);
  419.         return(TRUE);
  420.  
  421.     case WM_COMMAND:       // message: received a command
  422.         switch(wParam) {
  423.  
  424.         case IDCANCEL:     // System menu close command?
  425.             EndDialog(hDlg, FALSE);
  426.             return(TRUE);
  427.  
  428.         case IDOK:         // "OK" box selected?
  429.             GetDlgItemText(hDlg, IDD_SERVERNAME, pszNetworkAddress, UNCLEN);
  430.  
  431.             hOld = SetCursor(hHourGlass);
  432.             if (Bind(hDlg) != RPC_S_OK) {
  433.                 EndDialog(hDlg, FALSE);
  434.                 return(FALSE);
  435.             }
  436.  
  437.             SetCursor(hOld);
  438.             EndDialog(hDlg, TRUE);
  439.             return(TRUE);
  440.  
  441.         }
  442.  
  443.     }
  444.  
  445.     return(FALSE);  // Didn't process a message
  446. }
  447.  
  448.  
  449. /****************************************************************************
  450.  
  451.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  452.  
  453.     PURPOSE:  Processes messages for "About" dialog box
  454.  
  455.     MESSAGES: WM_INITDIALOG - initialize dialog box
  456.               WM_COMMAND    - Input received
  457.  
  458.     COMMENTS: No initialization is needed for this particular dialog box,
  459.               but TRUE must be returned to Windows.
  460.  
  461.               Wait for user to click on "Ok" button, then close the dialog box.
  462.  
  463. ****************************************************************************/
  464.  
  465. BOOL APIENTRY About(HWND hDlg,         // window handle of the dialog box
  466.                     UINT message,      // type of message
  467.                     UINT wParam,       // message-specific information
  468.                     LONG lParam)
  469. {
  470.     UNREFERENCED_PARAMETER(lParam);
  471.  
  472.     switch (message) {
  473.  
  474.     case WM_INITDIALOG:    // message: initialize dialog box
  475.         return(TRUE);
  476.  
  477.     case WM_COMMAND:       // message: received a command
  478.         if (wParam == IDOK || wParam == IDCANCEL) {
  479.            EndDialog(hDlg, TRUE);
  480.            return(TRUE);
  481.         }
  482.         break;
  483.  
  484.     }
  485.  
  486.     return(FALSE);  // Didn't process a message
  487. }
  488.  
  489.  
  490. /****************************************************************************
  491.  
  492.     FUNCTION: Endpoint(HWND, unsigned, WORD, LONG)
  493.  
  494.     PURPOSE:  Processes messages for "Endpoint" dialog box
  495.  
  496.     MESSAGES: WM_INITDIALOG - initialize dialog box
  497.               WM_COMMAND    - Input received
  498.  
  499.     COMMENTS: No initialization is needed for this particular dialog box,
  500.               but TRUE must be returned to Windows.
  501.  
  502.               Wait for user to click on "Ok" button, then close the dialog box.
  503.  
  504. ****************************************************************************/
  505.  
  506. BOOL APIENTRY Endpoint(HWND hDlg,        // window handle of the dialog box
  507.                        UINT message,     // type of message
  508.                        UINT wParam,      // message-specific information
  509.                        LONG lParam)
  510. {
  511.     HCURSOR hOld;
  512.  
  513.     UNREFERENCED_PARAMETER(lParam);
  514.  
  515.     switch (message) {
  516.  
  517.     case WM_INITDIALOG:    // message: initialize dialog box
  518.         SetDlgItemText(hDlg, IDD_ENDPOINTNAME, pszEndpoint);
  519.         return(TRUE);
  520.  
  521.     case WM_COMMAND:       // message: received a command
  522.         switch(wParam) {
  523.  
  524.         case IDCANCEL:
  525.             EndDialog(hDlg, FALSE);
  526.             return(TRUE);
  527.  
  528.         case IDOK:
  529.             GetDlgItemText(hDlg, IDD_ENDPOINTNAME, pszEndpoint, PATHLEN);
  530.  
  531.             hOld = SetCursor(hHourGlass);
  532.             if (Bind(hDlg) != RPC_S_OK) {
  533.                 EndDialog(hDlg, FALSE);
  534.                 return(FALSE);
  535.             }
  536.  
  537.             SetCursor(hOld);
  538.             EndDialog(hDlg, TRUE);
  539.             return(TRUE);
  540.  
  541.         }
  542.  
  543.     }
  544.  
  545.     return(FALSE);  // Didn't process a message
  546. }
  547.  
  548.  
  549. /****************************************************************************
  550.  
  551.     FUNCTION: Send(HWND, unsigned, WORD, LONG)
  552.  
  553.     PURPOSE:  Processes messages for "Send" dialog box
  554.  
  555.     MESSAGES: WM_INITDIALOG - initialize dialog box
  556.               WM_COMMAND    - Input received
  557.  
  558.     COMMENTS: No initialization is needed for this particular dialog box,
  559.               but TRUE must be returned to Windows.
  560.  
  561.               Wait for user to click on "Ok" button, then close the dialog box.
  562.  
  563. ****************************************************************************/
  564.  
  565. BOOL APIENTRY Send(HWND hDlg,        // window handle of the dialog box
  566.                    UINT message,     // type of message
  567.                    UINT wParam,      // message-specific information
  568.                    LONG lParam)
  569. {
  570.     UNREFERENCED_PARAMETER(lParam);
  571.  
  572.     switch (message) {
  573.  
  574.     case WM_INITDIALOG:    // message: initialize dialog box
  575.         SetDlgItemText(hDlg, IDD_MESSAGE, pszString);
  576.         return(TRUE);
  577.  
  578.     case WM_COMMAND:       // message: received a command
  579.         switch(wParam) {
  580.  
  581.         case IDCANCEL:
  582.             EndDialog(hDlg, FALSE);
  583.             return(TRUE);
  584.  
  585.         case IDOK:
  586.             GetDlgItemText(hDlg, IDD_MESSAGE, pszString, MSGLEN);
  587.  
  588.             RpcTryExcept {
  589.                 HelloProc(pszString);  // make call with user message
  590.             }
  591.             RpcExcept(1) {
  592.                 char pszFail[MSGLEN];
  593.  
  594.                 sprintf(pszFail, "%s (0x%x)\n", EXCEPT_MSG, RpcExceptionCode());
  595.                 MessageBox(hDlg,
  596.                            pszFail,
  597.                            "Remote Procedure Call",
  598.                            MB_ICONINFORMATION);
  599.             }
  600.             RpcEndExcept
  601.  
  602.             EndDialog(hDlg, TRUE);
  603.             return(TRUE);
  604.  
  605.         }
  606.  
  607.     }
  608.  
  609.     return(FALSE);  // Didn't process a message
  610. }
  611.  
  612.  
  613. /****************************************************************************
  614.  
  615.     FUNCTION: midl_user_allocate(size_t)
  616.  
  617.     PURPOSE:  Allocate memory as needed by the RPC runtime library
  618.  
  619.     COMMENTS: The stubs or runtime libraries may need to allocate memory.
  620.               By convention, they call a user-specified function named
  621.               midl_user_allocate.  In this application, no memory
  622.               management is needed, so a dummy function is provided.
  623.  
  624. ****************************************************************************/
  625.  
  626. void __RPC_FAR * __RPC_API midl_user_allocate(size_t len)
  627. {
  628.     UNREFERENCED_PARAMETER(len);
  629.     return(NULL);  // no memory management required
  630. }
  631.  
  632.  
  633. /****************************************************************************
  634.  
  635.     FUNCTION: midl_user_free(void *)
  636.  
  637.     PURPOSE:  Free memory as needed by the RPC runtime library
  638.  
  639.     COMMENTS: The stubs or runtime libraries may need to free memory.
  640.               By convention, they call a user-specified function named
  641.               midl_user_free.  In this application, no memory allocation
  642.               is needed so a dummy function is provided.
  643.  
  644. ****************************************************************************/
  645.  
  646. void __RPC_API midl_user_free(void __RPC_FAR * ptr)
  647. {
  648.     UNREFERENCED_PARAMETER(ptr);
  649.     return;    // no memory management required
  650. }
  651.  
  652.  
  653. /****************************************************************************
  654.  
  655.     FUNCTION: Bind(HWND)
  656.  
  657.     PURPOSE:  Make RPC API calls to bind to the server application
  658.  
  659.     COMMENTS: The binding calls are made from InitInstance() and
  660.               whenever the user changes the server name or endpoint.
  661.               If the bind operation is successful, the global flag
  662.               fBound is set to TRUE.
  663.  
  664.               The global flag fBound is used to determine whether to
  665.               call the RPC API function RpcBindingFree.
  666.  
  667. ****************************************************************************/
  668.  
  669. RPC_STATUS Bind(HWND hWnd)
  670. {
  671.     RPC_STATUS status;
  672.     char pszFail[MSGLEN];
  673.  
  674.     if (fBound == TRUE) {  // unbind only if bound
  675.         status = RpcStringFree(&pszStringBinding);
  676.         if (status) {
  677.             MessageBox(hWnd, "RpcStringFree failed", "RPC Error", MB_ICONSTOP);
  678.             return(status);
  679.         }
  680.  
  681.         status = RpcBindingFree(&hWHello);
  682.         if (status) {
  683.             MessageBox(hWnd, "RpcBindingFree failed", "RPC Error", MB_ICONSTOP);
  684.             return(status);
  685.         }
  686.  
  687.         fBound = FALSE;  // unbind successful; reset flag
  688.     }
  689.  
  690.     status = RpcStringBindingCompose(pszUuid,
  691.                                      pszProtocolSequence,
  692.                                      pszNetworkAddress,
  693.                                      pszEndpoint,
  694.                                      pszOptions,
  695.                                      &pszStringBinding);
  696.     if (status) {
  697.         sprintf(pszFail, "RpcStringBindingCompose failed: (0x%x)\nNetwork Address = %s\n",
  698.                 status, pszNetworkAddress);
  699.         MessageBox(hWnd,
  700.                    pszFail,
  701.                    "RPC Runtime Error",
  702.                    MB_ICONEXCLAMATION);
  703.         return(status);
  704.     }
  705.  
  706.     status = RpcBindingFromStringBinding(pszStringBinding,
  707.                                          &hWHello);
  708.     if (status) {
  709.         sprintf(pszFail, "RpcBindingFromStringBinding failed: (0x%x)\nString = %s\n",
  710.                 status, pszStringBinding);
  711.         MessageBox(hWnd,
  712.                    pszFail,
  713.                    "RPC Runtime Error",
  714.                    MB_ICONEXCLAMATION);
  715.         return(status);
  716.     }
  717.  
  718.     fBound = TRUE;  // bind successful; reset flag
  719.  
  720.     return(status);
  721. }
  722.  
  723. /**** end whelloc.c ****/
  724.