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

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