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