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

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