--- mstools/samples/rpc/handles/cxhndl/cxhndlp.c 2018/08/09 18:20:56 1.1 +++ mstools/samples/rpc/handles/cxhndl/cxhndlp.c 2018/08/09 18:24:17 1.1.1.3 @@ -1,68 +1,93 @@ /**************************************************************************** - Microsoft RPC Version 1.0 - Copyright Microsoft Corp. 1992 - cxhndl Example - - FILE: cxhndlp.c - PURPOSE: Remote procedures used in server application cxhndls - FUNCTIONS: RemoteOpen() - Open the file - RemoteRead() - Read a buffer's worth of the file - RemoteClose() - Close the file and shutdown server - Shutdown() - Shutdown the server - COMMENTS: + Microsoft RPC Version 1.0 + Copyright Microsoft Corp. 1992 + cxhndl Example + + FILE: cxhndlp.c + + PURPOSE: Remote procedures used in server application cxhndls + + FUNCTIONS: RemoteOpen() - Open the file + RemoteRead() - Read a buffer's worth of the file + RemoteClose() - Close the file and shutdown server + Shutdown() - Shutdown the server + + COMMENTS: This distributed application uses a context handle. ****************************************************************************/ -#include #include +#include #include -#include -#include "cxhndl.h" +#include "cxhndl.h" // header file generated by MIDL compiler -#define BUFSIZE 1024 typedef struct { - FILE * hFile; + FILE *hFile; char achFile[256]; } FILE_CONTEXT_TYPE; -FILE_CONTEXT_TYPE FileContext; -FILE_CONTEXT_TYPE * pFileContext = &FileContext; -// -// This remote procedure opens a file on the server. -// -short RemoteOpen(PPCONTEXT_HANDLE_TYPE pphContext, char * pszFileName) +/* This remote procedure opens a file on the server. */ +short RemoteOpen(PCONTEXT_HANDLE_TYPE *pphContext, + unsigned char *pszFileName + ) { - *pphContext = pFileContext; // initialize the context handle - strcpy(pFileContext->achFile, pszFileName); - if ((pFileContext->hFile = fopen(pszFileName, "r")) == NULL) - return(-1); - else - return(0); + FILE *hFile; + FILE_CONTEXT_TYPE *pFileContext; + + if ((hFile = fopen(pszFileName, "r")) == NULL) { + *pphContext = (PCONTEXT_HANDLE_TYPE) NULL; + return(-1); + } + else { + pFileContext = (FILE_CONTEXT_TYPE *) + midl_user_allocate(sizeof(FILE_CONTEXT_TYPE)); + pFileContext->hFile = hFile; + strcpy(pFileContext->achFile, pszFileName); + *pphContext = (PCONTEXT_HANDLE_TYPE) pFileContext; + return(0); + } } -// -// This remote procedure reads a file on the server. -// -short RemoteRead(PPCONTEXT_HANDLE_TYPE pphContext, char *pbBuf, short *pcbBuf) +/* This remote procedure reads a file on the server. */ +short RemoteRead(PCONTEXT_HANDLE_TYPE phContext, + unsigned char *pbBuf, + short *pcbBuf) { - FILE * fp; + FILE_CONTEXT_TYPE *pFileContext; printf("in RemoteRead\n"); - pFileContext = (FILE_CONTEXT_TYPE *) *pphContext; - fp = (FILE *) pFileContext->hFile; + pFileContext = (FILE_CONTEXT_TYPE *) phContext; *pcbBuf = (short) fread(pbBuf, - sizeof(char), - BUFSIZE, - fp); + sizeof(char), + BUFSIZE, + pFileContext->hFile); return(*pcbBuf); } -// The Shutdown procedure tells the server to stop listening -// for client requests -// +/* This remote procedure closes a file on the server. */ +short RemoteClose(PCONTEXT_HANDLE_TYPE *pphContext) +{ + FILE_CONTEXT_TYPE *pFileContext; + + printf("in RemoteClose\n"); + + pFileContext = (FILE_CONTEXT_TYPE *) *pphContext; + + if ( fclose( pFileContext->hFile ) == 0) + { + *pphContext = NULL; + return(0); + } + else + /* Context Rundown routine will attempt to close it again */ + return(-1); +} + +/* The Shutdown procedure tells the server to stop listening */ +/* for client requests. */ void Shutdown(void) { RPC_STATUS status; @@ -71,45 +96,28 @@ void Shutdown(void) status = RpcMgmtStopServerListening(NULL); printf("RpcMgmtStopServerListening returned: 0x%x\n", status); if (status) { - exit(2); + exit(status); } printf("Calling RpcServerUnregisterIf\n"); status = RpcServerUnregisterIf(NULL, NULL, FALSE); printf("RpcServerUnregisterIf returned 0x%x\n", status); if (status) { - exit(2); + exit(status); } } -// -// This remote procedure closes a file on the server and -// shuts down the server. -// -short RemoteClose(PPCONTEXT_HANDLE_TYPE pphContext) +/* The rundown routine is associated with the context handle type. */ +void __RPC_USER PCONTEXT_HANDLE_TYPE_rundown(PCONTEXT_HANDLE_TYPE phContext) { - printf("in RemoteClose\n"); - - pFileContext = (FILE_CONTEXT_TYPE *) *pphContext; - if (fclose(pFileContext->hFile) == 0) - return(0); - else - return(-1); -} + FILE_CONTEXT_TYPE *pFileContext; -// -// The rundown routine is associated with the context handle -// type. + printf("Context rundown routine\n"); -void PCONTEXT_HANDLE_TYPE_rundown(PCONTEXT_HANDLE_TYPE phContext) -{ -FILE_CONTEXT_TYPE *pFileContext; + pFileContext = (FILE_CONTEXT_TYPE *) phContext; + if (pFileContext->hFile != NULL) + fclose(pFileContext->hFile); +} - printf("Context rundown routine\n"); - Shutdown(); +/* end file cxhndlp.c */ - pFileContext = (FILE_CONTEXT_TYPE *) phContext; - if (pFileContext->hFile != NULL) - fclose(pFileContext->hFile); - phContext = NULL; -}