Annotation of mstools/samples/rpc/handles/cxhndl/cxhndlc.c, revision 1.1.1.3

1.1       root        1: /****************************************************************************
1.1.1.3 ! root        2:                   Microsoft RPC Version 1.0
        !             3:                Copyright Microsoft Corp. 1992
        !             4:                       cxhndl Example
        !             5: 
        !             6:     FILE:       cxhndlc.c
        !             7:     
        !             8:     USAGE:      cxhndlc  -n network_address
        !             9:                          -p protocol_sequence
        !            10:                          -e endpoint
        !            11:                          -o options
        !            12:                          -f filename
        !            13: 
        !            14:     PURPOSE:    Client side of RPC distributed application
        !            15:     
        !            16:     FUNCTIONS:  main() - binds to server and calls remote procedure
        !            17:     
        !            18:     COMMENTS:   This distributed application uses a context handle.
1.1       root       19: 
                     20: ****************************************************************************/
1.1.1.3 ! root       21: 
1.1       root       22: #include <stdlib.h>
1.1.1.3 ! root       23: #include <stdio.h>
        !            24: #include "cxhndl.h"    // header file generated by MIDL compiler
1.1       root       25: 
                     26: 
                     27: #define PURPOSE \
                     28: "This Microsoft RPC Version 1.0 sample program demonstrates\n\
                     29: the use of the [context_handle] attribute. For more information\n\
                     30: about attributes and RPC API functions, see the RPC programming\n\
                     31: guide and reference.\n\n"
                     32: 
                     33: 
                     34: void Usage(char * pszProgramName)
                     35: {
                     36:     fprintf(stderr, "%s", PURPOSE);
                     37:     fprintf(stderr, "Usage:  %s\n", pszProgramName);
                     38:     fprintf(stderr, " -p protocol_sequence\n");
                     39:     fprintf(stderr, " -n network_address\n");
                     40:     fprintf(stderr, " -e endpoint\n");
                     41:     fprintf(stderr, " -o options\n");
                     42:     fprintf(stderr, " -f filename\n");
                     43:     exit(1);
                     44: }
                     45: 
1.1.1.2   root       46: void _CRTAPI1 main(int argc, char **argv)
1.1       root       47: {
1.1.1.3 ! root       48:     RPC_STATUS status; 
1.1       root       49:     PCONTEXT_HANDLE_TYPE phContext = NULL;
                     50: 
1.1.1.2   root       51:     unsigned char * pbBuf = NULL;
1.1.1.3 ! root       52:     short cbRead;    // count of bytes read 
1.1       root       53: 
1.1.1.3 ! root       54:     unsigned char * pszUuid             = NULL;
1.1       root       55:     unsigned char * pszProtocolSequence = "ncacn_np";
                     56:     unsigned char * pszNetworkAddress   = NULL;
1.1.1.3 ! root       57:     unsigned char * pszEndpoint         = "\\pipe\\cxhndl";
1.1       root       58:     unsigned char * pszOptions          = NULL;
1.1.1.3 ! root       59:     unsigned char * pszStringBinding    = NULL;
        !            60:     unsigned char * pszFileName         = "readme.txt";
1.1       root       61:     int i;
                     62: 
1.1.1.3 ! root       63:     /* allow the user to override settings with command line switches */
1.1       root       64:     for (i = 1; i < argc; i++) {
1.1.1.3 ! root       65:         if ((*argv[i] == '-') || (*argv[i] == '/')) {
        !            66:             switch (tolower(*(argv[i]+1))) {
        !            67:             case 'p':  // protocol sequence
        !            68:                 pszProtocolSequence = argv[++i];
        !            69:                 break;
        !            70:             case 'n':  // network address
        !            71:                 pszNetworkAddress = argv[++i];
        !            72:                 break;
        !            73:             case 'e':
        !            74:                 pszEndpoint = argv[++i];
        !            75:                 break;
        !            76:             case 'o':
        !            77:                 pszOptions = argv[++i];
        !            78:                 break;
        !            79:             case 'f':
        !            80:                 pszFileName = argv[++i];
        !            81:                 break;
        !            82:             case 'h':
        !            83:             case '?':
        !            84:             default:
        !            85:                 Usage(argv[0]);
        !            86:             }
        !            87:         }
        !            88:         else
        !            89:             Usage(argv[0]);
1.1       root       90:     }
                     91: 
1.1.1.3 ! root       92:     pbBuf = (unsigned char *) 
        !            93:             midl_user_allocate(BUFSIZE * sizeof(unsigned char));
1.1       root       94: 
1.1.1.3 ! root       95:     /* Use a convenience function to concatenate the elements of  */
        !            96:     /* the string binding into the proper sequence.               */
1.1       root       97:     status = RpcStringBindingCompose(pszUuid,
                     98:                                      pszProtocolSequence,
                     99:                                      pszNetworkAddress,
                    100:                                      pszEndpoint,
                    101:                                      pszOptions,
                    102:                                      &pszStringBinding);
                    103:     printf("RpcStringBindingCompose returned 0x%x\n", status);
                    104:     printf("pszStringBinding = %s\n", pszStringBinding);
1.1.1.3 ! root      105:     if (status) {
        !           106:         exit(status);
        !           107:     }
1.1       root      108: 
1.1.1.3 ! root      109:     /* Set the binding handle that will be used to bind to the server. */
1.1       root      110:     status = RpcBindingFromStringBinding(pszStringBinding,
1.1.1.3 ! root      111:                                          &hStarter);
1.1       root      112:     printf("RpcBindingFromStringBinding returned 0x%x\n", status);
1.1.1.3 ! root      113:     if (status) {
        !           114:         exit(status);
        !           115:     }
1.1       root      116: 
                    117:     printf("Calling the remote procedure RemoteOpen\n");
                    118:     if (RemoteOpen(&phContext, pszFileName) < 0) {
1.1.1.3 ! root      119:         printf("Unable to open %s\n", pszFileName);
        !           120:         Shutdown();
        !           121:         exit(2);
1.1       root      122:     }
                    123: 
1.1.1.3 ! root      124:     /* Now the context handle also manages the binding. */
1.1       root      125:     status = RpcBindingFree(&hStarter);
                    126:     printf("RpcBindingFree returned 0x%x\n", status);
1.1.1.3 ! root      127:     if (status) {
        !           128:         exit(status);
        !           129:     }
1.1       root      130: 
                    131:     printf("Calling the remote procedure RemoteRead\n");
1.1.1.3 ! root      132:     while (RemoteRead(phContext, pbBuf, &cbRead) > 0) {
        !           133:         for (i = 0; i < cbRead; i++)
        !           134:             putchar(*(pbBuf+i));
1.1       root      135:     }
                    136: 
                    137:     printf("Calling the remote procedure RemoteClose\n");
                    138:     if (RemoteClose(&phContext) < 0 ) {
1.1.1.3 ! root      139:         printf("Close failed on %s\n", pszFileName);
        !           140:         exit(2);
1.1       root      141:     }
                    142: 
1.1.1.3 ! root      143:     /*  The calls to the remote procedures are complete. */
        !           144:     /*  Free the string and the binding handle           */
        !           145:     status = RpcStringFree(&pszStringBinding); 
1.1       root      146:     printf("RpcStringFree returned 0x%x\n", status);
1.1.1.3 ! root      147:     if (status) {
        !           148:         exit(status);
        !           149:     }
1.1       root      150: 
                    151:     exit(0);
                    152: 
1.1.1.3 ! root      153: }  // end main()
        !           154: 
        !           155: 
        !           156: /*********************************************************************/
        !           157: /*                 MIDL allocate and free                            */
        !           158: /*********************************************************************/
1.1       root      159: 
1.1.1.3 ! root      160: void __RPC_FAR * __RPC_API midl_user_allocate(size_t len)
1.1       root      161: {
                    162:     return(malloc(len));
                    163: }
                    164: 
1.1.1.3 ! root      165: void __RPC_API midl_user_free(void __RPC_FAR * ptr)
1.1       root      166: {
                    167:     free(ptr);
                    168: }
                    169: 
                    170: /* end cxhndlc.c */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.