Annotation of mstools/samples/rpc/data/dunion/dunionc.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:                     Discriminated Union Example
        !             5: 
        !             6:     FILE:       dunionc.c
        !             7: 
        !             8:     USAGE:      dunionc  -n network_address
        !             9:                          -p protocol_sequence
        !            10:                          -e endpoint
        !            11:                          -o options
        !            12:                          -d discriminant
        !            13:                          -v union_value
        !            14: 
        !            15:     PURPOSE:    Client side of RPC distributed application
        !            16: 
        !            17:     FUNCTIONS:  main() - binds to server and calls remote procedure
        !            18: 
        !            19:     COMMENTS:   This distributed application illustrates discriminated
        !            20:                 union.
1.1       root       21: 
                     22: ****************************************************************************/
1.1.1.3 ! root       23: 
1.1       root       24: #include <stdlib.h>
1.1.1.3 ! root       25: #include <stdio.h>
        !            26: #include <ctype.h>
        !            27: #include "dunion.h"    // header file generated by MIDL compiler
1.1       root       28: 
                     29: 
                     30: void Usage(char * pszProgramName)
                     31: {
                     32:     fprintf(stderr, "Usage:  %s\n", pszProgramName);
                     33:     fprintf(stderr, " -p protocol_sequence\n");
                     34:     fprintf(stderr, " -n network_address\n");
                     35:     fprintf(stderr, " -e endpoint\n");
                     36:     fprintf(stderr, " -o options\n");
                     37:     fprintf(stderr, " -d discriminant\n");
                     38:     fprintf(stderr, " -v union_value\n");
                     39:     exit(1);
                     40: }
                     41: 
1.1.1.3 ! root       42: void DisplayUnionValue(DISCRIM_UNION_PARAM_TYPE *up,
        !            43:                        short                    sDiscrim) 
1.1       root       44: {
                     45:     printf("sDiscrim = %d, data = ", sDiscrim);
                     46:     switch(sDiscrim) {
1.1.1.3 ! root       47:     case 0:
        !            48:         printf("short: %d\n", up->sVal);
        !            49:         break;
        !            50:     case 1:
        !            51:         printf("float: %f\n", up->fVal);
        !            52:         break;
        !            53:     case 2:
        !            54:         printf("char: %c\n", up->chVal);
        !            55:         break;
        !            56:     default:
        !            57:        printf("invalid\n");
        !            58:         break;
        !            59:     }
1.1       root       60: }
                     61: 
1.1.1.2   root       62: void _CRTAPI1 main(int argc, char **argv)
1.1       root       63: {
1.1.1.3 ! root       64:     RPC_STATUS status;  
        !            65:     unsigned char * pszUuid             = NULL;
1.1       root       66:     unsigned char * pszProtocolSequence = "ncacn_np";
                     67:     unsigned char * pszNetworkAddress   = NULL;
1.1.1.3 ! root       68:     unsigned char * pszEndpoint         = "\\pipe\\dunion";
1.1       root       69:     unsigned char * pszOptions          = NULL;
1.1.1.3 ! root       70:     unsigned char * pszStringBinding    = NULL;
1.1       root       71:     int i;
                     72: 
1.1.1.3 ! root       73:     short sDiscrim = 0;
        !            74:     DISCRIM_UNION_PARAM_TYPE  up = {1};
        !            75:     DISCRIM_UNION_STRUCT_TYPE us = {0, 1};
        !            76: 
        !            77:     /* allow the user to override settings with command line switches */
1.1       root       78:     for (i = 1; i < argc; i++) {
1.1.1.3 ! root       79:         if ((*argv[i] == '-') || (*argv[i] == '/')) {
        !            80:             switch (tolower(*(argv[i]+1))) {
        !            81:             case 'p':  // protocol sequence
        !            82:                 pszProtocolSequence = argv[++i];
        !            83:                 break;
        !            84:             case 'n':  // network address
        !            85:                 pszNetworkAddress = argv[++i];
        !            86:                 break;
        !            87:             case 'e':
        !            88:                 pszEndpoint = argv[++i];
        !            89:                 break;
        !            90:             case 'o':
        !            91:                 pszOptions = argv[++i];
        !            92:                 break;
        !            93:             case 'd':
        !            94:                 sDiscrim = (short) atoi(argv[++i]);
        !            95:                 if ((sDiscrim > 3) || (sDiscrim < 0))
        !            96:                     sDiscrim = 0;
        !            97:                 us.sDiscrim = sDiscrim;
        !            98:                 break;
        !            99:             case 'v':
        !           100:                 switch(sDiscrim) {
        !           101:                 case 0:
        !           102:                     up.sVal =  (short) atoi(argv[++i]);
        !           103:                     us.u.sVal = up.sVal;
        !           104:                     break;
        !           105:                 case 1:
        !           106:                     up.fVal =  (float) atof(argv[++i]);
        !           107:                     us.u.fVal = up.fVal;
        !           108:                     break;
        !           109:                 case 2:
        !           110:                     up.chVal = *(argv[++i]);
        !           111:                     us.u.chVal = up.chVal;
        !           112:                     break;
        !           113:                 default:
        !           114:                     break;
        !           115:                 }
        !           116:                 break;
        !           117:             case 'h':
        !           118:             case '?':
        !           119:             default:
        !           120:                 Usage(argv[0]);
        !           121:             }
        !           122:         }
        !           123:         else
        !           124:             Usage(argv[0]);
1.1       root      125:     }
                    126: 
1.1.1.3 ! root      127:     /* Use a convenience function to concatenate the elements of  */
        !           128:     /* the string binding into the proper sequence.               */
1.1       root      129:     status = RpcStringBindingCompose(pszUuid,
                    130:                                      pszProtocolSequence,
                    131:                                      pszNetworkAddress,
                    132:                                      pszEndpoint,
                    133:                                      pszOptions,
                    134:                                      &pszStringBinding);
                    135:     printf("RpcStringBindingCompose returned 0x%x\n", status);
                    136:     printf("pszStringBinding = %s\n", pszStringBinding);
1.1.1.3 ! root      137:     if (status) {
        !           138:         exit(status);
        !           139:     }
1.1       root      140: 
1.1.1.3 ! root      141:     /* Set the binding handle that will be used to bind to the server. */
1.1       root      142:     status = RpcBindingFromStringBinding(pszStringBinding,
1.1.1.3 ! root      143:                                          &hDiscrim);
1.1       root      144:     printf("RpcBindingFromStringBinding returned 0x%x\n", status);
1.1.1.3 ! root      145:     if (status) {
        !           146:         exit(status);
        !           147:     }
1.1       root      148: 
                    149:     printf("Calling the remote procedure 'UnionParamProc'\n");
                    150: 
1.1.1.3 ! root      151:     DisplayUnionValue(&up, sDiscrim);  // display value before call
        !           152:     UnionParamProc(&up, sDiscrim);     // call the remote procedure
1.1       root      153: 
1.1.1.3 ! root      154:     UnionStructProc(&us);              // call the remote procedure
        !           155:     DisplayUnionValue(&up, sDiscrim);  // display value after call
1.1       root      156: 
1.1.1.3 ! root      157:     Shutdown();                    // Shut down the server
1.1       root      158: 
1.1.1.3 ! root      159:     /*  The remote procedure call is complete.  Free the binding handle */
        !           160:     status = RpcBindingFree(&hDiscrim); 
1.1       root      161:     printf("RpcBindingFree returned 0x%x\n", status);
1.1.1.3 ! root      162:     if (status) {
        !           163:         exit(status);
        !           164:     }
1.1       root      165: 
                    166:     exit(0);
1.1.1.3 ! root      167: 
        !           168: }  // end main()
        !           169: 
        !           170: 
        !           171: /*********************************************************************/
        !           172: /*                 MIDL allocate and free                            */
        !           173: /*********************************************************************/
        !           174: 
        !           175: void __RPC_FAR * __RPC_API midl_user_allocate(size_t len)
        !           176: {
        !           177:     return(malloc(len));
        !           178: }
        !           179: 
        !           180: void __RPC_API midl_user_free(void __RPC_FAR * ptr)
        !           181: {
        !           182:     free(ptr);
1.1       root      183: }
                    184: 
1.1.1.3 ! root      185: /* end file dunionc.c */

unix.superglobalmegacorp.com

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