--- mstools/samples/rpc/mandel/mandel.c 2018/08/09 18:20:56 1.1.1.2 +++ mstools/samples/rpc/mandel/mandel.c 2018/08/09 18:22:00 1.1.1.3 @@ -29,17 +29,9 @@ #endif #include /* Required for all Windows applications */ +#include /* Allow portability from Win16, Win32 */ #include "mandel.h" /* Specific to this program */ -#define MENUNAME "MandelMenu" -#define CLASSNAME "MandelClass" -#define ABOUTBOX "AboutBox" -#define SAVEBOX "SAVEBOX" - - -#define POLL_TIME 100 -#define LINES 4 - #ifdef RPC char szTitle[] = "Mandelbrot RPC"; #else @@ -78,7 +70,16 @@ int iHistMaxI = 2; int iHistMaxJ = 3; RECT rcZoom; BOOL fRectDefined = FALSE; - +#ifdef RPC + int fBound = FALSE; /* flag indicates whether bound to svr */ + RPC_STATUS status; // returned by RPC API function + unsigned char * pszUuid = NULL; + unsigned char * pszProtocolSequence = "ncacn_np"; + unsigned char * pszEndpoint = "\\pipe\\mandel"; + unsigned char * pszOptions = NULL; + unsigned char * pszStringBinding; + unsigned char szNetworkAddress[UNCLEN+1] = {'\0'}; +#endif /* * @@ -110,14 +111,19 @@ int nCmdShow; /* show-window typ { MSG msg; /* message */ - UNREFERENCED_PARAMETER(lpCmdLine); +#ifdef RPC + unsigned long ulRpcException; +#endif +#ifdef RPC // add a runtime exception handler for RPC version // see below for additional exception handling code -#ifdef RPC RpcTryExcept { #endif + UNREFERENCED_PARAMETER(lpCmdLine); + + if (!hPrevInstance) /* Other instances of app running? */ if (!InitApplication(hInstance)) /* Initialize shared things */ return (FALSE); /* Exits if unable to initialize */ @@ -129,9 +135,8 @@ RpcTryExcept { /* Acquire and dispatch messages until a WM_QUIT message is received. */ - while (GetMessage(&msg, /* message structure */ - NULL, /* handle of window receiving the message */ + (HWND)NULL, /* handle of window receiving the message */ 0, /* lowest message to examine */ 0)) /* highest message to examine */ { @@ -139,20 +144,20 @@ RpcTryExcept { DispatchMessage(&msg); /* Dispatches message to window */ } - return (msg.wParam); /* Returns the value from PostQuitMessage */ - // add a runtime exception handler for RPC version // see above for additional exception handling code #ifdef RPC } // end of RpcTryExcept statements RpcExcept(1) { - sprintf(pszFail, "Please start the server application."); - MessageBox(msg.hwnd, pszFail, "Mandel Server Not Started", - MB_ICONHAND | MB_SYSTEMMODAL); + ulRpcException = RpcExceptionCode(); + sprintf(pszFail, "Runtime exception 0x%lx = %ld\n", ulRpcException, ulRpcException); + MessageBox(msg.hwnd, pszFail, "RPC Sample Application", + MB_ICONINFORMATION | MB_SYSTEMMODAL); } RpcEndExcept #endif + return (msg.wParam); /* Returns the value from PostQuitMessage */ } // end WinMain /* @@ -188,19 +193,15 @@ HANDLE hInstance; /* current in wc.cbWndExtra = 0; /* No per-window extra data. */ wc.hInstance = hInstance; /* Application that owns the class. */ wc.hIcon = LoadIcon(hInstance, "RPC_ICON"); - wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = GetStockObject(WHITE_BRUSH); wc.lpszMenuName = MENUNAME; /* Name of menu resource in .RC file. */ wc.lpszClassName = CLASSNAME; /* Name used in call to CreateWindow. */ /* Register the window class and return success/failure code. */ - return (RegisterClass(&wc)); } - - /* * FUNCTION: InitInstance(HANDLE, int) * @@ -222,6 +223,7 @@ BOOL InitInstance(hInstance, nCmdShow) { HWND hWnd; /* Main window handle. */ RECT rc; + HMENU hMenu; /* Save the instance handle in static variable, which will be used in */ /* many subsequence calls from this application to Windows. */ @@ -238,27 +240,31 @@ BOOL InitInstance(hInstance, nCmdShow) CW_USEDEFAULT, /* Default vertical position. */ WIDTH, /* Default width. */ HEIGHT, /* Default height. */ - NULL, /* Overlapped windows have no parent. */ - NULL, /* Use the window class menu. */ + (HWND)NULL, /* Overlapped windows have no parent. */ + (HMENU)NULL, /* Use the window class menu. */ hInstance, /* This instance owns this window. */ - NULL /* Pointer not needed. */ + (void FAR *)NULL /* Pointer not needed. */ ); /* If window could not be created, return "failure" */ if (!hWnd) - return (FALSE); + return (FALSE); /* Make the window visible; update its client area; and return "success" */ ShowWindow(hWnd, nCmdShow); /* Show the window */ - UpdateWindow(hWnd); /* Sends WM_PAINT message */ - + UpdateWindow(hWnd); /* Sends WM_PAINT message */ rc.top = rc.left = 0; rc.bottom = HEIGHT-1; rc.right = WIDTH-1; SetNewCalc(cptUL, dPrec, rc); + hMenu = GetMenu(hWnd); + +#ifndef RPC + EnableMenuItem(hMenu, IDM_SERVER, MF_GRAYED); /* disable option */ +#endif return (TRUE); /* Returns the value from PostQuitMessage */ } @@ -272,16 +278,11 @@ BOOL InitInstance(hInstance, nCmdShow) * * MESSAGES: * - * WM_COMMAND - application menu (About dialog box) + * WM_COMMAND - application menu * WM_DESTROY - destroy window * * COMMENTS: * - * To process the IDM_ABOUT message, call MakeProcInstance() to get - * the current instance address of the About() function, then call - * DialogBox to create the box according to the information in the - * MANDEL.RC file and turn control over to the About() function. - * When it returns, free the intance address. */ LONG APIENTRY MainWndProc( @@ -290,7 +291,7 @@ UINT message, /* type of mes UINT wParam, /* additional information */ LONG lParam) /* additional information */ { - FARPROC lpProcAbout; /* pointer to the "About" function */ + DLGPROC lpProc; /* pointer to the dialog box function */ PAINTSTRUCT ps; HDC hdc; static HDC hdcMem; @@ -310,7 +311,12 @@ LONG lParam) /* additional informat { case WM_CREATE: - if (!InitRemote(hWnd)) +#ifdef WIN16 + PostMessage(hWnd, WM_COMMAND, IDM_SERVER, 0L); /* force server spec */ +#else + PostMessage(hWnd, WM_COMMAND, IDM_BIND, 0L); /* bind to server */ +#endif + if (!InitRemote(hWnd)) return FALSE; InitHistogram(); @@ -335,8 +341,7 @@ LONG lParam) /* additional informat CheckMenuItem(GetMenu(hWnd), IDM_4LINES, MF_CHECKED); CheckMenuItem(GetMenu(hWnd), IDM_CONTINUOUS, MF_CHECKED); miOldLines = IDM_4LINES; // save to uncheck - - break; + break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); @@ -351,16 +356,22 @@ LONG lParam) /* additional informat case WM_COMMAND: /* message: command from application menu */ switch(wParam) { + case IDM_BIND: +#ifdef RPC + if (Bind(hWnd)) + PostMessage(hWnd, WM_DESTROY, 0, 0L); +#endif + break; case IDM_ABOUT: - lpProcAbout = MakeProcInstance((FARPROC)About, hInst); + lpProc = MakeProcInstance(About, hInst); DialogBox(hInst, /* current instance */ ABOUTBOX, /* resource to use */ hWnd, /* parent handle */ - lpProcAbout); /* About() instance address */ + lpProc); /* About() instance address */ - FreeProcInstance(lpProcAbout); + FreeProcInstance(lpProc); break; case IDM_ZOOMOUT: @@ -449,7 +460,7 @@ LONG lParam) /* additional informat DoSomeWork(hWnd, FALSE); break; - case IDM_EXIT: + case IDM_EXIT: DestroyWindow(hWnd); break; @@ -488,14 +499,23 @@ LONG lParam) /* additional informat } CheckMenuItem(GetMenu(hWnd), miOldLines, MF_CHECKED); - break; + break; +#ifdef RPC + case IDM_SERVER: + lpProc = MakeProcInstance(Server, hInst); + DialogBox(hInst, /* current instance */ + "ServerBox", /* resource to use */ + hWnd, /* parent handle */ + lpProc); /* Server instance address */ + FreeProcInstance(lpProc); + break; +#endif default: /* Lets Windows process it */ return (DefWindowProc(hWnd, message, wParam, lParam)); } break; - case WM_DESTROY: /* message: window being destroyed */ PostQuitMessage(0); DeleteDC(hdcMem); @@ -928,3 +948,117 @@ void * MIDL_user_allocate(size_t len) UNREFERENCED_PARAMETER(len); return(NULL); } + +/**************************************************************************** + + FUNCTION: Server(HWND, unsigned, WORD, LONG) + + PURPOSE: Processes messages for "Server" dialog box + + MESSAGES: + + WM_INITDIALOG - initialize dialog box + WM_COMMAND - Input received + + COMMENTS: + + No initialization is needed for this particular dialog box, but TRUE + must be returned to Windows. + + Wait for user to click on "Ok" button, then close the dialog box. + +****************************************************************************/ +BOOL APIENTRY Server( +HWND hDlg, /* window handle of the dialog box */ +UINT message, /* type of message */ +UINT wParam, /* message-specific information */ +LONG lParam) +{ +UNREFERENCED_PARAMETER(lParam); + +#ifdef RPC +switch (message) { + + case WM_INITDIALOG: /* message: initialize dialog box */ + SetDlgItemText( hDlg, IDD_SERVERNAME, szNetworkAddress); + return (TRUE); + + case WM_COMMAND: /* message: received a command */ + switch(wParam) { + case IDCANCEL: /* System menu close command? */ + EndDialog( hDlg, FALSE ); + return( TRUE ); + case IDOK: /* "OK" box selected? */ + GetDlgItemText( hDlg, IDD_SERVERNAME, szNetworkAddress, UNCLEN); + if (Bind(hDlg) != RPC_S_OK) { + EndDialog(hDlg, FALSE); + return(FALSE); + } + EndDialog(hDlg, TRUE); + return(TRUE); + } /* end switch wParam */ + + } /* end switch message */ + return (FALSE); /* Didn't process a message */ +#endif +} + +/**************************************************************************** + + FUNCTION: Bind(HWND) + + PURPOSE: Make RPC API calls to bind to the server application + + COMMENTS: + + The binding calls are made from InitInstance() and whenever + the user changes the server name or endpoint. If the bind + operation is successful, the global flag fBound is set to TRUE. + + The global flag fBound is used to determine whether to call + the RPC API function RpcBindingFree. + +****************************************************************************/ + +#ifdef RPC +RPC_STATUS Bind(HWND hWnd) +{ + RPC_STATUS status; + + if (fBound == TRUE) { /* unbind only if bound */ + fBound = FALSE; + status = RpcBindingFree(&hMandel); // remote calls done; unbind + sprintf(pszFail, "RpcBindingFree returned 0x%x", status); + MessageBox(hWnd, pszFail, "RPC Sample Application", MB_ICONINFORMATION); + if (status) + return(status); + } + status = RpcStringBindingCompose(pszUuid, + pszProtocolSequence, + szNetworkAddress, + pszEndpoint, + pszOptions, + &pszStringBinding); + sprintf(pszFail, "RpcStringBindingCompose returned: (0x%x)\nNetwork Address = %s\n", + status, szNetworkAddress); + MessageBox(hWnd, pszFail, "RPC Sample Application", MB_ICONINFORMATION); + + if (status) + return(status); + + status = RpcBindingFromStringBinding(pszStringBinding, + &hMandel); + + sprintf(pszFail, "RpcBindingFromStringBinding returned: (0x%x)\nString = %s\n", + status, pszStringBinding); + MessageBox(hWnd, pszFail, "RPC Sample Application", MB_ICONINFORMATION); + + if (status) + return(status); + + fBound = TRUE; /* bind successful; reset flag */ + return(status); +} +#endif + +/* end mandel.c */