[KERNEL32] Sync format_msg.c with Wine Staging 1.7.55. CORE-10536
[reactos.git] / rostests / win32 / rpcrt4 / context_handles / client.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <ctype.h>
4 #include "ctx.h"
5
6 #define TYPE_FORMAT_STRING_SIZE 23
7 #define PROC_FORMAT_STRING_SIZE 21
8 #define TRANSMIT_AS_TABLE_SIZE 0
9 #define WIRE_MARSHAL_TABLE_SIZE 0
10
11 typedef struct _MIDL_TYPE_FORMAT_STRING
12 {
13 short Pad;
14 unsigned char Format[ TYPE_FORMAT_STRING_SIZE ];
15 } MIDL_TYPE_FORMAT_STRING;
16
17 typedef struct _MIDL_PROC_FORMAT_STRING
18 {
19 short Pad;
20 unsigned char Format[ PROC_FORMAT_STRING_SIZE ];
21 } MIDL_PROC_FORMAT_STRING;
22
23 extern const MIDL_STUB_DESC hello_StubDesc;
24 extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;
25 //extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;*/
26
27 /*****************************************************************
28 * Modified from midl-generated stubs *
29 *****************************************************************/
30
31
32 void m_CtxOpen(
33 /* [out] */ PCTXTYPE __RPC_FAR *pphContext,
34 /* [in] */ long Value)
35 {
36 RPC_BINDING_HANDLE _Handle = 0;
37
38 RPC_MESSAGE _RpcMessage;
39
40 MIDL_STUB_MESSAGE _StubMsg;
41
42 char *ctx, *buf;
43 int i;
44
45 printf("\n*******************************************************************\n");
46 printf("**** CtxOpen() ***\n");
47 printf("*******************************************************************\n\n");
48
49 if(!pphContext)
50 {
51 RpcRaiseException(RPC_X_NULL_REF_POINTER);
52 }
53 RpcTryFinally
54 {
55 NdrClientInitializeNew(
56 ( PRPC_MESSAGE )&_RpcMessage,
57 ( PMIDL_STUB_MESSAGE )&_StubMsg,
58 ( PMIDL_STUB_DESC )&hello_StubDesc,
59 0);
60
61
62 _Handle = hBinding;
63
64
65 _StubMsg.BufferLength = 4U;
66 NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg, _StubMsg.BufferLength, _Handle );
67
68 *(( long __RPC_FAR * )_StubMsg.Buffer)++ = Value;
69
70 NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );
71
72 if ( (_RpcMessage.DataRepresentation & 0X0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION )
73 NdrConvert( (PMIDL_STUB_MESSAGE) &_StubMsg, (PFORMAT_STRING) &__MIDL_ProcFormatString.Format[0] );
74
75
76 printf("Before NdrClientContextUnmarshall: Buflen=%d\nBuffer: ", _StubMsg.RpcMsg->BufferLength);
77 for(buf = _StubMsg.Buffer, i = 0; i < _StubMsg.RpcMsg->BufferLength; i++)
78 printf("0x%x, ", buf[i] & 0x0FF);
79 printf("\n\n");
80
81 *pphContext = (void *)0;
82 NdrClientContextUnmarshall(
83 ( PMIDL_STUB_MESSAGE )&_StubMsg,
84 ( NDR_CCONTEXT __RPC_FAR * )pphContext,
85 _Handle);
86
87 ctx = (char*)*pphContext;
88 printf("\nNdrClientContextUnmarshall returned: handle=0x%p\n", ctx);
89 printf("00: 0x%x <- obviously pointer to binding handle copyed from _Handle\n", *((int*)ctx));
90 ctx+=4;
91 printf("04: 0x%x <- unknown field\n", *((int*)ctx));
92 printf("08: ");
93
94 for(ctx+=4, i = 0; i < 20; i++)
95 printf("0x%x,", *(ctx+i) & 0x0FF); printf(" <- ndr 20 bytes\n\n");
96
97 printf("Buflen=%d, Buffer: ", _StubMsg.BufferLength);
98 for(buf = _StubMsg.BufferStart; buf < _StubMsg.BufferEnd; buf++)
99 printf("0x%x,", *buf & 0x0FF);
100 printf("\n");
101
102
103 }
104 RpcFinally
105 {
106 NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );
107
108 }
109 RpcEndFinally
110
111 }
112
113 void m_CtxOpen2(
114 /* [out] */ PCTXTYPE __RPC_FAR *pphContext,
115 /* [in] */ long Value)
116 {
117
118 RPC_BINDING_HANDLE _Handle = 0;
119
120 RPC_MESSAGE _RpcMessage;
121
122 MIDL_STUB_MESSAGE _StubMsg;
123
124 char buf[255];
125
126 NdrClientInitializeNew(
127 ( PRPC_MESSAGE )&_RpcMessage,
128 ( PMIDL_STUB_MESSAGE )&_StubMsg,
129 ( PMIDL_STUB_DESC )&hello_StubDesc,
130 0);
131
132
133 _Handle = hBinding;
134
135
136 _StubMsg.BufferLength = 4U;
137 NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg, _StubMsg.BufferLength, _Handle );
138
139 *(( long __RPC_FAR * )_StubMsg.Buffer)++ = Value;
140
141 NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );
142
143 if ( (_RpcMessage.DataRepresentation & 0X0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION )
144 NdrConvert( (PMIDL_STUB_MESSAGE) &_StubMsg, (PFORMAT_STRING) &__MIDL_ProcFormatString.Format[0] );
145
146 *pphContext = (void *)0;
147
148 RpcTryExcept
149 {
150 NdrClientContextUnmarshall(
151 ( PMIDL_STUB_MESSAGE )&_StubMsg,
152 NULL,
153 _Handle);
154 }
155 RpcExcept(1)
156 {
157 printf("NdrClientContextUnmarshall reported exception = %d\n", RpcExceptionCode());
158 }
159 RpcEndExcept
160
161
162
163 RpcTryExcept
164 {
165 NdrClientContextUnmarshall(
166 ( PMIDL_STUB_MESSAGE )&_StubMsg,
167 (NDR_CCONTEXT __RPC_FAR * )pphContext,
168 NULL);
169 }
170 RpcExcept(1)
171 {
172 printf("NdrClientContextUnmarshall reported exception = %d\n", RpcExceptionCode());
173 }
174 RpcEndExcept
175
176
177 RpcTryExcept
178 {
179 NdrClientContextMarshall(
180 ( PMIDL_STUB_MESSAGE )&_StubMsg,
181 NULL,
182 1);
183 }
184 RpcExcept(1)
185 {
186 printf("NdrClientContextMarshall reported exception = %d\n", RpcExceptionCode());
187 }
188 RpcEndExcept
189
190 RpcTryExcept
191 {
192 NDRCContextUnmarshall( NULL, _Handle, buf, _RpcMessage.DataRepresentation );
193 }
194 RpcExcept(1)
195 {
196 printf("NDRCContextUnmarshall reported exception = %d\n", RpcExceptionCode());
197 }
198 RpcEndExcept
199
200
201 NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );
202
203
204 }
205
206 void m_CtxHello(
207 /* [in] */ PCTXTYPE phContext)
208 {
209
210 RPC_BINDING_HANDLE _Handle = 0;
211
212 RPC_MESSAGE _RpcMessage;
213
214 MIDL_STUB_MESSAGE _StubMsg;
215
216 char *buf;
217 int i;
218
219 printf("\n*******************************************************************\n");
220 printf("**** CtxHello() ***\n");
221 printf("*******************************************************************\n\n");
222
223 RpcTryFinally
224 {
225 NdrClientInitializeNew(
226 ( PRPC_MESSAGE )&_RpcMessage,
227 ( PMIDL_STUB_MESSAGE )&_StubMsg,
228 ( PMIDL_STUB_DESC )&hello_StubDesc,
229 1);
230
231
232 if(phContext != 0)
233 {
234 _Handle = NDRCContextBinding(( NDR_CCONTEXT )phContext);;
235
236 }
237 else
238 {
239 RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
240 }
241
242 _StubMsg.BufferLength = 20U;
243 NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg, _StubMsg.BufferLength, _Handle );
244
245 NdrClientContextMarshall(
246 ( PMIDL_STUB_MESSAGE )&_StubMsg,
247 ( NDR_CCONTEXT )phContext,
248 1);
249 printf("After NdrClientContextMarshall: Buflen=%d\nBuffer: ", _StubMsg.BufferLength );
250 for(buf = _StubMsg.Buffer, i = 0; i < _StubMsg.BufferLength; i++)
251 printf("0x%x, ", buf[i] & 0x0FF);
252 printf("\n\n");
253
254 NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );
255
256 }
257 RpcFinally
258 {
259 NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );
260
261 }
262 RpcEndFinally
263
264 }
265
266
267 void m_CtxClose(
268 /* [out][in] */ PCTXTYPE __RPC_FAR *pphContext)
269 {
270
271 RPC_BINDING_HANDLE _Handle = 0;
272
273 RPC_MESSAGE _RpcMessage;
274
275 MIDL_STUB_MESSAGE _StubMsg;
276 char *buf;
277 int i;
278
279 printf("\n*******************************************************************\n");
280 printf("**** CtxClose() ***\n");
281 printf("*******************************************************************\n\n");
282
283 if(!pphContext)
284 {
285 RpcRaiseException(RPC_X_NULL_REF_POINTER);
286 }
287 RpcTryFinally
288 {
289 NdrClientInitializeNew(
290 ( PRPC_MESSAGE )&_RpcMessage,
291 ( PMIDL_STUB_MESSAGE )&_StubMsg,
292 ( PMIDL_STUB_DESC )&hello_StubDesc,
293 2);
294
295
296 if(*pphContext != 0)
297 {
298 _Handle = NDRCContextBinding(( NDR_CCONTEXT )*pphContext);;
299
300 }
301
302 _StubMsg.BufferLength = 20U;
303 NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg, _StubMsg.BufferLength, _Handle );
304
305 NdrClientContextMarshall(
306 ( PMIDL_STUB_MESSAGE )&_StubMsg,
307 ( NDR_CCONTEXT )*pphContext,
308 0);
309
310 NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );
311
312 if ( (_RpcMessage.DataRepresentation & 0X0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION )
313 NdrConvert( (PMIDL_STUB_MESSAGE) &_StubMsg, (PFORMAT_STRING) &__MIDL_ProcFormatString.Format[14] );
314
315
316 printf("Before NdrClientContextUnmarshall: Buflen=%d\nBuffer: ", _StubMsg.BufferLength );
317 for(buf = _StubMsg.Buffer, i = 0; i < _StubMsg.BufferLength; i++)
318 printf("0x%x, ", buf[i] & 0x0FF);
319 printf("\n\n");
320
321 NdrClientContextUnmarshall(
322 ( PMIDL_STUB_MESSAGE )&_StubMsg,
323 ( NDR_CCONTEXT __RPC_FAR * )pphContext,
324 _Handle);
325
326
327 printf("\nNdrClientContextUnmarshall returned: handle=0x%p\n", *pphContext);
328
329
330 }
331 RpcFinally
332 {
333 NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );
334
335 }
336 RpcEndFinally
337
338 }
339
340 int interactive = 0;
341 void interact()
342 {
343 if(interactive)
344 {
345 printf("\nPress any key to continue...");
346 getch();
347 printf("\n\n");
348 }
349 }
350
351 void main(int argc, char **argv)
352 {
353 RPC_STATUS status;
354 unsigned long ulCode;
355 PCTXTYPE hContext;
356 char *pszStringBinding = NULL;
357 RPC_BINDING_HANDLE Handle = 0;
358 char buffer[255];
359
360 int test_num = 0;
361 int test_value = 31337;
362
363 if(argc<2)
364 {
365 printf("USAGE: client.exe <test_number> [test_value] [interactive]\n"
366 "Available tests:\n"
367 "0. General test\n"
368 "1. NULL pointer test\n"
369 "2. Context rundown routine");
370 return;
371 }
372
373 test_num = atoi(argv[1]);
374 if(argc>2) test_value = atoi(argv[2]);
375 if(argc>3) interactive = 1;
376
377 status = RpcStringBindingCompose(NULL,
378 "ncacn_np",
379 NULL,
380 "\\pipe\\hello",
381 NULL,
382 &pszStringBinding);
383
384 if (status)
385 {
386 printf("RpcStringBindingCompose %x\n", status);
387 exit(status);
388 }
389
390 status = RpcBindingFromStringBinding(pszStringBinding, &hBinding);
391
392 if (status)
393 {
394 printf("RpcBindingFromStringBinding %x\n", status);
395 exit(status);
396 }
397
398 RpcStringFree(&pszStringBinding);
399
400 switch(test_num)
401 {
402 case 0:
403 m_CtxOpen(&hContext, test_value);
404 RpcBindingFree(&hBinding);
405 m_CtxHello(hContext);
406 interact();
407 m_CtxClose(&hContext);
408 break;
409 case 1:
410 /////////////////////////////////////////////////////////////////////////////////////////
411 RpcTryExcept
412 {
413 Handle = NDRCContextBinding(NULL);
414 printf("NDRCContextBinding(NULL) returned %p\n", Handle);
415 }
416 RpcExcept(1)
417 {
418 printf("NDRCContextBinding(NULL) reported exception = %d\n", RpcExceptionCode());
419 }
420 RpcEndExcept
421
422 m_CtxOpen2(&hContext, test_value);
423
424 /////////////////////////////////////////////////////////////////////////////////////////
425 RpcTryExcept
426 {
427 NDRCContextMarshall(NULL, &buffer);
428 printf("NDRCContextMarshall(NULL) returned %p\n", Handle);
429 }
430 RpcExcept(1)
431 {
432 printf("NDRCContextMarshall(NULL) reported exception = %d\n", RpcExceptionCode());
433 }
434 RpcEndExcept
435 /////////////////////////////////////////////////////////////////////////////////////////
436 break;
437 case 2:
438 CtxOpen(&hContext, test_value);
439 interact();
440 ExitProcess(0);
441 break;
442 default:
443 printf("Unknown test %d\n", test_num);
444 }
445
446 }
447
448
449 void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
450 {
451 return(malloc(len));
452 }
453
454 void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
455 {
456 free(ptr);
457 }