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 return EXCEPTION_CONTINUE_SEARCH;
117 return EXCEPTION_EXECUTE_HANDLER;
120 static HRESULT WINAPI RpcChannelBuffer_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
123 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
125 *ppv = (LPVOID)iface;
126 IUnknown_AddRef(iface);
129 return E_NOINTERFACE;
132 static ULONG WINAPI RpcChannelBuffer_AddRef(LPRPCCHANNELBUFFER iface)
134 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
135 return InterlockedIncrement(&This->refs);
138 static ULONG WINAPI ServerRpcChannelBuffer_Release(LPRPCCHANNELBUFFER iface)
140 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
143 ref = InterlockedDecrement(&This->refs);
147 HeapFree(GetProcessHeap(), 0, This);
151 static ULONG WINAPI ClientRpcChannelBuffer_Release(LPRPCCHANNELBUFFER iface)
153 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
156 ref = InterlockedDecrement(&This->super.refs);
160 RpcBindingFree(&This->bind);
161 HeapFree(GetProcessHeap(), 0, This);
165 static HRESULT WINAPI ServerRpcChannelBuffer_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
167 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
168 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
171 TRACE("(%p)->(%p,%s)\n", This, olemsg, debugstr_guid(riid));
173 status = I_RpcGetBuffer(msg);
175 TRACE("-- %ld\n", status);
177 return HRESULT_FROM_WIN32(status);
180 static HRESULT WINAPI ClientRpcChannelBuffer_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
182 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
183 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
184 RPC_CLIENT_INTERFACE *cif;
187 TRACE("(%p)->(%p,%s)\n", This, olemsg, debugstr_guid(riid));
189 cif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_CLIENT_INTERFACE));
191 return E_OUTOFMEMORY;
193 cif->Length = sizeof(RPC_CLIENT_INTERFACE);
194 /* RPC interface ID = COM interface ID */
195 cif->InterfaceId.SyntaxGUID = *riid;
196 /* COM objects always have a version of 0.0 */
197 cif->InterfaceId.SyntaxVersion.MajorVersion = 0;
198 cif->InterfaceId.SyntaxVersion.MinorVersion = 0;
199 msg->RpcInterfaceInformation = cif;
200 msg->Handle = This->bind;
202 status = I_RpcGetBuffer(msg);
204 TRACE("-- %ld\n", status);
206 return HRESULT_FROM_WIN32(status);
209 /* this thread runs an outgoing RPC */
210 static DWORD WINAPI rpc_sendreceive_thread(LPVOID param)
212 struct dispatch_params *data = (struct dispatch_params *) param;
214 /* FIXME: trap and rethrow RPC exceptions in app thread */
215 data->status = I_RpcSendReceive((RPC_MESSAGE *)data->msg);
217 TRACE("completed with status 0x%lx\n", data->status);
222 static HRESULT WINAPI RpcChannelBuffer_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
225 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
228 struct dispatch_params *params;
230 IRpcStubBuffer *stub;
234 TRACE("(%p) iMethod=%ld\n", olemsg, olemsg->iMethod);
236 params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*params));
237 if (!params) return E_OUTOFMEMORY;
239 params->msg = olemsg;
240 params->status = RPC_S_OK;
243 /* Note: this is an optimization in the Microsoft OLE runtime that we need
244 * to copy, as shown by the test_no_couninitialize_client test. without
245 * short-circuiting the RPC runtime in the case below, the test will
246 * deadlock on the loader lock due to the RPC runtime needing to create
247 * a thread to process the RPC when this function is called indirectly
250 RpcBindingInqObject(msg->Handle, &ipid);
251 stub = ipid_to_apt_and_stubbuffer(&ipid, &apt);
252 if (apt && (apt->model & COINIT_APARTMENTTHREADED))
255 params->chan = NULL; /* FIXME: pass server channel */
256 params->handle = CreateEventW(NULL, FALSE, FALSE, NULL);
258 TRACE("Calling apartment thread 0x%08lx...\n", apt->tid);
260 PostMessageW(apt->win, DM_EXECUTERPC, 0, (LPARAM)params);
264 if (stub) IRpcStubBuffer_Release(stub);
266 /* we use a separate thread here because we need to be able to
267 * pump the message loop in the application thread: if we do not,
268 * any windows created by this thread will hang and RPCs that try
269 * and re-enter this STA from an incoming server thread will
270 * deadlock. InstallShield is an example of that.
272 params->handle = CreateThread(NULL, 0, rpc_sendreceive_thread, params, 0, &tid);
275 ERR("Could not create RpcSendReceive thread, error %lx\n", GetLastError());
279 if (apt) apartment_release(apt);
282 hr = CoWaitForMultipleHandles(0, INFINITE, 1, ¶ms->handle, &index);
283 CloseHandle(params->handle);
285 if (hr == S_OK) hr = params->hr;
287 status = params->status;
288 HeapFree(GetProcessHeap(), 0, params);
293 if (pstatus) *pstatus = status;
295 TRACE("RPC call status: 0x%lx\n", status);
296 if (status == RPC_S_OK)
298 else if (status == RPC_S_CALL_FAILED)
299 hr = *(HRESULT *)olemsg->Buffer;
301 hr = HRESULT_FROM_WIN32(status);
303 TRACE("-- 0x%08lx\n", hr);
308 static HRESULT WINAPI ServerRpcChannelBuffer_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
310 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
313 TRACE("(%p)\n", msg);
315 status = I_RpcFreeBuffer(msg);
317 TRACE("-- %ld\n", status);
319 return HRESULT_FROM_WIN32(status);
322 static HRESULT WINAPI ClientRpcChannelBuffer_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
324 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
327 TRACE("(%p)\n", msg);
329 status = I_RpcFreeBuffer(msg);
331 HeapFree(GetProcessHeap(), 0, msg->RpcInterfaceInformation);
332 msg->RpcInterfaceInformation = NULL;
334 TRACE("-- %ld\n", status);
336 return HRESULT_FROM_WIN32(status);
339 static HRESULT WINAPI RpcChannelBuffer_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
341 FIXME("(%p,%p), stub!\n", pdwDestContext, ppvDestContext);
345 static HRESULT WINAPI RpcChannelBuffer_IsConnected(LPRPCCHANNELBUFFER iface)
348 /* native does nothing too */
352 static const IRpcChannelBufferVtbl ClientRpcChannelBufferVtbl =
354 RpcChannelBuffer_QueryInterface,
355 RpcChannelBuffer_AddRef,
356 ClientRpcChannelBuffer_Release,
357 ClientRpcChannelBuffer_GetBuffer,
358 RpcChannelBuffer_SendReceive,
359 ClientRpcChannelBuffer_FreeBuffer,
360 RpcChannelBuffer_GetDestCtx,
361 RpcChannelBuffer_IsConnected
364 static const IRpcChannelBufferVtbl ServerRpcChannelBufferVtbl =
366 RpcChannelBuffer_QueryInterface,
367 RpcChannelBuffer_AddRef,
368 ServerRpcChannelBuffer_Release,
369 ServerRpcChannelBuffer_GetBuffer,
370 RpcChannelBuffer_SendReceive,
371 ServerRpcChannelBuffer_FreeBuffer,
372 RpcChannelBuffer_GetDestCtx,
373 RpcChannelBuffer_IsConnected
376 /* returns a channel buffer for proxies */
377 HRESULT RPC_CreateClientChannel(const OXID *oxid, const IPID *ipid, IRpcChannelBuffer **chan)
379 ClientRpcChannelBuffer *This;
381 RPC_BINDING_HANDLE bind;
383 LPWSTR string_binding;
385 /* connect to the apartment listener thread */
386 get_rpc_endpoint(endpoint, oxid);
388 TRACE("proxy pipe: connecting to endpoint: %s\n", debugstr_w(endpoint));
390 status = RpcStringBindingComposeW(
398 if (status == RPC_S_OK)
400 status = RpcBindingFromStringBindingW(string_binding, &bind);
402 if (status == RPC_S_OK)
404 IPID ipid2 = *ipid; /* why can't RpcBindingSetObject take a const? */
405 status = RpcBindingSetObject(bind, &ipid2);
406 if (status != RPC_S_OK)
407 RpcBindingFree(&bind);
410 RpcStringFreeW(&string_binding);
413 if (status != RPC_S_OK)
415 ERR("Couldn't get binding for endpoint %s, status = %ld\n", debugstr_w(endpoint), status);
416 return HRESULT_FROM_WIN32(status);
419 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
422 RpcBindingFree(&bind);
423 return E_OUTOFMEMORY;
426 This->super.lpVtbl = &ClientRpcChannelBufferVtbl;
427 This->super.refs = 1;
430 *chan = (IRpcChannelBuffer*)This;
435 HRESULT RPC_CreateServerChannel(IRpcChannelBuffer **chan)
437 RpcChannelBuffer *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
439 return E_OUTOFMEMORY;
441 This->lpVtbl = &ServerRpcChannelBufferVtbl;
444 *chan = (IRpcChannelBuffer*)This;
450 void RPC_ExecuteCall(struct dispatch_params *params)
454 params->hr = IRpcStubBuffer_Invoke(params->stub, params->msg, params->chan);
458 params->hr = GetExceptionCode();
461 IRpcStubBuffer_Release(params->stub);
462 if (params->handle) SetEvent(params->handle);
465 static void __RPC_STUB dispatch_rpc(RPC_MESSAGE *msg)
467 struct dispatch_params *params;
468 IRpcStubBuffer *stub;
472 RpcBindingInqObject(msg->Handle, &ipid);
474 TRACE("ipid = %s, iMethod = %d\n", debugstr_guid(&ipid), msg->ProcNum);
476 params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*params));
477 if (!params) return RpcRaiseException(E_OUTOFMEMORY);
479 stub = ipid_to_apt_and_stubbuffer(&ipid, &apt);
482 if (apt) apartment_release(apt);
483 ERR("no apartment found for ipid %s\n", debugstr_guid(&ipid));
484 return RpcRaiseException(RPC_E_DISCONNECTED);
487 params->msg = (RPCOLEMESSAGE *)msg;
489 params->chan = NULL; /* FIXME: pass server channel */
490 params->status = RPC_S_OK;
492 /* Note: this is the important difference between STAs and MTAs - we
493 * always execute RPCs to STAs in the thread that originally created the
494 * apartment (i.e. the one that pumps messages to the window) */
495 if (apt->model & COINIT_APARTMENTTHREADED)
497 params->handle = CreateEventW(NULL, FALSE, FALSE, NULL);
499 TRACE("Calling apartment thread 0x%08lx...\n", apt->tid);
501 PostMessageW(apt->win, DM_EXECUTERPC, 0, (LPARAM)params);
502 WaitForSingleObject(params->handle, INFINITE);
503 CloseHandle(params->handle);
506 RPC_ExecuteCall(params);
508 HeapFree(GetProcessHeap(), 0, params);
510 apartment_release(apt);
513 /* stub registration */
514 HRESULT RPC_RegisterInterface(REFIID riid)
516 struct registered_if *rif;
520 TRACE("(%s)\n", debugstr_guid(riid));
522 EnterCriticalSection(&csRegIf);
523 LIST_FOR_EACH_ENTRY(rif, ®istered_interfaces, struct registered_if, entry)
525 if (IsEqualGUID(&rif->If.InterfaceId.SyntaxGUID, riid))
534 TRACE("Creating new interface\n");
536 rif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rif));
542 rif->If.Length = sizeof(RPC_SERVER_INTERFACE);
543 /* RPC interface ID = COM interface ID */
544 rif->If.InterfaceId.SyntaxGUID = *riid;
545 rif->If.DispatchTable = &rpc_dispatch;
546 /* all other fields are 0, including the version asCOM objects
547 * always have a version of 0.0 */
548 status = RpcServerRegisterIfEx(
549 (RPC_IF_HANDLE)&rif->If,
551 RPC_IF_OLE | RPC_IF_AUTOLISTEN,
552 RPC_C_LISTEN_MAX_CALLS_DEFAULT,
554 if (status == RPC_S_OK)
555 list_add_tail(®istered_interfaces, &rif->entry);
558 ERR("RpcServerRegisterIfEx failed with error %ld\n", status);
559 HeapFree(GetProcessHeap(), 0, rif);
560 hr = HRESULT_FROM_WIN32(status);
566 LeaveCriticalSection(&csRegIf);
570 /* stub unregistration */
571 void RPC_UnregisterInterface(REFIID riid)
573 struct registered_if *rif;
574 EnterCriticalSection(&csRegIf);
575 LIST_FOR_EACH_ENTRY(rif, ®istered_interfaces, struct registered_if, entry)
577 if (IsEqualGUID(&rif->If.InterfaceId.SyntaxGUID, riid))
581 #if 0 /* this is a stub in builtin and spams the console with FIXME's */
582 IID iid = *riid; /* RpcServerUnregisterIf doesn't take const IID */
583 RpcServerUnregisterIf((RPC_IF_HANDLE)&rif->If, &iid, 0);
584 list_remove(&rif->entry);
585 HeapFree(GetProcessHeap(), 0, rif);
591 LeaveCriticalSection(&csRegIf);
594 /* make the apartment reachable by other threads and processes and create the
595 * IRemUnknown object */
596 void RPC_StartRemoting(struct apartment *apt)
598 if (!InterlockedExchange(&apt->remoting_started, TRUE))
603 get_rpc_endpoint(endpoint, &apt->oxid);
605 status = RpcServerUseProtseqEpW(
607 RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
610 if (status != RPC_S_OK)
611 ERR("Couldn't register endpoint %s\n", debugstr_w(endpoint));
613 /* FIXME: move remote unknown exporting into this function */
615 start_apartment_remote_unknown();
619 static HRESULT create_server(REFCLSID rclsid)
621 static const WCHAR wszLocalServer32[] = { 'L','o','c','a','l','S','e','r','v','e','r','3','2',0 };
622 static const WCHAR embedding[] = { ' ', '-','E','m','b','e','d','d','i','n','g',0 };
625 WCHAR command[MAX_PATH+sizeof(embedding)/sizeof(WCHAR)];
626 DWORD size = (MAX_PATH+1) * sizeof(WCHAR);
628 PROCESS_INFORMATION pinfo;
630 hres = COM_OpenKeyForCLSID(rclsid, wszLocalServer32, KEY_READ, &key);
632 ERR("class %s not registered\n", debugstr_guid(rclsid));
636 hres = RegQueryValueExW(key, NULL, NULL, NULL, (LPBYTE)command, &size);
639 WARN("No default value for LocalServer32 key\n");
640 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
643 memset(&sinfo,0,sizeof(sinfo));
644 sinfo.cb = sizeof(sinfo);
646 /* EXE servers are started with the -Embedding switch. */
648 strcatW(command, embedding);
650 TRACE("activating local server %s for %s\n", debugstr_w(command), debugstr_guid(rclsid));
652 /* FIXME: Win2003 supports a ServerExecutable value that is passed into
654 if (!CreateProcessW(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &sinfo, &pinfo)) {
655 WARN("failed to run local server %s\n", debugstr_w(command));
656 return HRESULT_FROM_WIN32(GetLastError());
658 CloseHandle(pinfo.hProcess);
659 CloseHandle(pinfo.hThread);
665 * start_local_service() - start a service given its name and parameters
667 static DWORD start_local_service(LPCWSTR name, DWORD num, LPWSTR *params)
669 SC_HANDLE handle, hsvc;
670 DWORD r = ERROR_FUNCTION_FAILED;
672 TRACE("Starting service %s %ld params\n", debugstr_w(name), num);
674 handle = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
677 hsvc = OpenServiceW(handle, name, SC_MANAGER_ALL_ACCESS);
680 if(StartServiceW(hsvc, num, (LPCWSTR*)params))
684 if (r == ERROR_SERVICE_ALREADY_RUNNING)
686 CloseServiceHandle(hsvc);
688 CloseServiceHandle(handle);
690 TRACE("StartService returned error %ld (%s)\n", r, r?"ok":"failed");
696 * create_local_service() - start a COM server in a service
698 * To start a Local Service, we read the AppID value under
699 * the class's CLSID key, then open the HKCR\\AppId key specified
700 * there and check for a LocalService value.
702 * Note: Local Services are not supported under Windows 9x
704 static HRESULT create_local_service(REFCLSID rclsid)
707 WCHAR buf[CHARS_IN_GUID], keyname[50];
708 static const WCHAR szAppId[] = { 'A','p','p','I','d',0 };
709 static const WCHAR szAppIdKey[] = { 'A','p','p','I','d','\\',0 };
710 static const WCHAR szLocalService[] = { 'L','o','c','a','l','S','e','r','v','i','c','e',0 };
711 static const WCHAR szServiceParams[] = {'S','e','r','v','i','c','e','P','a','r','a','m','s',0};
716 TRACE("Attempting to start Local service for %s\n", debugstr_guid(rclsid));
718 /* read the AppID value under the class's key */
719 hres = COM_OpenKeyForCLSID(rclsid, szAppId, KEY_READ, &hkey);
723 r = RegQueryValueExW(hkey, NULL, NULL, &type, (LPBYTE)buf, &sz);
725 if (r!=ERROR_SUCCESS || type!=REG_SZ)
728 /* read the LocalService and ServiceParameters values from the AppID key */
729 strcpyW(keyname, szAppIdKey);
730 strcatW(keyname, buf);
731 r = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &hkey);
732 if (r!=ERROR_SUCCESS)
735 r = RegQueryValueExW(hkey, szLocalService, NULL, &type, (LPBYTE)buf, &sz);
736 if (r==ERROR_SUCCESS && type==REG_SZ)
739 LPWSTR args[1] = { NULL };
742 * FIXME: I'm not really sure how to deal with the service parameters.
743 * I suspect that the string returned from RegQueryValueExW
744 * should be split into a number of arguments by spaces.
745 * It would make more sense if ServiceParams contained a
746 * REG_MULTI_SZ here, but it's a REG_SZ for the services
747 * that I'm interested in for the moment.
749 r = RegQueryValueExW(hkey, szServiceParams, NULL, &type, NULL, &sz);
750 if (r == ERROR_SUCCESS && type == REG_SZ && sz)
752 args[0] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sz);
754 RegQueryValueExW(hkey, szServiceParams, NULL, &type, (LPBYTE)args[0], &sz);
756 r = start_local_service(buf, num_args, args);
757 if (r==ERROR_SUCCESS)
759 HeapFree(GetProcessHeap(),0,args[0]);
767 static void get_localserver_pipe_name(WCHAR *pipefn, REFCLSID rclsid)
769 static const WCHAR wszPipeRef[] = {'\\','\\','.','\\','p','i','p','e','\\',0};
770 strcpyW(pipefn, wszPipeRef);
771 StringFromGUID2(rclsid, pipefn + sizeof(wszPipeRef)/sizeof(wszPipeRef[0]) - 1, CHARS_IN_GUID);
774 /* FIXME: should call to rpcss instead */
775 HRESULT RPC_GetLocalClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
780 DWORD res, bufferlen;
781 char marshalbuffer[200];
783 LARGE_INTEGER seekto;
784 ULARGE_INTEGER newpos;
787 static const int MAXTRIES = 30; /* 30 seconds */
789 TRACE("rclsid=%s, iid=%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
791 get_localserver_pipe_name(pipefn, rclsid);
793 while (tries++ < MAXTRIES) {
794 TRACE("waiting for %s\n", debugstr_w(pipefn));
796 WaitNamedPipeW( pipefn, NMPWAIT_WAIT_FOREVER );
797 hPipe = CreateFileW(pipefn, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
798 if (hPipe == INVALID_HANDLE_VALUE) {
800 if ( (hres = create_server(rclsid)) &&
801 (hres = create_local_service(rclsid)) )
805 WARN("Connecting to %s, no response yet, retrying: le is %lx\n", debugstr_w(pipefn), GetLastError());
811 if (!ReadFile(hPipe,marshalbuffer,sizeof(marshalbuffer),&bufferlen,NULL)) {
812 FIXME("Failed to read marshal id from classfactory of %s.\n",debugstr_guid(rclsid));
816 TRACE("read marshal id from pipe\n");
821 if (tries >= MAXTRIES)
822 return E_NOINTERFACE;
824 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
825 if (hres) return hres;
826 hres = IStream_Write(pStm,marshalbuffer,bufferlen,&res);
828 seekto.u.LowPart = 0;seekto.u.HighPart = 0;
829 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
831 TRACE("unmarshalling classfactory\n");
832 hres = CoUnmarshalInterface(pStm,&IID_IClassFactory,ppv);
834 IStream_Release(pStm);
839 struct local_server_params
845 /* FIXME: should call to rpcss instead */
846 static DWORD WINAPI local_server_thread(LPVOID param)
848 struct local_server_params * lsp = (struct local_server_params *)param;
852 IStream *pStm = lsp->stream;
854 unsigned char *buffer;
856 LARGE_INTEGER seekto;
857 ULARGE_INTEGER newpos;
860 TRACE("Starting threader for %s.\n",debugstr_guid(&lsp->clsid));
862 get_localserver_pipe_name(pipefn, &lsp->clsid);
864 HeapFree(GetProcessHeap(), 0, lsp);
866 hPipe = CreateNamedPipeW( pipefn, PIPE_ACCESS_DUPLEX,
867 PIPE_TYPE_BYTE|PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,
868 4096, 4096, 500 /* 0.5 second timeout */, NULL );
870 if (hPipe == INVALID_HANDLE_VALUE)
872 FIXME("pipe creation failed for %s, le is %ld\n", debugstr_w(pipefn), GetLastError());
877 if (!ConnectNamedPipe(hPipe,NULL)) {
878 ERR("Failure during ConnectNamedPipe %ld, ABORT!\n",GetLastError());
882 TRACE("marshalling IClassFactory to client\n");
884 hres = IStream_Stat(pStm,&ststg,0);
885 if (hres) return hres;
887 buflen = ststg.cbSize.u.LowPart;
888 buffer = HeapAlloc(GetProcessHeap(),0,buflen);
889 seekto.u.LowPart = 0;
890 seekto.u.HighPart = 0;
891 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
893 FIXME("IStream_Seek failed, %lx\n",hres);
897 hres = IStream_Read(pStm,buffer,buflen,&res);
899 FIXME("Stream Read failed, %lx\n",hres);
903 WriteFile(hPipe,buffer,buflen,&res,NULL);
904 FlushFileBuffers(hPipe);
905 DisconnectNamedPipe(hPipe);
907 TRACE("done marshalling IClassFactory\n");
910 IStream_Release(pStm);
914 void RPC_StartLocalServer(REFCLSID clsid, IStream *stream)
918 struct local_server_params *lsp = HeapAlloc(GetProcessHeap(), 0, sizeof(*lsp));
921 lsp->stream = stream;
923 thread = CreateThread(NULL, 0, local_server_thread, lsp, 0, &tid);
925 /* FIXME: failure handling */