shdocvw: Add German translation.
[wine/multimedia.git] / dlls / ole32 / rpc.c
blob75b6b1f7ab3a24b140966b68f04830e3071aa3d0
1 /*
2 * RPC Manager
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2002 Marcus Meissner
6 * Copyright 2005 Mike Hearn, Rob Shearman for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <string.h>
29 #define COBJMACROS
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "winsvc.h"
37 #include "objbase.h"
38 #include "ole2.h"
39 #include "rpc.h"
40 #include "winerror.h"
41 #include "winreg.h"
42 #include "wine/unicode.h"
44 #include "compobj_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
50 static void __RPC_STUB dispatch_rpc(RPC_MESSAGE *msg);
52 /* we only use one function to dispatch calls for all methods - we use the
53 * RPC_IF_OLE flag to tell the RPC runtime that this is the case */
54 static RPC_DISPATCH_FUNCTION rpc_dispatch_table[1] = { dispatch_rpc }; /* (RO) */
55 static RPC_DISPATCH_TABLE rpc_dispatch = { 1, rpc_dispatch_table }; /* (RO) */
57 static struct list registered_interfaces = LIST_INIT(registered_interfaces); /* (CS csRegIf) */
58 static CRITICAL_SECTION csRegIf;
59 static CRITICAL_SECTION_DEBUG csRegIf_debug =
61 0, 0, &csRegIf,
62 { &csRegIf_debug.ProcessLocksList, &csRegIf_debug.ProcessLocksList },
63 0, 0, { (DWORD_PTR)(__FILE__ ": dcom registered server interfaces") }
65 static CRITICAL_SECTION csRegIf = { &csRegIf_debug, -1, 0, 0, 0, 0 };
67 static struct list channel_hooks = LIST_INIT(channel_hooks); /* (CS csChannelHook) */
68 static CRITICAL_SECTION csChannelHook;
69 static CRITICAL_SECTION_DEBUG csChannelHook_debug =
71 0, 0, &csChannelHook,
72 { &csChannelHook_debug.ProcessLocksList, &csChannelHook_debug.ProcessLocksList },
73 0, 0, { (DWORD_PTR)(__FILE__ ": channel hooks") }
75 static CRITICAL_SECTION csChannelHook = { &csChannelHook_debug, -1, 0, 0, 0, 0 };
77 static WCHAR wszRpcTransport[] = {'n','c','a','l','r','p','c',0};
80 struct registered_if
82 struct list entry;
83 DWORD refs; /* ref count */
84 RPC_SERVER_INTERFACE If; /* interface registered with the RPC runtime */
87 /* get the pipe endpoint specified of the specified apartment */
88 static inline void get_rpc_endpoint(LPWSTR endpoint, const OXID *oxid)
90 /* FIXME: should get endpoint from rpcss */
91 static const WCHAR wszEndpointFormat[] = {'\\','p','i','p','e','\\','O','L','E','_','%','0','8','l','x','%','0','8','l','x',0};
92 wsprintfW(endpoint, wszEndpointFormat, (DWORD)(*oxid >> 32),(DWORD)*oxid);
95 typedef struct
97 const IRpcChannelBufferVtbl *lpVtbl;
98 LONG refs;
99 } RpcChannelBuffer;
101 typedef struct
103 RpcChannelBuffer super; /* superclass */
105 RPC_BINDING_HANDLE bind; /* handle to the remote server */
106 OXID oxid; /* apartment in which the channel is valid */
107 DWORD server_pid; /* id of server process */
108 DWORD dest_context; /* returned from GetDestCtx */
109 LPVOID dest_context_data; /* returned from GetDestCtx */
110 HANDLE event; /* cached event handle */
111 } ClientRpcChannelBuffer;
113 struct dispatch_params
115 RPCOLEMESSAGE *msg; /* message */
116 IRpcStubBuffer *stub; /* stub buffer, if applicable */
117 IRpcChannelBuffer *chan; /* server channel buffer, if applicable */
118 IID iid; /* ID of interface being called */
119 IUnknown *iface; /* interface being called */
120 HANDLE handle; /* handle that will become signaled when call finishes */
121 BOOL bypass_rpcrt; /* bypass RPC runtime? */
122 RPC_STATUS status; /* status (out) */
123 HRESULT hr; /* hresult (out) */
126 struct message_state
128 RPC_BINDING_HANDLE binding_handle;
129 ULONG prefix_data_len;
130 SChannelHookCallInfo channel_hook_info;
131 BOOL bypass_rpcrt;
133 /* client only */
134 HWND target_hwnd;
135 DWORD target_tid;
136 struct dispatch_params params;
139 typedef struct
141 ULONG conformance; /* NDR */
142 GUID id;
143 ULONG size;
144 /* [size_is((size+7)&~7)] */ unsigned char data[1];
145 } WIRE_ORPC_EXTENT;
147 typedef struct
149 ULONG size;
150 ULONG reserved;
151 unsigned char extent[1];
152 } WIRE_ORPC_EXTENT_ARRAY;
154 typedef struct
156 ULONG version;
157 ULONG flags;
158 ULONG reserved1;
159 GUID cid;
160 unsigned char extensions[1];
161 } WIRE_ORPCTHIS;
163 typedef struct
165 ULONG flags;
166 unsigned char extensions[1];
167 } WIRE_ORPCTHAT;
169 struct channel_hook_entry
171 struct list entry;
172 GUID id;
173 IChannelHook *hook;
176 struct channel_hook_buffer_data
178 GUID id;
179 ULONG extension_size;
183 static HRESULT unmarshal_ORPCTHAT(RPC_MESSAGE *msg, ORPCTHAT *orpcthat,
184 ORPC_EXTENT_ARRAY *orpc_ext_array, WIRE_ORPC_EXTENT **first_wire_orpc_extent);
186 /* Channel Hook Functions */
188 static ULONG ChannelHooks_ClientGetSize(SChannelHookCallInfo *info,
189 struct channel_hook_buffer_data **data, unsigned int *hook_count,
190 ULONG *extension_count)
192 struct channel_hook_entry *entry;
193 ULONG total_size = 0;
194 unsigned int hook_index = 0;
196 *hook_count = 0;
197 *extension_count = 0;
199 EnterCriticalSection(&csChannelHook);
201 LIST_FOR_EACH_ENTRY(entry, &channel_hooks, struct channel_hook_entry, entry)
202 (*hook_count)++;
204 if (*hook_count)
205 *data = HeapAlloc(GetProcessHeap(), 0, *hook_count * sizeof(struct channel_hook_buffer_data));
206 else
207 *data = NULL;
209 LIST_FOR_EACH_ENTRY(entry, &channel_hooks, struct channel_hook_entry, entry)
211 ULONG extension_size = 0;
213 IChannelHook_ClientGetSize(entry->hook, &entry->id, &info->iid, &extension_size);
215 TRACE("%s: extension_size = %u\n", debugstr_guid(&entry->id), extension_size);
217 extension_size = (extension_size+7)&~7;
218 (*data)[hook_index].id = entry->id;
219 (*data)[hook_index].extension_size = extension_size;
221 /* an extension is only put onto the wire if it has data to write */
222 if (extension_size)
224 total_size += FIELD_OFFSET(WIRE_ORPC_EXTENT, data[extension_size]);
225 (*extension_count)++;
228 hook_index++;
231 LeaveCriticalSection(&csChannelHook);
233 return total_size;
236 static unsigned char * ChannelHooks_ClientFillBuffer(SChannelHookCallInfo *info,
237 unsigned char *buffer, struct channel_hook_buffer_data *data,
238 unsigned int hook_count)
240 struct channel_hook_entry *entry;
242 EnterCriticalSection(&csChannelHook);
244 LIST_FOR_EACH_ENTRY(entry, &channel_hooks, struct channel_hook_entry, entry)
246 unsigned int i;
247 ULONG extension_size = 0;
248 WIRE_ORPC_EXTENT *wire_orpc_extent = (WIRE_ORPC_EXTENT *)buffer;
250 for (i = 0; i < hook_count; i++)
251 if (IsEqualGUID(&entry->id, &data[i].id))
252 extension_size = data[i].extension_size;
254 /* an extension is only put onto the wire if it has data to write */
255 if (!extension_size)
256 continue;
258 IChannelHook_ClientFillBuffer(entry->hook, &entry->id, &info->iid,
259 &extension_size, buffer + FIELD_OFFSET(WIRE_ORPC_EXTENT, data[0]));
261 TRACE("%s: extension_size = %u\n", debugstr_guid(&entry->id), extension_size);
263 /* FIXME: set unused portion of wire_orpc_extent->data to 0? */
265 wire_orpc_extent->conformance = (extension_size+7)&~7;
266 wire_orpc_extent->size = extension_size;
267 wire_orpc_extent->id = entry->id;
268 buffer += FIELD_OFFSET(WIRE_ORPC_EXTENT, data[wire_orpc_extent->conformance]);
271 LeaveCriticalSection(&csChannelHook);
273 return buffer;
276 static void ChannelHooks_ServerNotify(SChannelHookCallInfo *info,
277 DWORD lDataRep, WIRE_ORPC_EXTENT *first_wire_orpc_extent,
278 ULONG extension_count)
280 struct channel_hook_entry *entry;
281 ULONG i;
283 EnterCriticalSection(&csChannelHook);
285 LIST_FOR_EACH_ENTRY(entry, &channel_hooks, struct channel_hook_entry, entry)
287 WIRE_ORPC_EXTENT *wire_orpc_extent;
288 for (i = 0, wire_orpc_extent = first_wire_orpc_extent;
289 i < extension_count;
290 i++, wire_orpc_extent = (WIRE_ORPC_EXTENT *)&wire_orpc_extent->data[wire_orpc_extent->conformance])
292 if (IsEqualGUID(&entry->id, &wire_orpc_extent->id))
293 break;
295 if (i == extension_count) wire_orpc_extent = NULL;
297 IChannelHook_ServerNotify(entry->hook, &entry->id, &info->iid,
298 wire_orpc_extent ? wire_orpc_extent->size : 0,
299 wire_orpc_extent ? wire_orpc_extent->data : NULL,
300 lDataRep);
303 LeaveCriticalSection(&csChannelHook);
306 static ULONG ChannelHooks_ServerGetSize(SChannelHookCallInfo *info,
307 struct channel_hook_buffer_data **data, unsigned int *hook_count,
308 ULONG *extension_count)
310 struct channel_hook_entry *entry;
311 ULONG total_size = 0;
312 unsigned int hook_index = 0;
314 *hook_count = 0;
315 *extension_count = 0;
317 EnterCriticalSection(&csChannelHook);
319 LIST_FOR_EACH_ENTRY(entry, &channel_hooks, struct channel_hook_entry, entry)
320 (*hook_count)++;
322 if (*hook_count)
323 *data = HeapAlloc(GetProcessHeap(), 0, *hook_count * sizeof(struct channel_hook_buffer_data));
324 else
325 *data = NULL;
327 LIST_FOR_EACH_ENTRY(entry, &channel_hooks, struct channel_hook_entry, entry)
329 ULONG extension_size = 0;
331 IChannelHook_ServerGetSize(entry->hook, &entry->id, &info->iid, S_OK,
332 &extension_size);
334 TRACE("%s: extension_size = %u\n", debugstr_guid(&entry->id), extension_size);
336 extension_size = (extension_size+7)&~7;
337 (*data)[hook_index].id = entry->id;
338 (*data)[hook_index].extension_size = extension_size;
340 /* an extension is only put onto the wire if it has data to write */
341 if (extension_size)
343 total_size += FIELD_OFFSET(WIRE_ORPC_EXTENT, data[extension_size]);
344 (*extension_count)++;
347 hook_index++;
350 LeaveCriticalSection(&csChannelHook);
352 return total_size;
355 static unsigned char * ChannelHooks_ServerFillBuffer(SChannelHookCallInfo *info,
356 unsigned char *buffer, struct channel_hook_buffer_data *data,
357 unsigned int hook_count)
359 struct channel_hook_entry *entry;
361 EnterCriticalSection(&csChannelHook);
363 LIST_FOR_EACH_ENTRY(entry, &channel_hooks, struct channel_hook_entry, entry)
365 unsigned int i;
366 ULONG extension_size = 0;
367 WIRE_ORPC_EXTENT *wire_orpc_extent = (WIRE_ORPC_EXTENT *)buffer;
369 for (i = 0; i < hook_count; i++)
370 if (IsEqualGUID(&entry->id, &data[i].id))
371 extension_size = data[i].extension_size;
373 /* an extension is only put onto the wire if it has data to write */
374 if (!extension_size)
375 continue;
377 IChannelHook_ServerFillBuffer(entry->hook, &entry->id, &info->iid,
378 &extension_size, buffer + FIELD_OFFSET(WIRE_ORPC_EXTENT, data[0]),
379 S_OK);
381 TRACE("%s: extension_size = %u\n", debugstr_guid(&entry->id), extension_size);
383 /* FIXME: set unused portion of wire_orpc_extent->data to 0? */
385 wire_orpc_extent->conformance = (extension_size+7)&~7;
386 wire_orpc_extent->size = extension_size;
387 wire_orpc_extent->id = entry->id;
388 buffer += FIELD_OFFSET(WIRE_ORPC_EXTENT, data[wire_orpc_extent->conformance]);
391 LeaveCriticalSection(&csChannelHook);
393 return buffer;
396 static void ChannelHooks_ClientNotify(SChannelHookCallInfo *info,
397 DWORD lDataRep, WIRE_ORPC_EXTENT *first_wire_orpc_extent,
398 ULONG extension_count, HRESULT hrFault)
400 struct channel_hook_entry *entry;
401 ULONG i;
403 EnterCriticalSection(&csChannelHook);
405 LIST_FOR_EACH_ENTRY(entry, &channel_hooks, struct channel_hook_entry, entry)
407 WIRE_ORPC_EXTENT *wire_orpc_extent;
408 for (i = 0, wire_orpc_extent = first_wire_orpc_extent;
409 i < extension_count;
410 i++, wire_orpc_extent = (WIRE_ORPC_EXTENT *)&wire_orpc_extent->data[wire_orpc_extent->conformance])
412 if (IsEqualGUID(&entry->id, &wire_orpc_extent->id))
413 break;
415 if (i == extension_count) wire_orpc_extent = NULL;
417 IChannelHook_ClientNotify(entry->hook, &entry->id, &info->iid,
418 wire_orpc_extent ? wire_orpc_extent->size : 0,
419 wire_orpc_extent ? wire_orpc_extent->data : NULL,
420 lDataRep, hrFault);
423 LeaveCriticalSection(&csChannelHook);
426 HRESULT RPC_RegisterChannelHook(REFGUID rguid, IChannelHook *hook)
428 struct channel_hook_entry *entry;
430 TRACE("(%s, %p)\n", debugstr_guid(rguid), hook);
432 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
433 if (!entry)
434 return E_OUTOFMEMORY;
436 entry->id = *rguid;
437 entry->hook = hook;
438 IChannelHook_AddRef(hook);
440 EnterCriticalSection(&csChannelHook);
441 list_add_tail(&channel_hooks, &entry->entry);
442 LeaveCriticalSection(&csChannelHook);
444 return S_OK;
447 void RPC_UnregisterAllChannelHooks(void)
449 struct channel_hook_entry *cursor;
450 struct channel_hook_entry *cursor2;
452 EnterCriticalSection(&csChannelHook);
453 LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, &channel_hooks, struct channel_hook_entry, entry)
454 HeapFree(GetProcessHeap(), 0, cursor);
455 LeaveCriticalSection(&csChannelHook);
458 /* RPC Channel Buffer Functions */
460 static HRESULT WINAPI RpcChannelBuffer_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
462 *ppv = NULL;
463 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
465 *ppv = iface;
466 IUnknown_AddRef(iface);
467 return S_OK;
469 return E_NOINTERFACE;
472 static ULONG WINAPI RpcChannelBuffer_AddRef(LPRPCCHANNELBUFFER iface)
474 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
475 return InterlockedIncrement(&This->refs);
478 static ULONG WINAPI ServerRpcChannelBuffer_Release(LPRPCCHANNELBUFFER iface)
480 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
481 ULONG ref;
483 ref = InterlockedDecrement(&This->refs);
484 if (ref)
485 return ref;
487 HeapFree(GetProcessHeap(), 0, This);
488 return 0;
491 static ULONG WINAPI ClientRpcChannelBuffer_Release(LPRPCCHANNELBUFFER iface)
493 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
494 ULONG ref;
496 ref = InterlockedDecrement(&This->super.refs);
497 if (ref)
498 return ref;
500 if (This->event) CloseHandle(This->event);
501 RpcBindingFree(&This->bind);
502 HeapFree(GetProcessHeap(), 0, This);
503 return 0;
506 static HRESULT WINAPI ServerRpcChannelBuffer_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
508 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
509 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
510 RPC_STATUS status;
511 ORPCTHAT *orpcthat;
512 struct message_state *message_state;
513 ULONG extensions_size;
514 struct channel_hook_buffer_data *channel_hook_data;
515 unsigned int channel_hook_count;
516 ULONG extension_count;
518 TRACE("(%p)->(%p,%s)\n", This, olemsg, debugstr_guid(riid));
520 message_state = msg->Handle;
521 /* restore the binding handle and the real start of data */
522 msg->Handle = message_state->binding_handle;
523 msg->Buffer = (char *)msg->Buffer - message_state->prefix_data_len;
525 extensions_size = ChannelHooks_ServerGetSize(&message_state->channel_hook_info,
526 &channel_hook_data, &channel_hook_count, &extension_count);
528 msg->BufferLength += FIELD_OFFSET(WIRE_ORPCTHAT, extensions) + sizeof(DWORD);
529 if (extensions_size)
531 msg->BufferLength += FIELD_OFFSET(WIRE_ORPC_EXTENT_ARRAY, extent[2*sizeof(DWORD) + extensions_size]);
532 if (extension_count & 1)
533 msg->BufferLength += FIELD_OFFSET(WIRE_ORPC_EXTENT, data[0]);
536 if (message_state->bypass_rpcrt)
538 msg->Buffer = HeapAlloc(GetProcessHeap(), 0, msg->BufferLength);
539 if (msg->Buffer)
540 status = RPC_S_OK;
541 else
542 status = ERROR_OUTOFMEMORY;
544 else
545 status = I_RpcGetBuffer(msg);
547 orpcthat = msg->Buffer;
548 msg->Buffer = (char *)msg->Buffer + FIELD_OFFSET(WIRE_ORPCTHAT, extensions);
550 orpcthat->flags = ORPCF_NULL /* FIXME? */;
552 /* NDR representation of orpcthat->extensions */
553 *(DWORD *)msg->Buffer = extensions_size ? 1 : 0;
554 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
556 if (extensions_size)
558 WIRE_ORPC_EXTENT_ARRAY *orpc_extent_array = msg->Buffer;
559 orpc_extent_array->size = extension_count;
560 orpc_extent_array->reserved = 0;
561 msg->Buffer = (char *)msg->Buffer + FIELD_OFFSET(WIRE_ORPC_EXTENT_ARRAY, extent);
562 /* NDR representation of orpc_extent_array->extent */
563 *(DWORD *)msg->Buffer = 1;
564 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
565 /* NDR representation of [size_is] attribute of orpc_extent_array->extent */
566 *(DWORD *)msg->Buffer = (extension_count + 1) & ~1;
567 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
569 msg->Buffer = ChannelHooks_ServerFillBuffer(&message_state->channel_hook_info,
570 msg->Buffer, channel_hook_data, channel_hook_count);
572 /* we must add a dummy extension if there is an odd extension
573 * count to meet the contract specified by the size_is attribute */
574 if (extension_count & 1)
576 WIRE_ORPC_EXTENT *wire_orpc_extent = msg->Buffer;
577 wire_orpc_extent->conformance = 0;
578 wire_orpc_extent->id = GUID_NULL;
579 wire_orpc_extent->size = 0;
580 msg->Buffer = (char *)msg->Buffer + FIELD_OFFSET(WIRE_ORPC_EXTENT, data[0]);
584 HeapFree(GetProcessHeap(), 0, channel_hook_data);
586 /* store the prefixed data length so that we can restore the real buffer
587 * later */
588 message_state->prefix_data_len = (char *)msg->Buffer - (char *)orpcthat;
589 msg->BufferLength -= message_state->prefix_data_len;
590 /* save away the message state again */
591 msg->Handle = message_state;
593 TRACE("-- %d\n", status);
595 return HRESULT_FROM_WIN32(status);
598 static HANDLE ClientRpcChannelBuffer_GetEventHandle(ClientRpcChannelBuffer *This)
600 HANDLE event = InterlockedExchangePointer(&This->event, NULL);
602 /* Note: must be auto-reset event so we can reuse it without a call
603 * to ResetEvent */
604 if (!event) event = CreateEventW(NULL, FALSE, FALSE, NULL);
606 return event;
609 static void ClientRpcChannelBuffer_ReleaseEventHandle(ClientRpcChannelBuffer *This, HANDLE event)
611 if (InterlockedCompareExchangePointer(&This->event, event, NULL))
612 /* already a handle cached in This */
613 CloseHandle(event);
616 static HRESULT WINAPI ClientRpcChannelBuffer_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
618 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
619 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
620 RPC_CLIENT_INTERFACE *cif;
621 RPC_STATUS status;
622 ORPCTHIS *orpcthis;
623 struct message_state *message_state;
624 ULONG extensions_size;
625 struct channel_hook_buffer_data *channel_hook_data;
626 unsigned int channel_hook_count;
627 ULONG extension_count;
628 IPID ipid;
629 HRESULT hr;
630 APARTMENT *apt = NULL;
632 TRACE("(%p)->(%p,%s)\n", This, olemsg, debugstr_guid(riid));
634 cif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_CLIENT_INTERFACE));
635 if (!cif)
636 return E_OUTOFMEMORY;
638 message_state = HeapAlloc(GetProcessHeap(), 0, sizeof(*message_state));
639 if (!message_state)
641 HeapFree(GetProcessHeap(), 0, cif);
642 return E_OUTOFMEMORY;
645 cif->Length = sizeof(RPC_CLIENT_INTERFACE);
646 /* RPC interface ID = COM interface ID */
647 cif->InterfaceId.SyntaxGUID = *riid;
648 /* COM objects always have a version of 0.0 */
649 cif->InterfaceId.SyntaxVersion.MajorVersion = 0;
650 cif->InterfaceId.SyntaxVersion.MinorVersion = 0;
651 msg->Handle = This->bind;
652 msg->RpcInterfaceInformation = cif;
654 message_state->prefix_data_len = 0;
655 message_state->binding_handle = This->bind;
657 message_state->channel_hook_info.iid = *riid;
658 message_state->channel_hook_info.cbSize = sizeof(message_state->channel_hook_info);
659 message_state->channel_hook_info.uCausality = COM_CurrentCausalityId();
660 message_state->channel_hook_info.dwServerPid = This->server_pid;
661 message_state->channel_hook_info.iMethod = msg->ProcNum;
662 message_state->channel_hook_info.pObject = NULL; /* only present on server-side */
663 message_state->target_hwnd = NULL;
664 message_state->target_tid = 0;
665 memset(&message_state->params, 0, sizeof(message_state->params));
667 extensions_size = ChannelHooks_ClientGetSize(&message_state->channel_hook_info,
668 &channel_hook_data, &channel_hook_count, &extension_count);
670 msg->BufferLength += FIELD_OFFSET(WIRE_ORPCTHIS, extensions) + sizeof(DWORD);
671 if (extensions_size)
673 msg->BufferLength += FIELD_OFFSET(WIRE_ORPC_EXTENT_ARRAY, extent[2*sizeof(DWORD) + extensions_size]);
674 if (extension_count & 1)
675 msg->BufferLength += FIELD_OFFSET(WIRE_ORPC_EXTENT, data[0]);
678 RpcBindingInqObject(message_state->binding_handle, &ipid);
679 hr = ipid_get_dispatch_params(&ipid, &apt, &message_state->params.stub,
680 &message_state->params.chan,
681 &message_state->params.iid,
682 &message_state->params.iface);
683 if (hr == S_OK)
685 /* stub, chan, iface and iid are unneeded in multi-threaded case as we go
686 * via the RPC runtime */
687 if (apt->multi_threaded)
689 IRpcStubBuffer_Release(message_state->params.stub);
690 message_state->params.stub = NULL;
691 IRpcChannelBuffer_Release(message_state->params.chan);
692 message_state->params.chan = NULL;
693 message_state->params.iface = NULL;
695 else
697 message_state->params.bypass_rpcrt = TRUE;
698 message_state->target_hwnd = apartment_getwindow(apt);
699 message_state->target_tid = apt->tid;
700 /* we assume later on that this being non-NULL is the indicator that
701 * means call directly instead of going through RPC runtime */
702 if (!message_state->target_hwnd)
703 ERR("window for apartment %s is NULL\n", wine_dbgstr_longlong(apt->oxid));
706 if (apt) apartment_release(apt);
707 message_state->params.handle = ClientRpcChannelBuffer_GetEventHandle(This);
708 /* Note: message_state->params.msg is initialised in
709 * ClientRpcChannelBuffer_SendReceive */
711 /* shortcut the RPC runtime */
712 if (message_state->target_hwnd)
714 msg->Buffer = HeapAlloc(GetProcessHeap(), 0, msg->BufferLength);
715 if (msg->Buffer)
716 status = RPC_S_OK;
717 else
718 status = ERROR_OUTOFMEMORY;
720 else
721 status = I_RpcGetBuffer(msg);
723 msg->Handle = message_state;
725 if (status == RPC_S_OK)
727 orpcthis = msg->Buffer;
728 msg->Buffer = (char *)msg->Buffer + FIELD_OFFSET(WIRE_ORPCTHIS, extensions);
730 orpcthis->version.MajorVersion = COM_MAJOR_VERSION;
731 orpcthis->version.MinorVersion = COM_MINOR_VERSION;
732 orpcthis->flags = message_state->channel_hook_info.dwServerPid ? ORPCF_LOCAL : ORPCF_NULL;
733 orpcthis->reserved1 = 0;
734 orpcthis->cid = message_state->channel_hook_info.uCausality;
736 /* NDR representation of orpcthis->extensions */
737 *(DWORD *)msg->Buffer = extensions_size ? 1 : 0;
738 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
740 if (extensions_size)
742 ORPC_EXTENT_ARRAY *orpc_extent_array = msg->Buffer;
743 orpc_extent_array->size = extension_count;
744 orpc_extent_array->reserved = 0;
745 msg->Buffer = (char *)msg->Buffer + FIELD_OFFSET(WIRE_ORPC_EXTENT_ARRAY, extent);
746 /* NDR representation of orpc_extent_array->extent */
747 *(DWORD *)msg->Buffer = 1;
748 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
749 /* NDR representation of [size_is] attribute of orpc_extent_array->extent */
750 *(DWORD *)msg->Buffer = (extension_count + 1) & ~1;
751 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
753 msg->Buffer = ChannelHooks_ClientFillBuffer(&message_state->channel_hook_info,
754 msg->Buffer, channel_hook_data, channel_hook_count);
756 /* we must add a dummy extension if there is an odd extension
757 * count to meet the contract specified by the size_is attribute */
758 if (extension_count & 1)
760 WIRE_ORPC_EXTENT *wire_orpc_extent = msg->Buffer;
761 wire_orpc_extent->conformance = 0;
762 wire_orpc_extent->id = GUID_NULL;
763 wire_orpc_extent->size = 0;
764 msg->Buffer = (char *)msg->Buffer + FIELD_OFFSET(WIRE_ORPC_EXTENT, data[0]);
768 /* store the prefixed data length so that we can restore the real buffer
769 * pointer in ClientRpcChannelBuffer_SendReceive. */
770 message_state->prefix_data_len = (char *)msg->Buffer - (char *)orpcthis;
771 msg->BufferLength -= message_state->prefix_data_len;
774 HeapFree(GetProcessHeap(), 0, channel_hook_data);
776 TRACE("-- %d\n", status);
778 return HRESULT_FROM_WIN32(status);
781 static HRESULT WINAPI ServerRpcChannelBuffer_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
783 FIXME("stub\n");
784 return E_NOTIMPL;
787 /* this thread runs an outgoing RPC */
788 static DWORD WINAPI rpc_sendreceive_thread(LPVOID param)
790 struct dispatch_params *data = param;
792 /* Note: I_RpcSendReceive doesn't raise exceptions like the higher-level
793 * RPC functions do */
794 data->status = I_RpcSendReceive((RPC_MESSAGE *)data->msg);
796 TRACE("completed with status 0x%x\n", data->status);
798 SetEvent(data->handle);
800 return 0;
803 static inline HRESULT ClientRpcChannelBuffer_IsCorrectApartment(ClientRpcChannelBuffer *This, APARTMENT *apt)
805 OXID oxid;
806 if (!apt)
807 return S_FALSE;
808 if (apartment_getoxid(apt, &oxid) != S_OK)
809 return S_FALSE;
810 if (This->oxid != oxid)
811 return S_FALSE;
812 return S_OK;
815 static HRESULT WINAPI ClientRpcChannelBuffer_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
817 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
818 HRESULT hr;
819 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
820 RPC_STATUS status;
821 DWORD index;
822 struct message_state *message_state;
823 ORPCTHAT orpcthat;
824 ORPC_EXTENT_ARRAY orpc_ext_array;
825 WIRE_ORPC_EXTENT *first_wire_orpc_extent = NULL;
826 HRESULT hrFault = S_OK;
828 TRACE("(%p) iMethod=%d\n", olemsg, olemsg->iMethod);
830 hr = ClientRpcChannelBuffer_IsCorrectApartment(This, COM_CurrentApt());
831 if (hr != S_OK)
833 ERR("called from wrong apartment, should have been 0x%s\n",
834 wine_dbgstr_longlong(This->oxid));
835 return RPC_E_WRONG_THREAD;
837 /* This situation should be impossible in multi-threaded apartments,
838 * because the calling thread isn't re-enterable.
839 * Note: doing a COM call during the processing of a sent message is
840 * only disallowed if a client call is already being waited for
841 * completion */
842 if (!COM_CurrentApt()->multi_threaded &&
843 COM_CurrentInfo()->pending_call_count_client &&
844 InSendMessage())
846 ERR("can't make an outgoing COM call in response to a sent message\n");
847 return RPC_E_CANTCALLOUT_ININPUTSYNCCALL;
850 message_state = msg->Handle;
851 /* restore the binding handle and the real start of data */
852 msg->Handle = message_state->binding_handle;
853 msg->Buffer = (char *)msg->Buffer - message_state->prefix_data_len;
854 msg->BufferLength += message_state->prefix_data_len;
856 /* Note: this is an optimization in the Microsoft OLE runtime that we need
857 * to copy, as shown by the test_no_couninitialize_client test. without
858 * short-circuiting the RPC runtime in the case below, the test will
859 * deadlock on the loader lock due to the RPC runtime needing to create
860 * a thread to process the RPC when this function is called indirectly
861 * from DllMain */
863 message_state->params.msg = olemsg;
864 if (message_state->params.bypass_rpcrt)
866 TRACE("Calling apartment thread 0x%08x...\n", message_state->target_tid);
868 msg->ProcNum &= ~RPC_FLAGS_VALID_BIT;
870 if (!PostMessageW(message_state->target_hwnd, DM_EXECUTERPC, 0,
871 (LPARAM)&message_state->params))
873 ERR("PostMessage failed with error %u\n", GetLastError());
875 /* Note: message_state->params.iface doesn't have a reference and
876 * so doesn't need to be released */
878 hr = HRESULT_FROM_WIN32(GetLastError());
881 else
883 /* we use a separate thread here because we need to be able to
884 * pump the message loop in the application thread: if we do not,
885 * any windows created by this thread will hang and RPCs that try
886 * and re-enter this STA from an incoming server thread will
887 * deadlock. InstallShield is an example of that.
889 if (!QueueUserWorkItem(rpc_sendreceive_thread, &message_state->params, WT_EXECUTEDEFAULT))
891 ERR("QueueUserWorkItem failed with error %u\n", GetLastError());
892 hr = E_UNEXPECTED;
894 else
895 hr = S_OK;
898 if (hr == S_OK)
900 if (WaitForSingleObject(message_state->params.handle, 0))
902 COM_CurrentInfo()->pending_call_count_client++;
903 hr = CoWaitForMultipleHandles(0, INFINITE, 1, &message_state->params.handle, &index);
904 COM_CurrentInfo()->pending_call_count_client--;
907 ClientRpcChannelBuffer_ReleaseEventHandle(This, message_state->params.handle);
909 /* for WM shortcut, faults are returned in params->hr */
910 if (hr == S_OK)
911 hrFault = message_state->params.hr;
913 status = message_state->params.status;
915 orpcthat.flags = ORPCF_NULL;
916 orpcthat.extensions = NULL;
918 TRACE("RPC call status: 0x%x\n", status);
919 if (status != RPC_S_OK)
920 hr = HRESULT_FROM_WIN32(status);
922 TRACE("hrFault = 0x%08x\n", hrFault);
924 /* FIXME: this condition should be
925 * "hr == S_OK && (!hrFault || msg->BufferLength > FIELD_OFFSET(ORPCTHAT, extensions) + 4)"
926 * but we don't currently reset the message length for PostMessage
927 * dispatched calls */
928 if (hr == S_OK && hrFault == S_OK)
930 HRESULT hr2;
931 char *original_buffer = msg->Buffer;
933 /* handle ORPCTHAT and client extensions */
935 hr2 = unmarshal_ORPCTHAT(msg, &orpcthat, &orpc_ext_array, &first_wire_orpc_extent);
936 if (FAILED(hr2))
937 hr = hr2;
939 message_state->prefix_data_len = (char *)msg->Buffer - original_buffer;
940 msg->BufferLength -= message_state->prefix_data_len;
942 else
943 message_state->prefix_data_len = 0;
945 if (hr == S_OK)
947 ChannelHooks_ClientNotify(&message_state->channel_hook_info,
948 msg->DataRepresentation,
949 first_wire_orpc_extent,
950 orpcthat.extensions && first_wire_orpc_extent ? orpcthat.extensions->size : 0,
951 hrFault);
954 /* save away the message state again */
955 msg->Handle = message_state;
957 if (pstatus) *pstatus = status;
959 if (hr == S_OK)
960 hr = hrFault;
962 TRACE("-- 0x%08x\n", hr);
964 return hr;
967 static HRESULT WINAPI ServerRpcChannelBuffer_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
969 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
970 RPC_STATUS status;
971 struct message_state *message_state;
973 TRACE("(%p)\n", msg);
975 message_state = msg->Handle;
976 /* restore the binding handle and the real start of data */
977 msg->Handle = message_state->binding_handle;
978 msg->Buffer = (char *)msg->Buffer - message_state->prefix_data_len;
979 msg->BufferLength += message_state->prefix_data_len;
980 message_state->prefix_data_len = 0;
982 if (message_state->bypass_rpcrt)
984 HeapFree(GetProcessHeap(), 0, msg->Buffer);
985 status = RPC_S_OK;
987 else
988 status = I_RpcFreeBuffer(msg);
990 msg->Handle = message_state;
992 TRACE("-- %d\n", status);
994 return HRESULT_FROM_WIN32(status);
997 static HRESULT WINAPI ClientRpcChannelBuffer_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
999 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
1000 RPC_STATUS status;
1001 struct message_state *message_state;
1003 TRACE("(%p)\n", msg);
1005 message_state = msg->Handle;
1006 /* restore the binding handle and the real start of data */
1007 msg->Handle = message_state->binding_handle;
1008 msg->Buffer = (char *)msg->Buffer - message_state->prefix_data_len;
1009 msg->BufferLength += message_state->prefix_data_len;
1011 if (message_state->params.bypass_rpcrt)
1013 HeapFree(GetProcessHeap(), 0, msg->Buffer);
1014 status = RPC_S_OK;
1016 else
1017 status = I_RpcFreeBuffer(msg);
1019 HeapFree(GetProcessHeap(), 0, msg->RpcInterfaceInformation);
1020 msg->RpcInterfaceInformation = NULL;
1022 if (message_state->params.stub)
1023 IRpcStubBuffer_Release(message_state->params.stub);
1024 if (message_state->params.chan)
1025 IRpcChannelBuffer_Release(message_state->params.chan);
1026 HeapFree(GetProcessHeap(), 0, message_state);
1028 TRACE("-- %d\n", status);
1030 return HRESULT_FROM_WIN32(status);
1033 static HRESULT WINAPI ClientRpcChannelBuffer_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1035 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
1037 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1039 *pdwDestContext = This->dest_context;
1040 *ppvDestContext = This->dest_context_data;
1042 return S_OK;
1045 static HRESULT WINAPI ServerRpcChannelBuffer_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1047 WARN("(%p,%p), stub!\n", pdwDestContext, ppvDestContext);
1049 /* FIXME: implement this by storing the dwDestContext and pvDestContext
1050 * values passed into IMarshal_MarshalInterface and returning them here */
1051 *pdwDestContext = MSHCTX_DIFFERENTMACHINE;
1052 *ppvDestContext = NULL;
1053 return S_OK;
1056 static HRESULT WINAPI RpcChannelBuffer_IsConnected(LPRPCCHANNELBUFFER iface)
1058 TRACE("()\n");
1059 /* native does nothing too */
1060 return S_OK;
1063 static const IRpcChannelBufferVtbl ClientRpcChannelBufferVtbl =
1065 RpcChannelBuffer_QueryInterface,
1066 RpcChannelBuffer_AddRef,
1067 ClientRpcChannelBuffer_Release,
1068 ClientRpcChannelBuffer_GetBuffer,
1069 ClientRpcChannelBuffer_SendReceive,
1070 ClientRpcChannelBuffer_FreeBuffer,
1071 ClientRpcChannelBuffer_GetDestCtx,
1072 RpcChannelBuffer_IsConnected
1075 static const IRpcChannelBufferVtbl ServerRpcChannelBufferVtbl =
1077 RpcChannelBuffer_QueryInterface,
1078 RpcChannelBuffer_AddRef,
1079 ServerRpcChannelBuffer_Release,
1080 ServerRpcChannelBuffer_GetBuffer,
1081 ServerRpcChannelBuffer_SendReceive,
1082 ServerRpcChannelBuffer_FreeBuffer,
1083 ServerRpcChannelBuffer_GetDestCtx,
1084 RpcChannelBuffer_IsConnected
1087 /* returns a channel buffer for proxies */
1088 HRESULT RPC_CreateClientChannel(const OXID *oxid, const IPID *ipid,
1089 const OXID_INFO *oxid_info,
1090 DWORD dest_context, void *dest_context_data,
1091 IRpcChannelBuffer **chan)
1093 ClientRpcChannelBuffer *This;
1094 WCHAR endpoint[200];
1095 RPC_BINDING_HANDLE bind;
1096 RPC_STATUS status;
1097 LPWSTR string_binding;
1099 /* FIXME: get the endpoint from oxid_info->psa instead */
1100 get_rpc_endpoint(endpoint, oxid);
1102 TRACE("proxy pipe: connecting to endpoint: %s\n", debugstr_w(endpoint));
1104 status = RpcStringBindingComposeW(
1105 NULL,
1106 wszRpcTransport,
1107 NULL,
1108 endpoint,
1109 NULL,
1110 &string_binding);
1112 if (status == RPC_S_OK)
1114 status = RpcBindingFromStringBindingW(string_binding, &bind);
1116 if (status == RPC_S_OK)
1118 IPID ipid2 = *ipid; /* why can't RpcBindingSetObject take a const? */
1119 status = RpcBindingSetObject(bind, &ipid2);
1120 if (status != RPC_S_OK)
1121 RpcBindingFree(&bind);
1124 RpcStringFreeW(&string_binding);
1127 if (status != RPC_S_OK)
1129 ERR("Couldn't get binding for endpoint %s, status = %d\n", debugstr_w(endpoint), status);
1130 return HRESULT_FROM_WIN32(status);
1133 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1134 if (!This)
1136 RpcBindingFree(&bind);
1137 return E_OUTOFMEMORY;
1140 This->super.lpVtbl = &ClientRpcChannelBufferVtbl;
1141 This->super.refs = 1;
1142 This->bind = bind;
1143 apartment_getoxid(COM_CurrentApt(), &This->oxid);
1144 This->server_pid = oxid_info->dwPid;
1145 This->dest_context = dest_context;
1146 This->dest_context_data = dest_context_data;
1147 This->event = NULL;
1149 *chan = (IRpcChannelBuffer*)This;
1151 return S_OK;
1154 HRESULT RPC_CreateServerChannel(IRpcChannelBuffer **chan)
1156 RpcChannelBuffer *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1157 if (!This)
1158 return E_OUTOFMEMORY;
1160 This->lpVtbl = &ServerRpcChannelBufferVtbl;
1161 This->refs = 1;
1163 *chan = (IRpcChannelBuffer*)This;
1165 return S_OK;
1168 /* unmarshals ORPC_EXTENT_ARRAY according to NDR rules, but doesn't allocate
1169 * any memory */
1170 static HRESULT unmarshal_ORPC_EXTENT_ARRAY(RPC_MESSAGE *msg, const char *end,
1171 ORPC_EXTENT_ARRAY *extensions,
1172 WIRE_ORPC_EXTENT **first_wire_orpc_extent)
1174 DWORD pointer_id;
1175 DWORD i;
1177 memcpy(extensions, msg->Buffer, FIELD_OFFSET(WIRE_ORPC_EXTENT_ARRAY, extent));
1178 msg->Buffer = (char *)msg->Buffer + FIELD_OFFSET(WIRE_ORPC_EXTENT_ARRAY, extent);
1180 if ((const char *)msg->Buffer + 2 * sizeof(DWORD) > end)
1181 return RPC_E_INVALID_HEADER;
1183 pointer_id = *(DWORD *)msg->Buffer;
1184 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
1185 extensions->extent = NULL;
1187 if (pointer_id)
1189 WIRE_ORPC_EXTENT *wire_orpc_extent;
1191 /* conformance */
1192 if (*(DWORD *)msg->Buffer != ((extensions->size+1)&~1))
1193 return RPC_S_INVALID_BOUND;
1195 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
1197 /* arbitrary limit for security (don't know what native does) */
1198 if (extensions->size > 256)
1200 ERR("too many extensions: %d\n", extensions->size);
1201 return RPC_S_INVALID_BOUND;
1204 *first_wire_orpc_extent = wire_orpc_extent = msg->Buffer;
1205 for (i = 0; i < ((extensions->size+1)&~1); i++)
1207 if ((const char *)&wire_orpc_extent->data[0] > end)
1208 return RPC_S_INVALID_BOUND;
1209 if (wire_orpc_extent->conformance != ((wire_orpc_extent->size+7)&~7))
1210 return RPC_S_INVALID_BOUND;
1211 if ((const char *)&wire_orpc_extent->data[wire_orpc_extent->conformance] > end)
1212 return RPC_S_INVALID_BOUND;
1213 TRACE("size %u, guid %s\n", wire_orpc_extent->size, debugstr_guid(&wire_orpc_extent->id));
1214 wire_orpc_extent = (WIRE_ORPC_EXTENT *)&wire_orpc_extent->data[wire_orpc_extent->conformance];
1216 msg->Buffer = wire_orpc_extent;
1219 return S_OK;
1222 /* unmarshals ORPCTHIS according to NDR rules, but doesn't allocate any memory */
1223 static HRESULT unmarshal_ORPCTHIS(RPC_MESSAGE *msg, ORPCTHIS *orpcthis,
1224 ORPC_EXTENT_ARRAY *orpc_ext_array, WIRE_ORPC_EXTENT **first_wire_orpc_extent)
1226 const char *end = (char *)msg->Buffer + msg->BufferLength;
1228 *first_wire_orpc_extent = NULL;
1230 if (msg->BufferLength < FIELD_OFFSET(WIRE_ORPCTHIS, extensions) + sizeof(DWORD))
1232 ERR("invalid buffer length\n");
1233 return RPC_E_INVALID_HEADER;
1236 memcpy(orpcthis, msg->Buffer, FIELD_OFFSET(WIRE_ORPCTHIS, extensions));
1237 msg->Buffer = (char *)msg->Buffer + FIELD_OFFSET(WIRE_ORPCTHIS, extensions);
1239 if ((const char *)msg->Buffer + sizeof(DWORD) > end)
1240 return RPC_E_INVALID_HEADER;
1242 if (*(DWORD *)msg->Buffer)
1243 orpcthis->extensions = orpc_ext_array;
1244 else
1245 orpcthis->extensions = NULL;
1247 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
1249 if (orpcthis->extensions)
1251 HRESULT hr = unmarshal_ORPC_EXTENT_ARRAY(msg, end, orpc_ext_array,
1252 first_wire_orpc_extent);
1253 if (FAILED(hr))
1254 return hr;
1257 if ((orpcthis->version.MajorVersion != COM_MAJOR_VERSION) ||
1258 (orpcthis->version.MinorVersion > COM_MINOR_VERSION))
1260 ERR("COM version {%d, %d} not supported\n",
1261 orpcthis->version.MajorVersion, orpcthis->version.MinorVersion);
1262 return RPC_E_VERSION_MISMATCH;
1265 if (orpcthis->flags & ~(ORPCF_LOCAL|ORPCF_RESERVED1|ORPCF_RESERVED2|ORPCF_RESERVED3|ORPCF_RESERVED4))
1267 ERR("invalid flags 0x%x\n", orpcthis->flags & ~(ORPCF_LOCAL|ORPCF_RESERVED1|ORPCF_RESERVED2|ORPCF_RESERVED3|ORPCF_RESERVED4));
1268 return RPC_E_INVALID_HEADER;
1271 return S_OK;
1274 static HRESULT unmarshal_ORPCTHAT(RPC_MESSAGE *msg, ORPCTHAT *orpcthat,
1275 ORPC_EXTENT_ARRAY *orpc_ext_array, WIRE_ORPC_EXTENT **first_wire_orpc_extent)
1277 const char *end = (char *)msg->Buffer + msg->BufferLength;
1279 *first_wire_orpc_extent = NULL;
1281 if (msg->BufferLength < FIELD_OFFSET(WIRE_ORPCTHAT, extensions) + sizeof(DWORD))
1283 ERR("invalid buffer length\n");
1284 return RPC_E_INVALID_HEADER;
1287 memcpy(orpcthat, msg->Buffer, FIELD_OFFSET(WIRE_ORPCTHAT, extensions));
1288 msg->Buffer = (char *)msg->Buffer + FIELD_OFFSET(WIRE_ORPCTHAT, extensions);
1290 if ((const char *)msg->Buffer + sizeof(DWORD) > end)
1291 return RPC_E_INVALID_HEADER;
1293 if (*(DWORD *)msg->Buffer)
1294 orpcthat->extensions = orpc_ext_array;
1295 else
1296 orpcthat->extensions = NULL;
1298 msg->Buffer = (char *)msg->Buffer + sizeof(DWORD);
1300 if (orpcthat->extensions)
1302 HRESULT hr = unmarshal_ORPC_EXTENT_ARRAY(msg, end, orpc_ext_array,
1303 first_wire_orpc_extent);
1304 if (FAILED(hr))
1305 return hr;
1308 if (orpcthat->flags & ~(ORPCF_LOCAL|ORPCF_RESERVED1|ORPCF_RESERVED2|ORPCF_RESERVED3|ORPCF_RESERVED4))
1310 ERR("invalid flags 0x%x\n", orpcthat->flags & ~(ORPCF_LOCAL|ORPCF_RESERVED1|ORPCF_RESERVED2|ORPCF_RESERVED3|ORPCF_RESERVED4));
1311 return RPC_E_INVALID_HEADER;
1314 return S_OK;
1317 void RPC_ExecuteCall(struct dispatch_params *params)
1319 struct message_state *message_state = NULL;
1320 RPC_MESSAGE *msg = (RPC_MESSAGE *)params->msg;
1321 char *original_buffer = msg->Buffer;
1322 ORPCTHIS orpcthis;
1323 ORPC_EXTENT_ARRAY orpc_ext_array;
1324 WIRE_ORPC_EXTENT *first_wire_orpc_extent;
1325 GUID old_causality_id;
1327 /* handle ORPCTHIS and server extensions */
1329 params->hr = unmarshal_ORPCTHIS(msg, &orpcthis, &orpc_ext_array, &first_wire_orpc_extent);
1330 if (params->hr != S_OK)
1332 msg->Buffer = original_buffer;
1333 goto exit;
1336 message_state = HeapAlloc(GetProcessHeap(), 0, sizeof(*message_state));
1337 if (!message_state)
1339 params->hr = E_OUTOFMEMORY;
1340 msg->Buffer = original_buffer;
1341 goto exit;
1344 message_state->prefix_data_len = (char *)msg->Buffer - original_buffer;
1345 message_state->binding_handle = msg->Handle;
1346 message_state->bypass_rpcrt = params->bypass_rpcrt;
1348 message_state->channel_hook_info.iid = params->iid;
1349 message_state->channel_hook_info.cbSize = sizeof(message_state->channel_hook_info);
1350 message_state->channel_hook_info.uCausality = orpcthis.cid;
1351 message_state->channel_hook_info.dwServerPid = GetCurrentProcessId();
1352 message_state->channel_hook_info.iMethod = msg->ProcNum;
1353 message_state->channel_hook_info.pObject = params->iface;
1355 if (orpcthis.extensions && first_wire_orpc_extent &&
1356 orpcthis.extensions->size)
1357 ChannelHooks_ServerNotify(&message_state->channel_hook_info, msg->DataRepresentation, first_wire_orpc_extent, orpcthis.extensions->size);
1359 msg->Handle = message_state;
1360 msg->BufferLength -= message_state->prefix_data_len;
1362 /* call message filter */
1364 if (COM_CurrentApt()->filter)
1366 DWORD handlecall;
1367 INTERFACEINFO interface_info;
1368 CALLTYPE calltype;
1370 interface_info.pUnk = params->iface;
1371 interface_info.iid = params->iid;
1372 interface_info.wMethod = msg->ProcNum;
1374 if (IsEqualGUID(&orpcthis.cid, &COM_CurrentInfo()->causality_id))
1375 calltype = CALLTYPE_NESTED;
1376 else if (COM_CurrentInfo()->pending_call_count_server == 0)
1377 calltype = CALLTYPE_TOPLEVEL;
1378 else
1379 calltype = CALLTYPE_TOPLEVEL_CALLPENDING;
1381 handlecall = IMessageFilter_HandleInComingCall(COM_CurrentApt()->filter,
1382 calltype,
1383 UlongToHandle(GetCurrentProcessId()),
1384 0 /* FIXME */,
1385 &interface_info);
1386 TRACE("IMessageFilter_HandleInComingCall returned %d\n", handlecall);
1387 switch (handlecall)
1389 case SERVERCALL_REJECTED:
1390 params->hr = RPC_E_CALL_REJECTED;
1391 goto exit_reset_state;
1392 case SERVERCALL_RETRYLATER:
1393 #if 0 /* FIXME: handle retries on the client side before enabling this code */
1394 params->hr = RPC_E_RETRY;
1395 goto exit_reset_state;
1396 #else
1397 FIXME("retry call later not implemented\n");
1398 break;
1399 #endif
1400 case SERVERCALL_ISHANDLED:
1401 default:
1402 break;
1406 /* invoke the method */
1408 /* save the old causality ID - note: any calls executed while processing
1409 * messages received during the SendReceive will appear to originate from
1410 * this call - this should be checked with what Windows does */
1411 old_causality_id = COM_CurrentInfo()->causality_id;
1412 COM_CurrentInfo()->causality_id = orpcthis.cid;
1413 COM_CurrentInfo()->pending_call_count_server++;
1414 params->hr = IRpcStubBuffer_Invoke(params->stub, params->msg, params->chan);
1415 COM_CurrentInfo()->pending_call_count_server--;
1416 COM_CurrentInfo()->causality_id = old_causality_id;
1418 /* the invoke allocated a new buffer, so free the old one */
1419 if (message_state->bypass_rpcrt && original_buffer != msg->Buffer)
1420 HeapFree(GetProcessHeap(), 0, original_buffer);
1422 exit_reset_state:
1423 message_state = msg->Handle;
1424 msg->Handle = message_state->binding_handle;
1425 msg->Buffer = (char *)msg->Buffer - message_state->prefix_data_len;
1426 msg->BufferLength += message_state->prefix_data_len;
1428 exit:
1429 HeapFree(GetProcessHeap(), 0, message_state);
1430 if (params->handle) SetEvent(params->handle);
1433 static void __RPC_STUB dispatch_rpc(RPC_MESSAGE *msg)
1435 struct dispatch_params *params;
1436 APARTMENT *apt;
1437 IPID ipid;
1438 HRESULT hr;
1440 RpcBindingInqObject(msg->Handle, &ipid);
1442 TRACE("ipid = %s, iMethod = %d\n", debugstr_guid(&ipid), msg->ProcNum);
1444 params = HeapAlloc(GetProcessHeap(), 0, sizeof(*params));
1445 if (!params)
1447 RpcRaiseException(E_OUTOFMEMORY);
1448 return;
1451 hr = ipid_get_dispatch_params(&ipid, &apt, &params->stub, &params->chan,
1452 &params->iid, &params->iface);
1453 if (hr != S_OK)
1455 ERR("no apartment found for ipid %s\n", debugstr_guid(&ipid));
1456 HeapFree(GetProcessHeap(), 0, params);
1457 RpcRaiseException(hr);
1458 return;
1461 params->msg = (RPCOLEMESSAGE *)msg;
1462 params->status = RPC_S_OK;
1463 params->hr = S_OK;
1464 params->handle = NULL;
1465 params->bypass_rpcrt = FALSE;
1467 /* Note: this is the important difference between STAs and MTAs - we
1468 * always execute RPCs to STAs in the thread that originally created the
1469 * apartment (i.e. the one that pumps messages to the window) */
1470 if (!apt->multi_threaded)
1472 params->handle = CreateEventW(NULL, FALSE, FALSE, NULL);
1474 TRACE("Calling apartment thread 0x%08x...\n", apt->tid);
1476 if (PostMessageW(apartment_getwindow(apt), DM_EXECUTERPC, 0, (LPARAM)params))
1477 WaitForSingleObject(params->handle, INFINITE);
1478 else
1480 ERR("PostMessage failed with error %u\n", GetLastError());
1481 IRpcChannelBuffer_Release(params->chan);
1482 IRpcStubBuffer_Release(params->stub);
1484 CloseHandle(params->handle);
1486 else
1488 BOOL joined = FALSE;
1489 if (!COM_CurrentInfo()->apt)
1491 apartment_joinmta();
1492 joined = TRUE;
1494 RPC_ExecuteCall(params);
1495 if (joined)
1497 apartment_release(COM_CurrentInfo()->apt);
1498 COM_CurrentInfo()->apt = NULL;
1502 hr = params->hr;
1503 if (params->chan)
1504 IRpcChannelBuffer_Release(params->chan);
1505 if (params->stub)
1506 IRpcStubBuffer_Release(params->stub);
1507 HeapFree(GetProcessHeap(), 0, params);
1509 apartment_release(apt);
1511 /* if IRpcStubBuffer_Invoke fails, we should raise an exception to tell
1512 * the RPC runtime that the call failed */
1513 if (hr) RpcRaiseException(hr);
1516 /* stub registration */
1517 HRESULT RPC_RegisterInterface(REFIID riid)
1519 struct registered_if *rif;
1520 BOOL found = FALSE;
1521 HRESULT hr = S_OK;
1523 TRACE("(%s)\n", debugstr_guid(riid));
1525 EnterCriticalSection(&csRegIf);
1526 LIST_FOR_EACH_ENTRY(rif, &registered_interfaces, struct registered_if, entry)
1528 if (IsEqualGUID(&rif->If.InterfaceId.SyntaxGUID, riid))
1530 rif->refs++;
1531 found = TRUE;
1532 break;
1535 if (!found)
1537 TRACE("Creating new interface\n");
1539 rif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rif));
1540 if (rif)
1542 RPC_STATUS status;
1544 rif->refs = 1;
1545 rif->If.Length = sizeof(RPC_SERVER_INTERFACE);
1546 /* RPC interface ID = COM interface ID */
1547 rif->If.InterfaceId.SyntaxGUID = *riid;
1548 rif->If.DispatchTable = &rpc_dispatch;
1549 /* all other fields are 0, including the version asCOM objects
1550 * always have a version of 0.0 */
1551 status = RpcServerRegisterIfEx(
1552 (RPC_IF_HANDLE)&rif->If,
1553 NULL, NULL,
1554 RPC_IF_OLE | RPC_IF_AUTOLISTEN,
1555 RPC_C_LISTEN_MAX_CALLS_DEFAULT,
1556 NULL);
1557 if (status == RPC_S_OK)
1558 list_add_tail(&registered_interfaces, &rif->entry);
1559 else
1561 ERR("RpcServerRegisterIfEx failed with error %d\n", status);
1562 HeapFree(GetProcessHeap(), 0, rif);
1563 hr = HRESULT_FROM_WIN32(status);
1566 else
1567 hr = E_OUTOFMEMORY;
1569 LeaveCriticalSection(&csRegIf);
1570 return hr;
1573 /* stub unregistration */
1574 void RPC_UnregisterInterface(REFIID riid)
1576 struct registered_if *rif;
1577 EnterCriticalSection(&csRegIf);
1578 LIST_FOR_EACH_ENTRY(rif, &registered_interfaces, struct registered_if, entry)
1580 if (IsEqualGUID(&rif->If.InterfaceId.SyntaxGUID, riid))
1582 if (!--rif->refs)
1584 RpcServerUnregisterIf((RPC_IF_HANDLE)&rif->If, NULL, TRUE);
1585 list_remove(&rif->entry);
1586 HeapFree(GetProcessHeap(), 0, rif);
1588 break;
1591 LeaveCriticalSection(&csRegIf);
1594 /* get the info for an OXID, including the IPID for the rem unknown interface
1595 * and the string binding */
1596 HRESULT RPC_ResolveOxid(OXID oxid, OXID_INFO *oxid_info)
1598 TRACE("%s\n", wine_dbgstr_longlong(oxid));
1600 oxid_info->dwTid = 0;
1601 oxid_info->dwPid = 0;
1602 oxid_info->dwAuthnHint = RPC_C_AUTHN_LEVEL_NONE;
1603 /* FIXME: this is a hack around not having an OXID resolver yet -
1604 * this function should contact the machine's OXID resolver and then it
1605 * should give us the IPID of the IRemUnknown interface */
1606 oxid_info->ipidRemUnknown.Data1 = 0xffffffff;
1607 oxid_info->ipidRemUnknown.Data2 = 0xffff;
1608 oxid_info->ipidRemUnknown.Data3 = 0xffff;
1609 memcpy(oxid_info->ipidRemUnknown.Data4, &oxid, sizeof(OXID));
1610 oxid_info->psa = NULL /* FIXME */;
1612 return S_OK;
1615 /* make the apartment reachable by other threads and processes and create the
1616 * IRemUnknown object */
1617 void RPC_StartRemoting(struct apartment *apt)
1619 if (!InterlockedExchange(&apt->remoting_started, TRUE))
1621 WCHAR endpoint[200];
1622 RPC_STATUS status;
1624 get_rpc_endpoint(endpoint, &apt->oxid);
1626 status = RpcServerUseProtseqEpW(
1627 wszRpcTransport,
1628 RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
1629 endpoint,
1630 NULL);
1631 if (status != RPC_S_OK)
1632 ERR("Couldn't register endpoint %s\n", debugstr_w(endpoint));
1634 /* FIXME: move remote unknown exporting into this function */
1636 start_apartment_remote_unknown();
1640 static HRESULT create_server(REFCLSID rclsid)
1642 static const WCHAR wszLocalServer32[] = { 'L','o','c','a','l','S','e','r','v','e','r','3','2',0 };
1643 static const WCHAR embedding[] = { ' ', '-','E','m','b','e','d','d','i','n','g',0 };
1644 HKEY key;
1645 HRESULT hres;
1646 WCHAR command[MAX_PATH+sizeof(embedding)/sizeof(WCHAR)];
1647 DWORD size = (MAX_PATH+1) * sizeof(WCHAR);
1648 STARTUPINFOW sinfo;
1649 PROCESS_INFORMATION pinfo;
1651 hres = COM_OpenKeyForCLSID(rclsid, wszLocalServer32, KEY_READ, &key);
1652 if (FAILED(hres)) {
1653 ERR("class %s not registered\n", debugstr_guid(rclsid));
1654 return hres;
1657 hres = RegQueryValueExW(key, NULL, NULL, NULL, (LPBYTE)command, &size);
1658 RegCloseKey(key);
1659 if (hres) {
1660 WARN("No default value for LocalServer32 key\n");
1661 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
1664 memset(&sinfo,0,sizeof(sinfo));
1665 sinfo.cb = sizeof(sinfo);
1667 /* EXE servers are started with the -Embedding switch. */
1669 strcatW(command, embedding);
1671 TRACE("activating local server %s for %s\n", debugstr_w(command), debugstr_guid(rclsid));
1673 /* FIXME: Win2003 supports a ServerExecutable value that is passed into
1674 * CreateProcess */
1675 if (!CreateProcessW(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &sinfo, &pinfo)) {
1676 WARN("failed to run local server %s\n", debugstr_w(command));
1677 return HRESULT_FROM_WIN32(GetLastError());
1679 CloseHandle(pinfo.hProcess);
1680 CloseHandle(pinfo.hThread);
1682 return S_OK;
1686 * start_local_service() - start a service given its name and parameters
1688 static DWORD start_local_service(LPCWSTR name, DWORD num, LPCWSTR *params)
1690 SC_HANDLE handle, hsvc;
1691 DWORD r = ERROR_FUNCTION_FAILED;
1693 TRACE("Starting service %s %d params\n", debugstr_w(name), num);
1695 handle = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
1696 if (!handle)
1697 return r;
1698 hsvc = OpenServiceW(handle, name, SERVICE_START);
1699 if (hsvc)
1701 if(StartServiceW(hsvc, num, params))
1702 r = ERROR_SUCCESS;
1703 else
1704 r = GetLastError();
1705 if (r == ERROR_SERVICE_ALREADY_RUNNING)
1706 r = ERROR_SUCCESS;
1707 CloseServiceHandle(hsvc);
1709 else
1710 r = GetLastError();
1711 CloseServiceHandle(handle);
1713 TRACE("StartService returned error %u (%s)\n", r, (r == ERROR_SUCCESS) ? "ok":"failed");
1715 return r;
1719 * create_local_service() - start a COM server in a service
1721 * To start a Local Service, we read the AppID value under
1722 * the class's CLSID key, then open the HKCR\\AppId key specified
1723 * there and check for a LocalService value.
1725 * Note: Local Services are not supported under Windows 9x
1727 static HRESULT create_local_service(REFCLSID rclsid)
1729 HRESULT hres;
1730 WCHAR buf[CHARS_IN_GUID];
1731 static const WCHAR szLocalService[] = { 'L','o','c','a','l','S','e','r','v','i','c','e',0 };
1732 static const WCHAR szServiceParams[] = {'S','e','r','v','i','c','e','P','a','r','a','m','s',0};
1733 HKEY hkey;
1734 LONG r;
1735 DWORD type, sz;
1737 TRACE("Attempting to start Local service for %s\n", debugstr_guid(rclsid));
1739 hres = COM_OpenKeyForAppIdFromCLSID(rclsid, KEY_READ, &hkey);
1740 if (FAILED(hres))
1741 return hres;
1743 /* read the LocalService and ServiceParameters values from the AppID key */
1744 sz = sizeof buf;
1745 r = RegQueryValueExW(hkey, szLocalService, NULL, &type, (LPBYTE)buf, &sz);
1746 if (r==ERROR_SUCCESS && type==REG_SZ)
1748 DWORD num_args = 0;
1749 LPWSTR args[1] = { NULL };
1752 * FIXME: I'm not really sure how to deal with the service parameters.
1753 * I suspect that the string returned from RegQueryValueExW
1754 * should be split into a number of arguments by spaces.
1755 * It would make more sense if ServiceParams contained a
1756 * REG_MULTI_SZ here, but it's a REG_SZ for the services
1757 * that I'm interested in for the moment.
1759 r = RegQueryValueExW(hkey, szServiceParams, NULL, &type, NULL, &sz);
1760 if (r == ERROR_SUCCESS && type == REG_SZ && sz)
1762 args[0] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sz);
1763 num_args++;
1764 RegQueryValueExW(hkey, szServiceParams, NULL, &type, (LPBYTE)args[0], &sz);
1766 r = start_local_service(buf, num_args, (LPCWSTR *)args);
1767 if (r != ERROR_SUCCESS)
1768 hres = REGDB_E_CLASSNOTREG; /* FIXME: check retval */
1769 HeapFree(GetProcessHeap(),0,args[0]);
1771 else
1773 WARN("No LocalService value\n");
1774 hres = REGDB_E_CLASSNOTREG; /* FIXME: check retval */
1776 RegCloseKey(hkey);
1778 return hres;
1782 static void get_localserver_pipe_name(WCHAR *pipefn, REFCLSID rclsid)
1784 static const WCHAR wszPipeRef[] = {'\\','\\','.','\\','p','i','p','e','\\',0};
1785 strcpyW(pipefn, wszPipeRef);
1786 StringFromGUID2(rclsid, pipefn + sizeof(wszPipeRef)/sizeof(wszPipeRef[0]) - 1, CHARS_IN_GUID);
1789 /* FIXME: should call to rpcss instead */
1790 HRESULT RPC_GetLocalClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
1792 HRESULT hres;
1793 HANDLE hPipe;
1794 WCHAR pipefn[100];
1795 DWORD res, bufferlen;
1796 char marshalbuffer[200];
1797 IStream *pStm;
1798 LARGE_INTEGER seekto;
1799 ULARGE_INTEGER newpos;
1800 int tries = 0;
1802 static const int MAXTRIES = 30; /* 30 seconds */
1804 TRACE("rclsid=%s, iid=%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
1806 get_localserver_pipe_name(pipefn, rclsid);
1808 while (tries++ < MAXTRIES) {
1809 TRACE("waiting for %s\n", debugstr_w(pipefn));
1811 WaitNamedPipeW( pipefn, NMPWAIT_WAIT_FOREVER );
1812 hPipe = CreateFileW(pipefn, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1813 if (hPipe == INVALID_HANDLE_VALUE) {
1814 DWORD index;
1815 DWORD start_ticks;
1816 if (tries == 1) {
1817 if ( (hres = create_local_service(rclsid)) &&
1818 (hres = create_server(rclsid)) )
1819 return hres;
1820 } else {
1821 WARN("Connecting to %s, no response yet, retrying: le is %u\n", debugstr_w(pipefn), GetLastError());
1823 /* wait for one second, even if messages arrive */
1824 start_ticks = GetTickCount();
1825 do {
1826 CoWaitForMultipleHandles(0, 1000, 0, NULL, &index);
1827 } while (GetTickCount() - start_ticks < 1000);
1828 continue;
1830 bufferlen = 0;
1831 if (!ReadFile(hPipe,marshalbuffer,sizeof(marshalbuffer),&bufferlen,NULL)) {
1832 FIXME("Failed to read marshal id from classfactory of %s.\n",debugstr_guid(rclsid));
1833 Sleep(1000);
1834 continue;
1836 TRACE("read marshal id from pipe\n");
1837 CloseHandle(hPipe);
1838 break;
1841 if (tries >= MAXTRIES)
1842 return E_NOINTERFACE;
1844 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
1845 if (hres) return hres;
1846 hres = IStream_Write(pStm,marshalbuffer,bufferlen,&res);
1847 if (hres) goto out;
1848 seekto.u.LowPart = 0;seekto.u.HighPart = 0;
1849 hres = IStream_Seek(pStm,seekto,STREAM_SEEK_SET,&newpos);
1851 TRACE("unmarshalling classfactory\n");
1852 hres = CoUnmarshalInterface(pStm,&IID_IClassFactory,ppv);
1853 out:
1854 IStream_Release(pStm);
1855 return hres;
1859 struct local_server_params
1861 CLSID clsid;
1862 IStream *stream;
1863 HANDLE ready_event;
1864 HANDLE stop_event;
1865 HANDLE thread;
1866 BOOL multi_use;
1869 /* FIXME: should call to rpcss instead */
1870 static DWORD WINAPI local_server_thread(LPVOID param)
1872 struct local_server_params * lsp = param;
1873 HANDLE hPipe;
1874 WCHAR pipefn[100];
1875 HRESULT hres;
1876 IStream *pStm = lsp->stream;
1877 STATSTG ststg;
1878 unsigned char *buffer;
1879 int buflen;
1880 LARGE_INTEGER seekto;
1881 ULARGE_INTEGER newpos;
1882 ULONG res;
1883 BOOL multi_use = lsp->multi_use;
1884 OVERLAPPED ovl;
1885 HANDLE pipe_event;
1886 DWORD bytes;
1888 TRACE("Starting threader for %s.\n",debugstr_guid(&lsp->clsid));
1890 memset(&ovl, 0, sizeof(ovl));
1891 get_localserver_pipe_name(pipefn, &lsp->clsid);
1893 hPipe = CreateNamedPipeW( pipefn, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1894 PIPE_TYPE_BYTE|PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,
1895 4096, 4096, 500 /* 0.5 second timeout */, NULL );
1897 SetEvent(lsp->ready_event);
1899 if (hPipe == INVALID_HANDLE_VALUE)
1901 FIXME("pipe creation failed for %s, le is %u\n", debugstr_w(pipefn), GetLastError());
1902 return 1;
1905 ovl.hEvent = pipe_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1907 while (1) {
1908 if (!ConnectNamedPipe(hPipe, &ovl))
1910 DWORD error = GetLastError();
1911 if (error == ERROR_IO_PENDING)
1913 HANDLE handles[2] = { pipe_event, lsp->stop_event };
1914 DWORD ret;
1915 ret = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1916 if (ret != WAIT_OBJECT_0)
1917 break;
1919 /* client already connected isn't an error */
1920 else if (error != ERROR_PIPE_CONNECTED)
1922 ERR("ConnectNamedPipe failed with error %d\n", GetLastError());
1923 break;
1927 TRACE("marshalling IClassFactory to client\n");
1929 hres = IStream_Stat(pStm,&ststg,STATFLAG_NONAME);
1930 if (hres) return hres;
1932 seekto.u.LowPart = 0;
1933 seekto.u.HighPart = 0;
1934 hres = IStream_Seek(pStm,seekto,STREAM_SEEK_SET,&newpos);
1935 if (hres) {
1936 FIXME("IStream_Seek failed, %x\n",hres);
1937 CloseHandle(hPipe);
1938 CloseHandle(pipe_event);
1939 return hres;
1942 buflen = ststg.cbSize.u.LowPart;
1943 buffer = HeapAlloc(GetProcessHeap(),0,buflen);
1945 hres = IStream_Read(pStm,buffer,buflen,&res);
1946 if (hres) {
1947 FIXME("Stream Read failed, %x\n",hres);
1948 CloseHandle(hPipe);
1949 CloseHandle(pipe_event);
1950 HeapFree(GetProcessHeap(),0,buffer);
1951 return hres;
1954 WriteFile(hPipe,buffer,buflen,&res,&ovl);
1955 GetOverlappedResult(hPipe, &ovl, &bytes, TRUE);
1956 HeapFree(GetProcessHeap(),0,buffer);
1958 FlushFileBuffers(hPipe);
1959 DisconnectNamedPipe(hPipe);
1961 TRACE("done marshalling IClassFactory\n");
1963 if (!multi_use)
1965 TRACE("single use object, shutting down pipe %s\n", debugstr_w(pipefn));
1966 break;
1969 CloseHandle(hPipe);
1970 CloseHandle(pipe_event);
1971 return 0;
1974 /* starts listening for a local server */
1975 HRESULT RPC_StartLocalServer(REFCLSID clsid, IStream *stream, BOOL multi_use, void **registration)
1977 DWORD tid;
1978 struct local_server_params *lsp;
1980 lsp = HeapAlloc(GetProcessHeap(), 0, sizeof(*lsp));
1981 if (!lsp)
1982 return E_OUTOFMEMORY;
1984 lsp->clsid = *clsid;
1985 lsp->stream = stream;
1986 IStream_AddRef(stream);
1987 lsp->ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1988 if (!lsp->ready_event)
1990 HeapFree(GetProcessHeap(), 0, lsp);
1991 return HRESULT_FROM_WIN32(GetLastError());
1993 lsp->stop_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1994 if (!lsp->stop_event)
1996 CloseHandle(lsp->ready_event);
1997 HeapFree(GetProcessHeap(), 0, lsp);
1998 return HRESULT_FROM_WIN32(GetLastError());
2000 lsp->multi_use = multi_use;
2002 lsp->thread = CreateThread(NULL, 0, local_server_thread, lsp, 0, &tid);
2003 if (!lsp->thread)
2005 CloseHandle(lsp->ready_event);
2006 CloseHandle(lsp->stop_event);
2007 HeapFree(GetProcessHeap(), 0, lsp);
2008 return HRESULT_FROM_WIN32(GetLastError());
2011 WaitForSingleObject(lsp->ready_event, INFINITE);
2012 CloseHandle(lsp->ready_event);
2013 lsp->ready_event = NULL;
2015 *registration = lsp;
2016 return S_OK;
2019 /* stops listening for a local server */
2020 void RPC_StopLocalServer(void *registration)
2022 struct local_server_params *lsp = registration;
2024 /* signal local_server_thread to stop */
2025 SetEvent(lsp->stop_event);
2026 /* wait for it to exit */
2027 WaitForSingleObject(lsp->thread, INFINITE);
2029 IStream_Release(lsp->stream);
2030 CloseHandle(lsp->stop_event);
2031 CloseHandle(lsp->thread);
2032 HeapFree(GetProcessHeap(), 0, lsp);