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 / handles / cxhndl / cxhndlp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  3.7 KB  |  130 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                         cxhndl Example
  5.  
  6.     FILE:       cxhndlp.c
  7.  
  8.     PURPOSE:    Remote procedures used in server application cxhndls
  9.  
  10.     FUNCTIONS:  RemoteOpen() - Open the file
  11.                 RemoteRead() - Read a buffer's worth of the file
  12.                 RemoteClose() - Close the file and shutdown server
  13.                 Shutdown() - Shutdown the server
  14.  
  15.     COMMENTS:   This distributed application uses a context handle.
  16.  
  17. ****************************************************************************/
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "cxhndl.h"    // header file generated by MIDL compiler
  23.  
  24.  
  25. typedef struct {
  26.     FILE *hFile;
  27.     char achFile[256];
  28. } FILE_CONTEXT_TYPE;
  29.  
  30.  
  31. /* This remote procedure opens a file on the server. */
  32. short RemoteOpen(PCONTEXT_HANDLE_TYPE  *pphContext,
  33.                  unsigned char         *pszFileName
  34.     )
  35. {
  36.     FILE *hFile;
  37.     FILE_CONTEXT_TYPE *pFileContext;
  38.  
  39.     if ((hFile = fopen(pszFileName, "r")) == NULL) {
  40.         *pphContext = (PCONTEXT_HANDLE_TYPE) NULL;
  41.         return(-1);
  42.     }
  43.     else {
  44.         pFileContext = (FILE_CONTEXT_TYPE *)
  45.                        midl_user_allocate(sizeof(FILE_CONTEXT_TYPE));
  46.         pFileContext->hFile = hFile;
  47.         strcpy(pFileContext->achFile, pszFileName);
  48.         *pphContext = (PCONTEXT_HANDLE_TYPE) pFileContext;
  49.         return(0);
  50.     }
  51. }
  52.  
  53. /* This remote procedure reads a file on the server. */
  54. short RemoteRead(PCONTEXT_HANDLE_TYPE  phContext,
  55.                  unsigned char         *pbBuf,
  56.                  short                 *pcbBuf)
  57. {
  58.     FILE_CONTEXT_TYPE *pFileContext;
  59.  
  60.     printf("in RemoteRead\n");
  61.  
  62.     pFileContext = (FILE_CONTEXT_TYPE *) phContext;
  63.     *pcbBuf = (short) fread(pbBuf,
  64.                             sizeof(char),
  65.                             BUFSIZE,
  66.                             pFileContext->hFile);
  67.     return(*pcbBuf);
  68. }
  69.  
  70. /* This remote procedure closes a file on the server. */
  71. short RemoteClose(PCONTEXT_HANDLE_TYPE *pphContext)
  72. {
  73.     FILE_CONTEXT_TYPE *pFileContext;
  74.  
  75.     printf("in RemoteClose\n");
  76.  
  77.     pFileContext = (FILE_CONTEXT_TYPE *) *pphContext;
  78.  
  79.     if ( fclose( pFileContext->hFile ) == 0)
  80.         {
  81.         midl_user_free( *pphContext );
  82.         *pphContext = NULL;
  83.         return(0);
  84.         }
  85.     else
  86.         /* Context Rundown routine will attempt to close it again */
  87.         return(-1);
  88. }
  89.  
  90. /* The Shutdown procedure tells the server to stop listening */
  91. /* for client requests.                                      */
  92. void Shutdown(void)
  93. {
  94.     RPC_STATUS status;
  95.  
  96.     printf("Calling RpcMgmtStopServerListening\n");
  97.     status = RpcMgmtStopServerListening(NULL);
  98.     printf("RpcMgmtStopServerListening returned: 0x%x\n", status);
  99.     if (status) {
  100.         exit(status);
  101.     }
  102.  
  103.     printf("Calling RpcServerUnregisterIf\n");
  104.     status = RpcServerUnregisterIf(NULL, NULL, FALSE);
  105.     printf("RpcServerUnregisterIf returned 0x%x\n", status);
  106.     if (status) {
  107.         exit(status);
  108.     }
  109. }
  110.  
  111. /* The rundown routine is associated with the context handle type. */
  112. void __RPC_USER PCONTEXT_HANDLE_TYPE_rundown(PCONTEXT_HANDLE_TYPE phContext)
  113. {
  114.     FILE_CONTEXT_TYPE *pFileContext;
  115.  
  116.     printf("Context rundown routine\n");
  117.  
  118.     if ( phContext )
  119.     {
  120.         pFileContext = (FILE_CONTEXT_TYPE *) phContext;
  121.         if (pFileContext->hFile != NULL)
  122.             fclose(pFileContext->hFile);
  123.  
  124.         midl_user_free( phContext );
  125.     }
  126.  
  127. }
  128.  
  129. /* end file cxhndlp.c */
  130.