home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / rpc / cluuid / cluuids.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  6.8 KB  |  205 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                       Cluuid Example
  5.  
  6.     FILE:       cluuids.c
  7.  
  8.     USAGE:      cluuids  -p protocol_sequence
  9.                          -e endpoint
  10.                          -m max calls
  11.                          -n min calls
  12.                          -f flag for RpcServerListen
  13.                          -1 client object uuid
  14.                          -2 manager epv uuid
  15.  
  16.     PURPOSE:    Server side of RPC distributed application hello
  17.  
  18.     FUNCTIONS:  main() - registers server as RPC server
  19.  
  20.     COMMENTS:   Print "hello, world" on the server.
  21.                 When you supply a type UUID, the client must
  22.                 supply the same UUID.
  23.  
  24. ****************************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include "cluuid.h"    // header file generated by MIDL compiler
  31.  
  32. // the second implementation of the remote procedure
  33. extern void HelloProc2(unsigned char * pszString);
  34.  
  35. #define PURPOSE \
  36. "This Microsoft RPC Version 2.0 sample program demonstrates how\n\
  37. to supply multiple implementations of the remote procedure\n\
  38. specified in the interface. It also demonstrates how the client\n\
  39. selects among the implementations by providing a client object uuid.\n\n"
  40.  
  41. #define NULL_UUID_STRING "00000000-0000-0000-0000-000000000000"
  42.  
  43. void Usage(char * pszProgramName)
  44. {
  45.     fprintf(stderr, "%s", PURPOSE);
  46.     fprintf(stderr, "Usage:  %s\n", pszProgramName);
  47.     fprintf(stderr, " -p protocol_sequence\n");
  48.     fprintf(stderr, " -e endpoint\n");
  49.     fprintf(stderr, " -m maxcalls\n");
  50.     fprintf(stderr, " -n mincalls\n");
  51.     fprintf(stderr, " -f flag_wait_op\n");
  52.     fprintf(stderr, " -1 client uuid\n");
  53.     fprintf(stderr, " -2 manager uuid\n");
  54.     exit(1);
  55. }
  56.  
  57. /* main:  register the interface, start listening for clients */
  58. void _CRTAPI1 main(int argc, char * argv[])
  59. {
  60.     RPC_STATUS status;
  61.     UUID MgrTypeUuid, ClientUuid;
  62.     unsigned char * pszProtocolSequence = "ncacn_np";
  63.     unsigned char * pszSecurity         = NULL;
  64.     unsigned char * pszClientUuid       = NULL_UUID_STRING;
  65.     unsigned char * pszMgrTypeUuid      = "11111111-1111-1111-1111-111111111111";
  66.     unsigned char * pszEndpoint         = "\\pipe\\cluuid";
  67.     unsigned int    cMinCalls           = 1;
  68.     unsigned int    cMaxCalls           = 20;
  69.     unsigned int    fDontWait           = FALSE;
  70.     int i;
  71.  
  72.     cluuid_SERVER_EPV epv2;    // the mgr_epv for the 2nd implementation
  73.  
  74.     /* allow the user to override settings with command line switches */
  75.     for (i = 1; i < argc; i++) {
  76.         if ((*argv[i] == '-') || (*argv[i] == '/')) {
  77.             switch (tolower(*(argv[i]+1))) {
  78.             case 'p':  // protocol sequence
  79.                 pszProtocolSequence = argv[++i];
  80.                 break;
  81.             case 'e':
  82.                 pszEndpoint = argv[++i];
  83.                 break;
  84.             case 'm':
  85.                 cMaxCalls = (unsigned int) atoi(argv[++i]);
  86.                 break;
  87.             case 'n':
  88.                 cMinCalls = (unsigned int) atoi(argv[++i]);
  89.                 break;
  90.             case 'f':
  91.                 fDontWait = (unsigned int) atoi(argv[++i]);
  92.                 break;
  93.             case '1':
  94.                 pszMgrTypeUuid = argv[++i];
  95.                 break;
  96.             case '2':
  97.                 pszClientUuid = argv[++i];
  98.                 break;
  99.             case 'h':
  100.             case '?':
  101.             default:
  102.                 Usage(argv[0]);
  103.             }
  104.         }
  105.         else
  106.             Usage(argv[0]);
  107.     }
  108.  
  109.     status = RpcServerUseProtseqEp(pszProtocolSequence,
  110.                                    cMaxCalls,
  111.                                    pszEndpoint,
  112.                                    pszSecurity);  // Security descriptor
  113.     printf("RpcServerUseProtseqEp returned 0x%x\n", status);
  114.     if (status) {
  115.         exit(status);
  116.     }
  117.  
  118.     status = UuidFromString(pszClientUuid, &ClientUuid);
  119.     printf("UuidFromString returned 0x%x = %d\n", status, status);
  120.     if (status) {
  121.         exit(status);
  122.     }
  123.  
  124.     status = UuidFromString(pszMgrTypeUuid, &MgrTypeUuid);
  125.     printf("UuidFromString returned 0x%x = %d\n", status, status);
  126.     if (status) {
  127.         exit(status);
  128.     }
  129.     if (strcmp (pszMgrTypeUuid, NULL_UUID_STRING) == 0) {
  130.         printf("Register object using non-null uuid %s\n", pszMgrTypeUuid);
  131.         exit(1);
  132.     }
  133.  
  134.     if (strcmp (pszClientUuid, NULL_UUID_STRING) == 0) {
  135.         printf("Register object using non-null uuid %s\n", pszMgrTypeUuid);
  136.         ClientUuid = MgrTypeUuid;
  137.     }
  138.  
  139.     RpcObjectSetType(&ClientUuid, &MgrTypeUuid);  // associate type UUID with nil UUID
  140.     printf("RpcObjectSetType returned 0x%x\n", status);
  141.     if (status) {
  142.         exit(status);
  143.     }
  144.  
  145.     status = RpcServerRegisterIf(cluuid_ServerIfHandle, // interface to register
  146.                                  NULL,   // MgrTypeUuid
  147.                                  NULL);  // MgrEpv; null means use default
  148.     printf("RpcServerRegisterIf returned 0x%x\n", status);
  149.     if (status) {
  150.         exit(status);
  151.     }
  152.  
  153.     /* register the second manager epv and associate it with the
  154.        specified uuid.  the second uuid must be non-null so that
  155.        it will not conflict with the NULL uuid already registered
  156.        for this interface
  157.     */
  158.     epv2.HelloProc = HelloProc2;
  159.     epv2.Shutdown = Shutdown;
  160.     status = RpcServerRegisterIf(cluuid_ServerIfHandle, // interface to register
  161.                                  &MgrTypeUuid,  // MgrTypeUuid
  162.                                  &epv2);        // 2nd manager epv
  163.     printf("RpcServerRegisterIf returned 0x%x\n", status);
  164.     if (status) {
  165.         exit(status);
  166.     }
  167.  
  168.     printf("Calling RpcServerListen\n");
  169.     status = RpcServerListen(cMinCalls,
  170.                              cMaxCalls,
  171.                              fDontWait);
  172.     printf("RpcServerListen returned: 0x%x\n", status);
  173.     if (status) {
  174.         exit(status);
  175.     }
  176.  
  177.     if (fDontWait) {
  178.         printf("Calling RpcMgmtWaitServerListen\n");
  179.         status = RpcMgmtWaitServerListen();  //  wait operation
  180.         printf("RpcMgmtWaitServerListen returned: 0x%x\n", status);
  181.         if (status) {
  182.             exit(status);
  183.         }
  184.     }
  185.  
  186. } // end main()
  187.  
  188.  
  189. /*********************************************************************/
  190. /*                 MIDL allocate and free                            */
  191. /*********************************************************************/
  192.  
  193. void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  194. {
  195.     return(malloc(len));
  196. }
  197.  
  198. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  199. {
  200.     free(ptr);
  201. }
  202.  
  203.  
  204. /* end file cluuids.c */
  205.