Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.5 KiB

  1. /****************************************************************************
  2. Microsoft RPC Version 1`1
  3. Copyright Microsoft Corp. 1992
  4. Hello Example
  5. FILE: helloc.c
  6. USAGE: client -n network_address
  7. -p protocol_sequence
  8. -e endpoint
  9. -o options
  10. -u uuid
  11. PURPOSE: Client side of RPC distributed application
  12. FUNCTIONS: main() - binds to server and calls remote procedure
  13. COMMENTS:
  14. This distributed application prints a string such as "hello, world"
  15. on the server. The client manages its connection to the server.
  16. The client uses the binding handle hello_IfHandle defined in the
  17. file hello.h.
  18. ****************************************************************************/
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <rpc.h> // RPC API functions, types
  23. #include "hello.h" // header file generated by MIDL compiler
  24. void Usage(char * pszProgramName)
  25. {
  26. fprintf(stderr, "Usage: %s\n", pszProgramName);
  27. fprintf(stderr, " -p protocol_sequence\n");
  28. fprintf(stderr, " -n network_address\n");
  29. fprintf(stderr, " -e endpoint\n");
  30. fprintf(stderr, " -o options\n");
  31. fprintf(stderr, " -u uuid\n");
  32. fprintf(stderr, " -s string\n");
  33. exit(1);
  34. }
  35. int __cdecl
  36. main (argc, argv)
  37. int argc;
  38. char *argv[];
  39. {
  40. RPC_STATUS status; // returned by RPC API function
  41. unsigned char * pszUuid = "12345678-1234-1234-1234-123456789ABC";
  42. unsigned char * pszProtocolSequence = "ncacn_np";
  43. unsigned char * pszNetworkAddress = NULL;
  44. unsigned char * pszEndpoint = "\\pipe\\hello";
  45. unsigned char * pszOptions = NULL;
  46. unsigned char * pszStringBinding = NULL;
  47. unsigned char * pszString = "hello, world";
  48. int i;
  49. // allow the user to override settings with command line switches
  50. for (i = 1; i < argc; i++) {
  51. if ((*argv[i] == '-') || (*argv[i] == '/')) {
  52. switch (tolower(*(argv[i]+1))) {
  53. case 'p': // protocol sequence
  54. pszProtocolSequence = argv[++i];
  55. break;
  56. case 'n': // network address
  57. pszNetworkAddress = argv[++i];
  58. break;
  59. case 'e':
  60. pszEndpoint = argv[++i];
  61. break;
  62. case 'o':
  63. pszOptions = argv[++i];
  64. break;
  65. case 'u':
  66. pszUuid = argv[++i];
  67. break;
  68. case 's':
  69. pszString = argv[++i];
  70. break;
  71. case 'h':
  72. case '?':
  73. default:
  74. Usage(argv[0]);
  75. }
  76. } else {
  77. Usage(argv[0]);
  78. }
  79. }
  80. // Use a convenience function to concatenate the elements of
  81. // the string binding into the proper sequence.
  82. status = RpcStringBindingCompose(pszUuid,
  83. pszProtocolSequence,
  84. pszNetworkAddress,
  85. pszEndpoint,
  86. pszOptions,
  87. &pszStringBinding);
  88. if (status) {
  89. printf("RpcStringBindingCompose returned 0x%x\n", status);
  90. exit(2);
  91. }
  92. printf("pszStringBinding = %s\n", pszStringBinding);
  93. //
  94. // Set the binding handle that will be used to bind to the server.
  95. //
  96. status = RpcBindingFromStringBinding(pszStringBinding,
  97. &hello_IfHandle);
  98. if (status) {
  99. printf("RpcBindingFromStringBinding returned 0x%x\n", status);
  100. exit(2);
  101. }
  102. //
  103. // Tell RPC to do the security thing.
  104. //
  105. status = RpcBindingSetAuthInfo(
  106. hello_IfHandle,
  107. "ntdev\\chandans",
  108. RPC_C_AUTHN_LEVEL_CONNECT,
  109. 18,
  110. NULL,
  111. RPC_C_AUTHZ_NAME );
  112. if ( status ) {
  113. printf("RpcBindingSetAuthInfo returned %ld\n", status);
  114. exit(2);
  115. }
  116. //
  117. // Do the actual RPC calls to the server.
  118. //
  119. printf(" print the string '%s' on the server\n", pszString);
  120. RpcTryExcept {
  121. int i;
  122. for ( i=0; i<100 ; i++ ) {
  123. HelloProc(pszString); // make call with user message
  124. }
  125. Shutdown(); // shut down the server side
  126. } RpcExcept(1) {
  127. printf("Runtime library reported an exception 0x%lx\n",
  128. RpcExceptionCode());
  129. } RpcEndExcept
  130. // The calls to the remote procedures are complete.
  131. // Free the string and the binding handle
  132. status = RpcStringFree(&pszStringBinding); // remote calls done; unbind
  133. if (status) {
  134. printf("RpcStringFree returned 0x%x\n", status);
  135. exit(2);
  136. }
  137. status = RpcBindingFree(&hello_IfHandle); // remote calls done; unbind
  138. if (status) {
  139. printf("RpcBindingFree returned 0x%x\n", status);
  140. exit(2);
  141. }
  142. return 0;
  143. }
  144. // ====================================================================
  145. // MIDL allocate and free
  146. // ====================================================================
  147. void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len)
  148. {
  149. return(malloc(len));
  150. }
  151. void __RPC_API MIDL_user_free(void __RPC_FAR * ptr)
  152. {
  153. free(ptr);
  154. }
  155. /* end file helloc.c */