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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
46 #include "wine/unicode.h"
47 #include "wine/exception.h"
49 #include "compobj_private.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 static void __RPC_STUB dispatch_rpc(RPC_MESSAGE *msg);
57 /* we only use one function to dispatch calls for all methods - we use the
58 * RPC_IF_OLE flag to tell the RPC runtime that this is the case */
59 static RPC_DISPATCH_FUNCTION rpc_dispatch_table[1] = { dispatch_rpc }; /* (RO) */
60 static RPC_DISPATCH_TABLE rpc_dispatch = { 1, rpc_dispatch_table }; /* (RO) */
62 static struct list registered_interfaces = LIST_INIT(registered_interfaces); /* (CS csRegIf) */
63 static CRITICAL_SECTION csRegIf;
64 static CRITICAL_SECTION_DEBUG csRegIf_debug =
67 { &csRegIf_debug.ProcessLocksList, &csRegIf_debug.ProcessLocksList },
68 0, 0, { (DWORD_PTR)(__FILE__ ": dcom registered server interfaces") }
70 static CRITICAL_SECTION csRegIf = { &csRegIf_debug, -1, 0, 0, 0, 0 };
72 static WCHAR wszPipeTransport[] = {'n','c','a','c','n','_','n','p',0};
78 DWORD refs; /* ref count */
79 RPC_SERVER_INTERFACE If; /* interface registered with the RPC runtime */
82 /* get the pipe endpoint specified of the specified apartment */
83 static inline void get_rpc_endpoint(LPWSTR endpoint, const OXID *oxid)
85 /* FIXME: should get endpoint from rpcss */
86 static const WCHAR wszEndpointFormat[] = {'\\','p','i','p','e','\\','O','L','E','_','%','0','8','l','x','%','0','8','l','x',0};
87 wsprintfW(endpoint, wszEndpointFormat, (DWORD)(*oxid >> 32),(DWORD)*oxid);
92 const IRpcChannelBufferVtbl *lpVtbl;
98 RpcChannelBuffer super; /* superclass */
100 RPC_BINDING_HANDLE bind; /* handle to the remote server */
101 } ClientRpcChannelBuffer;
103 struct dispatch_params
105 RPCOLEMESSAGE *msg; /* message */
106 IRpcStubBuffer *stub; /* stub buffer, if applicable */
107 IRpcChannelBuffer *chan; /* server channel buffer, if applicable */
108 HANDLE handle; /* handle that will become signaled when call finishes */
109 RPC_STATUS status; /* status (out) */
110 HRESULT hr; /* hresult (out) */
113 static WINE_EXCEPTION_FILTER(ole_filter)
115 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
116 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
117 return EXCEPTION_CONTINUE_SEARCH;
118 return EXCEPTION_EXECUTE_HANDLER;
121 static HRESULT WINAPI RpcChannelBuffer_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
124 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
126 *ppv = (LPVOID)iface;
127 IUnknown_AddRef(iface);
130 return E_NOINTERFACE;
133 static ULONG WINAPI RpcChannelBuffer_AddRef(LPRPCCHANNELBUFFER iface)
135 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
136 return InterlockedIncrement(&This->refs);
139 static ULONG WINAPI ServerRpcChannelBuffer_Release(LPRPCCHANNELBUFFER iface)
141 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
144 ref = InterlockedDecrement(&This->refs);
148 HeapFree(GetProcessHeap(), 0, This);
152 static ULONG WINAPI ClientRpcChannelBuffer_Release(LPRPCCHANNELBUFFER iface)
154 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
157 ref = InterlockedDecrement(&This->super.refs);
161 RpcBindingFree(&This->bind);
162 HeapFree(GetProcessHeap(), 0, This);
166 static HRESULT WINAPI ServerRpcChannelBuffer_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
168 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
169 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
172 TRACE("(%p)->(%p,%s)\n", This, olemsg, debugstr_guid(riid));
174 status = I_RpcGetBuffer(msg);
176 TRACE("-- %ld\n", status);
178 return HRESULT_FROM_WIN32(status);
181 static HRESULT WINAPI ClientRpcChannelBuffer_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
183 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
184 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
185 RPC_CLIENT_INTERFACE *cif;
188 TRACE("(%p)->(%p,%s)\n", This, olemsg, debugstr_guid(riid));
190 cif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_CLIENT_INTERFACE));
192 return E_OUTOFMEMORY;
194 cif->Length = sizeof(RPC_CLIENT_INTERFACE);
195 /* RPC interface ID = COM interface ID */
196 cif->InterfaceId.SyntaxGUID = *riid;
197 /* COM objects always have a version of 0.0 */
198 cif->InterfaceId.SyntaxVersion.MajorVersion = 0;
199 cif->InterfaceId.SyntaxVersion.MinorVersion = 0;
200 msg->RpcInterfaceInformation = cif;
201 msg->Handle = This->bind;
203 status = I_RpcGetBuffer(msg);
205 TRACE("-- %ld\n", status);
207 return HRESULT_FROM_WIN32(status);
210 /* this thread runs an outgoing RPC */
211 static DWORD WINAPI rpc_sendreceive_thread(LPVOID param)
213 struct dispatch_params *data = (struct dispatch_params *) param;
215 /* FIXME: trap and rethrow RPC exceptions in app thread */
216 data->status = I_RpcSendReceive((RPC_MESSAGE *)data->msg);
218 TRACE("completed with status 0x%lx\n", data->status);
223 static HRESULT WINAPI RpcChannelBuffer_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
226 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
229 struct dispatch_params *params;
231 IRpcStubBuffer *stub;
235 TRACE("(%p) iMethod=%ld\n", olemsg, olemsg->iMethod);
237 params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*params));
238 if (!params) return E_OUTOFMEMORY;
240 params->msg = olemsg;
241 params->status = RPC_S_OK;
244 /* Note: this is an optimization in the Microsoft OLE runtime that we need
245 * to copy, as shown by the test_no_couninitialize_client test. without
246 * short-circuiting the RPC runtime in the case below, the test will
247 * deadlock on the loader lock due to the RPC runtime needing to create
248 * a thread to process the RPC when this function is called indirectly
251 RpcBindingInqObject(msg->Handle, &ipid);
252 stub = ipid_to_apt_and_stubbuffer(&ipid, &apt);
253 if (apt && (apt->model & COINIT_APARTMENTTHREADED))
256 params->chan = NULL; /* FIXME: pass server channel */
257 params->handle = CreateEventW(NULL, FALSE, FALSE, NULL);
259 TRACE("Calling apartment thread 0x%08lx...\n", apt->tid);
261 PostMessageW(apt->win, DM_EXECUTERPC, 0, (LPARAM)params);
265 if (stub) IRpcStubBuffer_Release(stub);
267 /* we use a separate thread here because we need to be able to
268 * pump the message loop in the application thread: if we do not,
269 * any windows created by this thread will hang and RPCs that try
270 * and re-enter this STA from an incoming server thread will
271 * deadlock. InstallShield is an example of that.
273 params->handle = CreateThread(NULL, 0, rpc_sendreceive_thread, params, 0, &tid);
276 ERR("Could not create RpcSendReceive thread, error %lx\n", GetLastError());
280 if (apt) apartment_release(apt);
283 hr = CoWaitForMultipleHandles(0, INFINITE, 1, ¶ms->handle, &index);
284 CloseHandle(params->handle);
286 if (hr == S_OK) hr = params->hr;
288 status = params->status;
289 HeapFree(GetProcessHeap(), 0, params);
294 if (pstatus) *pstatus = status;
296 TRACE("RPC call status: 0x%lx\n", status);
297 if (status == RPC_S_OK)
299 else if (status == RPC_S_CALL_FAILED)
300 hr = *(HRESULT *)olemsg->Buffer;
302 hr = HRESULT_FROM_WIN32(status);
304 TRACE("-- 0x%08lx\n", hr);
309 static HRESULT WINAPI ServerRpcChannelBuffer_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
311 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
314 TRACE("(%p)\n", msg);
316 status = I_RpcFreeBuffer(msg);
318 TRACE("-- %ld\n", status);
320 return HRESULT_FROM_WIN32(status);
323 static HRESULT WINAPI ClientRpcChannelBuffer_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
325 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
328 TRACE("(%p)\n", msg);
330 status = I_RpcFreeBuffer(msg);
332 HeapFree(GetProcessHeap(), 0, msg->RpcInterfaceInformation);
333 msg->RpcInterfaceInformation = NULL;
335 TRACE("-- %ld\n", status);
337 return HRESULT_FROM_WIN32(status);
340 static HRESULT WINAPI RpcChannelBuffer_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
342 FIXME("(%p,%p), stub!\n", pdwDestContext, ppvDestContext);
346 static HRESULT WINAPI RpcChannelBuffer_IsConnected(LPRPCCHANNELBUFFER iface)
349 /* native does nothing too */
353 static const IRpcChannelBufferVtbl ClientRpcChannelBufferVtbl =
355 RpcChannelBuffer_QueryInterface,
356 RpcChannelBuffer_AddRef,
357 ClientRpcChannelBuffer_Release,
358 ClientRpcChannelBuffer_GetBuffer,
359 RpcChannelBuffer_SendReceive,
360 ClientRpcChannelBuffer_FreeBuffer,
361 RpcChannelBuffer_GetDestCtx,
362 RpcChannelBuffer_IsConnected
365 static const IRpcChannelBufferVtbl ServerRpcChannelBufferVtbl =
367 RpcChannelBuffer_QueryInterface,
368 RpcChannelBuffer_AddRef,
369 ServerRpcChannelBuffer_Release,
370 ServerRpcChannelBuffer_GetBuffer,
371 RpcChannelBuffer_SendReceive,
372 ServerRpcChannelBuffer_FreeBuffer,
373 RpcChannelBuffer_GetDestCtx,
374 RpcChannelBuffer_IsConnected
377 /* returns a channel buffer for proxies */
378 HRESULT RPC_CreateClientChannel(const OXID *oxid, const IPID *ipid, IRpcChannelBuffer **chan)
380 ClientRpcChannelBuffer *This;
382 RPC_BINDING_HANDLE bind;
384 LPWSTR string_binding;
386 /* connect to the apartment listener thread */
387 get_rpc_endpoint(endpoint, oxid);
389 TRACE("proxy pipe: connecting to endpoint: %s\n", debugstr_w(endpoint));
391 status = RpcStringBindingComposeW(
399 if (status == RPC_S_OK)
401 status = RpcBindingFromStringBindingW(string_binding, &bind);
403 if (status == RPC_S_OK)
405 IPID ipid2 = *ipid; /* why can't RpcBindingSetObject take a const? */
406 status = RpcBindingSetObject(bind, &ipid2);
407 if (status != RPC_S_OK)
408 RpcBindingFree(&bind);
411 RpcStringFreeW(&string_binding);
414 if (status != RPC_S_OK)
416 ERR("Couldn't get binding for endpoint %s, status = %ld\n", debugstr_w(endpoint), status);
417 return HRESULT_FROM_WIN32(status);
420 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
423 RpcBindingFree(&bind);
424 return E_OUTOFMEMORY;
427 This->super.lpVtbl = &ClientRpcChannelBufferVtbl;
428 This->super.refs = 1;
431 *chan = (IRpcChannelBuffer*)This;
436 HRESULT RPC_CreateServerChannel(IRpcChannelBuffer **chan)
438 RpcChannelBuffer *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
440 return E_OUTOFMEMORY;
442 This->lpVtbl = &ServerRpcChannelBufferVtbl;
445 *chan = (IRpcChannelBuffer*)This;
451 void RPC_ExecuteCall(struct dispatch_params *params)
455 params->hr = IRpcStubBuffer_Invoke(params->stub, params->msg, params->chan);
459 params->hr = GetExceptionCode();
462 IRpcStubBuffer_Release(params->stub);
463 if (params->handle) SetEvent(params->handle);
466 static void __RPC_STUB dispatch_rpc(RPC_MESSAGE *msg)
468 struct dispatch_params *params;
469 IRpcStubBuffer *stub;
473 RpcBindingInqObject(msg->Handle, &ipid);
475 TRACE("ipid = %s, iMethod = %d\n", debugstr_guid(&ipid), msg->ProcNum);
477 params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*params));
478 if (!params) return RpcRaiseException(E_OUTOFMEMORY);
480 stub = ipid_to_apt_and_stubbuffer(&ipid, &apt);
483 if (apt) apartment_release(apt);
484 ERR("no apartment found for ipid %s\n", debugstr_guid(&ipid));
485 return RpcRaiseException(RPC_E_DISCONNECTED);
488 params->msg = (RPCOLEMESSAGE *)msg;
490 params->chan = NULL; /* FIXME: pass server channel */
491 params->status = RPC_S_OK;
493 /* Note: this is the important difference between STAs and MTAs - we
494 * always execute RPCs to STAs in the thread that originally created the
495 * apartment (i.e. the one that pumps messages to the window) */
496 if (apt->model & COINIT_APARTMENTTHREADED)
498 params->handle = CreateEventW(NULL, FALSE, FALSE, NULL);
500 TRACE("Calling apartment thread 0x%08lx...\n", apt->tid);
502 PostMessageW(apt->win, DM_EXECUTERPC, 0, (LPARAM)params);
503 WaitForSingleObject(params->handle, INFINITE);
504 CloseHandle(params->handle);
507 RPC_ExecuteCall(params);
509 HeapFree(GetProcessHeap(), 0, params);
511 apartment_release(apt);
514 /* stub registration */
515 HRESULT RPC_RegisterInterface(REFIID riid)
517 struct registered_if *rif;
521 TRACE("(%s)\n", debugstr_guid(riid));
523 EnterCriticalSection(&csRegIf);
524 LIST_FOR_EACH_ENTRY(rif, ®istered_interfaces, struct registered_if, entry)
526 if (IsEqualGUID(&rif->If.InterfaceId.SyntaxGUID, riid))
535 TRACE("Creating new interface\n");
537 rif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rif));
543 rif->If.Length = sizeof(RPC_SERVER_INTERFACE);
544 /* RPC interface ID = COM interface ID */
545 rif->If.InterfaceId.SyntaxGUID = *riid;
546 rif->If.DispatchTable = &rpc_dispatch;
547 /* all other fields are 0, including the version asCOM objects
548 * always have a version of 0.0 */
549 status = RpcServerRegisterIfEx(
550 (RPC_IF_HANDLE)&rif->If,
552 RPC_IF_OLE | RPC_IF_AUTOLISTEN,
553 RPC_C_LISTEN_MAX_CALLS_DEFAULT,
555 if (status == RPC_S_OK)
556 list_add_tail(®istered_interfaces, &rif->entry);
559 ERR("RpcServerRegisterIfEx failed with error %ld\n", status);
560 HeapFree(GetProcessHeap(), 0, rif);
561 hr = HRESULT_FROM_WIN32(status);
567 LeaveCriticalSection(&csRegIf);
571 /* stub unregistration */
572 void RPC_UnregisterInterface(REFIID riid)
574 struct registered_if *rif;
575 EnterCriticalSection(&csRegIf);
576 LIST_FOR_EACH_ENTRY(rif, ®istered_interfaces, struct registered_if, entry)
578 if (IsEqualGUID(&rif->If.InterfaceId.SyntaxGUID, riid))
582 #if 0 /* this is a stub in builtin and spams the console with FIXME's */
583 IID iid = *riid; /* RpcServerUnregisterIf doesn't take const IID */
584 RpcServerUnregisterIf((RPC_IF_HANDLE)&rif->If, &iid, 0);
585 list_remove(&rif->entry);
586 HeapFree(GetProcessHeap(), 0, rif);
592 LeaveCriticalSection(&csRegIf);
595 /* make the apartment reachable by other threads and processes and create the
596 * IRemUnknown object */
597 void RPC_StartRemoting(struct apartment *apt)
599 if (!InterlockedExchange(&apt->remoting_started, TRUE))
604 get_rpc_endpoint(endpoint, &apt->oxid);
606 status = RpcServerUseProtseqEpW(
608 RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
611 if (status != RPC_S_OK)
612 ERR("Couldn't register endpoint %s\n", debugstr_w(endpoint));
614 /* FIXME: move remote unknown exporting into this function */
616 start_apartment_remote_unknown();
620 static HRESULT create_server(REFCLSID rclsid)
622 static const WCHAR wszLocalServer32[] = { 'L','o','c','a','l','S','e','r','v','e','r','3','2',0 };
623 static const WCHAR embedding[] = { ' ', '-','E','m','b','e','d','d','i','n','g',0 };
626 WCHAR command[MAX_PATH+sizeof(embedding)/sizeof(WCHAR)];
627 DWORD size = MAX_PATH+1 * sizeof(WCHAR);
629 PROCESS_INFORMATION pinfo;
631 hres = COM_OpenKeyForCLSID(rclsid, wszLocalServer32, KEY_READ, &key);
633 ERR("class %s not registered\n", debugstr_guid(rclsid));
637 hres = RegQueryValueExW(key, NULL, NULL, NULL, (LPBYTE)command, &size);
640 WARN("No default value for LocalServer32 key\n");
641 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
644 memset(&sinfo,0,sizeof(sinfo));
645 sinfo.cb = sizeof(sinfo);
647 /* EXE servers are started with the -Embedding switch. */
649 strcatW(command, embedding);
651 TRACE("activating local server %s for %s\n", debugstr_w(command), debugstr_guid(rclsid));
653 /* FIXME: Win2003 supports a ServerExecutable value that is passed into
655 if (!CreateProcessW(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &sinfo, &pinfo)) {
656 WARN("failed to run local server %s\n", debugstr_w(command));
657 return HRESULT_FROM_WIN32(GetLastError());
659 CloseHandle(pinfo.hProcess);
660 CloseHandle(pinfo.hThread);
666 * start_local_service() - start a service given its name and parameters
668 static DWORD start_local_service(LPCWSTR name, DWORD num, LPWSTR *params)
670 SC_HANDLE handle, hsvc;
671 DWORD r = ERROR_FUNCTION_FAILED;
673 TRACE("Starting service %s %ld params\n", debugstr_w(name), num);
675 handle = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
678 hsvc = OpenServiceW(handle, name, SC_MANAGER_ALL_ACCESS);
681 if(StartServiceW(hsvc, num, (LPCWSTR*)params))
685 if (r == ERROR_SERVICE_ALREADY_RUNNING)
687 CloseServiceHandle(hsvc);
689 CloseServiceHandle(handle);
691 TRACE("StartService returned error %ld (%s)\n", r, r?"ok":"failed");
697 * create_local_service() - start a COM server in a service
699 * To start a Local Service, we read the AppID value under
700 * the class's CLSID key, then open the HKCR\\AppId key specified
701 * there and check for a LocalService value.
703 * Note: Local Services are not supported under Windows 9x
705 static HRESULT create_local_service(REFCLSID rclsid)
708 WCHAR buf[CHARS_IN_GUID], keyname[50];
709 static const WCHAR szAppId[] = { 'A','p','p','I','d',0 };
710 static const WCHAR szAppIdKey[] = { 'A','p','p','I','d','\\',0 };
711 static const WCHAR szLocalService[] = { 'L','o','c','a','l','S','e','r','v','i','c','e',0 };
712 static const WCHAR szServiceParams[] = {'S','e','r','v','i','c','e','P','a','r','a','m','s',0};
717 TRACE("Attempting to start Local service for %s\n", debugstr_guid(rclsid));
719 /* read the AppID value under the class's key */
720 hres = COM_OpenKeyForCLSID(rclsid, szAppId, KEY_READ, &hkey);
724 r = RegQueryValueExW(hkey, NULL, NULL, &type, (LPBYTE)buf, &sz);
726 if (r!=ERROR_SUCCESS || type!=REG_SZ)
729 /* read the LocalService and ServiceParameters values from the AppID key */
730 strcpyW(keyname, szAppIdKey);
731 strcatW(keyname, buf);
732 r = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &hkey);
733 if (r!=ERROR_SUCCESS)
736 r = RegQueryValueExW(hkey, szLocalService, NULL, &type, (LPBYTE)buf, &sz);
737 if (r==ERROR_SUCCESS && type==REG_SZ)
740 LPWSTR args[1] = { NULL };
743 * FIXME: I'm not really sure how to deal with the service parameters.
744 * I suspect that the string returned from RegQueryValueExW
745 * should be split into a number of arguments by spaces.
746 * It would make more sense if ServiceParams contained a
747 * REG_MULTI_SZ here, but it's a REG_SZ for the services
748 * that I'm interested in for the moment.
750 r = RegQueryValueExW(hkey, szServiceParams, NULL, &type, NULL, &sz);
751 if (r == ERROR_SUCCESS && type == REG_SZ && sz)
753 args[0] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sz);
755 RegQueryValueExW(hkey, szServiceParams, NULL, &type, (LPBYTE)args[0], &sz);
757 r = start_local_service(buf, num_args, args);
758 if (r==ERROR_SUCCESS)
760 HeapFree(GetProcessHeap(),0,args[0]);
768 static void get_localserver_pipe_name(WCHAR *pipefn, REFCLSID rclsid)
770 static const WCHAR wszPipeRef[] = {'\\','\\','.','\\','p','i','p','e','\\',0};
771 strcpyW(pipefn, wszPipeRef);
772 StringFromGUID2(rclsid, pipefn + sizeof(wszPipeRef)/sizeof(wszPipeRef[0]) - 1, CHARS_IN_GUID);
775 /* FIXME: should call to rpcss instead */
776 HRESULT RPC_GetLocalClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
781 DWORD res, bufferlen;
782 char marshalbuffer[200];
784 LARGE_INTEGER seekto;
785 ULARGE_INTEGER newpos;
788 static const int MAXTRIES = 30; /* 30 seconds */
790 TRACE("rclsid=%s, iid=%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
792 get_localserver_pipe_name(pipefn, rclsid);
794 while (tries++ < MAXTRIES) {
795 TRACE("waiting for %s\n", debugstr_w(pipefn));
797 WaitNamedPipeW( pipefn, NMPWAIT_WAIT_FOREVER );
798 hPipe = CreateFileW(pipefn, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
799 if (hPipe == INVALID_HANDLE_VALUE) {
801 if ( (hres = create_server(rclsid)) &&
802 (hres = create_local_service(rclsid)) )
806 WARN("Connecting to %s, no response yet, retrying: le is %lx\n", debugstr_w(pipefn), GetLastError());
812 if (!ReadFile(hPipe,marshalbuffer,sizeof(marshalbuffer),&bufferlen,NULL)) {
813 FIXME("Failed to read marshal id from classfactory of %s.\n",debugstr_guid(rclsid));
817 TRACE("read marshal id from pipe\n");
822 if (tries >= MAXTRIES)
823 return E_NOINTERFACE;
825 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
826 if (hres) return hres;
827 hres = IStream_Write(pStm,marshalbuffer,bufferlen,&res);
829 seekto.u.LowPart = 0;seekto.u.HighPart = 0;
830 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
832 TRACE("unmarshalling classfactory\n");
833 hres = CoUnmarshalInterface(pStm,&IID_IClassFactory,ppv);
835 IStream_Release(pStm);
840 struct local_server_params
846 /* FIXME: should call to rpcss instead */
847 static DWORD WINAPI local_server_thread(LPVOID param)
849 struct local_server_params * lsp = (struct local_server_params *)param;
853 IStream *pStm = lsp->stream;
855 unsigned char *buffer;
857 LARGE_INTEGER seekto;
858 ULARGE_INTEGER newpos;
861 TRACE("Starting threader for %s.\n",debugstr_guid(&lsp->clsid));
863 get_localserver_pipe_name(pipefn, &lsp->clsid);
865 HeapFree(GetProcessHeap(), 0, lsp);
867 hPipe = CreateNamedPipeW( pipefn, PIPE_ACCESS_DUPLEX,
868 PIPE_TYPE_BYTE|PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,
869 4096, 4096, 500 /* 0.5 second timeout */, NULL );
871 if (hPipe == INVALID_HANDLE_VALUE)
873 FIXME("pipe creation failed for %s, le is %ld\n", debugstr_w(pipefn), GetLastError());
878 if (!ConnectNamedPipe(hPipe,NULL)) {
879 ERR("Failure during ConnectNamedPipe %ld, ABORT!\n",GetLastError());
883 TRACE("marshalling IClassFactory to client\n");
885 hres = IStream_Stat(pStm,&ststg,0);
886 if (hres) return hres;
888 buflen = ststg.cbSize.u.LowPart;
889 buffer = HeapAlloc(GetProcessHeap(),0,buflen);
890 seekto.u.LowPart = 0;
891 seekto.u.HighPart = 0;
892 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
894 FIXME("IStream_Seek failed, %lx\n",hres);
898 hres = IStream_Read(pStm,buffer,buflen,&res);
900 FIXME("Stream Read failed, %lx\n",hres);
904 WriteFile(hPipe,buffer,buflen,&res,NULL);
905 FlushFileBuffers(hPipe);
906 DisconnectNamedPipe(hPipe);
908 TRACE("done marshalling IClassFactory\n");
911 IStream_Release(pStm);
915 void RPC_StartLocalServer(REFCLSID clsid, IStream *stream)
919 struct local_server_params *lsp = HeapAlloc(GetProcessHeap(), 0, sizeof(*lsp));
922 lsp->stream = stream;
924 thread = CreateThread(NULL, 0, local_server_thread, lsp, 0, &tid);
926 /* FIXME: failure handling */