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
45 #include "wine/unicode.h"
47 #include "compobj_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(ole);
53 static void __RPC_STUB dispatch_rpc(RPC_MESSAGE *msg);
55 /* we only use one function to dispatch calls for all methods - we use the
56 * RPC_IF_OLE flag to tell the RPC runtime that this is the case */
57 static RPC_DISPATCH_FUNCTION rpc_dispatch_table[1] = { dispatch_rpc }; /* (RO) */
58 static RPC_DISPATCH_TABLE rpc_dispatch = { 1, rpc_dispatch_table }; /* (RO) */
60 static struct list registered_interfaces = LIST_INIT(registered_interfaces); /* (CS csRegIf) */
61 static CRITICAL_SECTION csRegIf;
62 static CRITICAL_SECTION_DEBUG csRegIf_debug =
65 { &csRegIf_debug.ProcessLocksList, &csRegIf_debug.ProcessLocksList },
66 0, 0, { 0, (DWORD)(__FILE__ ": dcom registered server interfaces") }
68 static CRITICAL_SECTION csRegIf = { &csRegIf_debug, -1, 0, 0, 0, 0 };
70 static WCHAR wszPipeTransport[] = {'n','c','a','c','n','_','n','p',0};
76 DWORD refs; /* ref count */
77 RPC_SERVER_INTERFACE If; /* interface registered with the RPC runtime */
80 /* get the pipe endpoint specified of the specified apartment */
81 static inline void get_rpc_endpoint(LPWSTR endpoint, const OXID *oxid)
83 /* FIXME: should get endpoint from rpcss */
84 static const WCHAR wszEndpointFormat[] = {'\\','p','i','p','e','\\','O','L','E','_','%','0','8','l','x','%','0','8','l','x',0};
85 wsprintfW(endpoint, wszEndpointFormat, (DWORD)(*oxid >> 32),(DWORD)*oxid);
90 const IRpcChannelBufferVtbl *lpVtbl;
96 RpcChannelBuffer super; /* superclass */
98 RPC_BINDING_HANDLE bind; /* handle to the remote server */
99 } ClientRpcChannelBuffer;
101 struct dispatch_params
103 RPCOLEMESSAGE *msg; /* message */
104 IRpcStubBuffer *stub; /* stub buffer, if applicable */
105 IRpcChannelBuffer *chan; /* server channel buffer, if applicable */
106 HANDLE handle; /* handle that will become signaled when call finishes */
107 RPC_STATUS status; /* status (out) */
110 static HRESULT WINAPI RpcChannelBuffer_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
113 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
115 *ppv = (LPVOID)iface;
116 IUnknown_AddRef(iface);
119 return E_NOINTERFACE;
122 static ULONG WINAPI RpcChannelBuffer_AddRef(LPRPCCHANNELBUFFER iface)
124 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
125 return InterlockedIncrement(&This->refs);
128 static ULONG WINAPI ServerRpcChannelBuffer_Release(LPRPCCHANNELBUFFER iface)
130 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
133 ref = InterlockedDecrement(&This->refs);
137 HeapFree(GetProcessHeap(), 0, This);
141 static ULONG WINAPI ClientRpcChannelBuffer_Release(LPRPCCHANNELBUFFER iface)
143 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
146 ref = InterlockedDecrement(&This->super.refs);
150 RpcBindingFree(&This->bind);
151 HeapFree(GetProcessHeap(), 0, This);
155 static HRESULT WINAPI ServerRpcChannelBuffer_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
157 RpcChannelBuffer *This = (RpcChannelBuffer *)iface;
158 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
161 TRACE("(%p)->(%p,%s)\n", This, olemsg, debugstr_guid(riid));
163 status = I_RpcGetBuffer(msg);
165 TRACE("-- %ld\n", status);
167 return HRESULT_FROM_WIN32(status);
170 static HRESULT WINAPI ClientRpcChannelBuffer_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
172 ClientRpcChannelBuffer *This = (ClientRpcChannelBuffer *)iface;
173 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
174 RPC_CLIENT_INTERFACE *cif;
177 TRACE("(%p)->(%p,%s)\n", This, olemsg, debugstr_guid(riid));
179 cif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_CLIENT_INTERFACE));
181 return E_OUTOFMEMORY;
183 cif->Length = sizeof(RPC_CLIENT_INTERFACE);
184 /* RPC interface ID = COM interface ID */
185 cif->InterfaceId.SyntaxGUID = *riid;
186 /* COM objects always have a version of 0.0 */
187 cif->InterfaceId.SyntaxVersion.MajorVersion = 0;
188 cif->InterfaceId.SyntaxVersion.MinorVersion = 0;
189 msg->RpcInterfaceInformation = cif;
190 msg->Handle = This->bind;
192 status = I_RpcGetBuffer(msg);
194 TRACE("-- %ld\n", status);
196 return HRESULT_FROM_WIN32(status);
199 /* this thread runs an outgoing RPC */
200 static DWORD WINAPI rpc_sendreceive_thread(LPVOID param)
202 struct dispatch_params *data = (struct dispatch_params *) param;
204 /* FIXME: trap and rethrow RPC exceptions in app thread */
205 data->status = I_RpcSendReceive((RPC_MESSAGE *)data->msg);
207 TRACE("completed with status 0x%lx\n", data->status);
212 static HRESULT WINAPI RpcChannelBuffer_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
217 struct dispatch_params *params;
220 TRACE("(%p) iMethod=%ld\n", olemsg, olemsg->iMethod);
222 params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*params));
223 if (!params) return E_OUTOFMEMORY;
225 params->msg = olemsg;
226 params->status = RPC_S_OK;
228 /* we use a separate thread here because we need to be able to
229 * pump the message loop in the application thread: if we do not,
230 * any windows created by this thread will hang and RPCs that try
231 * and re-enter this STA from an incoming server thread will
232 * deadlock. InstallShield is an example of that.
234 params->handle = CreateThread(NULL, 0, rpc_sendreceive_thread, params, 0, &tid);
237 ERR("Could not create RpcSendReceive thread, error %lx\n", GetLastError());
242 hr = CoWaitForMultipleHandles(0, INFINITE, 1, ¶ms->handle, &index);
243 CloseHandle(params->handle);
245 status = params->status;
246 HeapFree(GetProcessHeap(), 0, params);
251 if (pstatus) *pstatus = status;
253 TRACE("RPC call status: 0x%lx\n", status);
254 if (status == RPC_S_OK)
256 else if (status == RPC_S_CALL_FAILED)
257 hr = *(HRESULT *)olemsg->Buffer;
259 hr = HRESULT_FROM_WIN32(status);
261 TRACE("-- 0x%08lx\n", hr);
266 static HRESULT WINAPI ServerRpcChannelBuffer_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
268 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
271 TRACE("(%p)\n", msg);
273 status = I_RpcFreeBuffer(msg);
275 TRACE("-- %ld\n", status);
277 return HRESULT_FROM_WIN32(status);
280 static HRESULT WINAPI ClientRpcChannelBuffer_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
282 RPC_MESSAGE *msg = (RPC_MESSAGE *)olemsg;
285 TRACE("(%p)\n", msg);
287 status = I_RpcFreeBuffer(msg);
289 HeapFree(GetProcessHeap(), 0, msg->RpcInterfaceInformation);
290 msg->RpcInterfaceInformation = NULL;
292 TRACE("-- %ld\n", status);
294 return HRESULT_FROM_WIN32(status);
297 static HRESULT WINAPI RpcChannelBuffer_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
299 FIXME("(%p,%p), stub!\n", pdwDestContext, ppvDestContext);
303 static HRESULT WINAPI RpcChannelBuffer_IsConnected(LPRPCCHANNELBUFFER iface)
306 /* native does nothing too */
310 static const IRpcChannelBufferVtbl ClientRpcChannelBufferVtbl =
312 RpcChannelBuffer_QueryInterface,
313 RpcChannelBuffer_AddRef,
314 ClientRpcChannelBuffer_Release,
315 ClientRpcChannelBuffer_GetBuffer,
316 RpcChannelBuffer_SendReceive,
317 ClientRpcChannelBuffer_FreeBuffer,
318 RpcChannelBuffer_GetDestCtx,
319 RpcChannelBuffer_IsConnected
322 static const IRpcChannelBufferVtbl ServerRpcChannelBufferVtbl =
324 RpcChannelBuffer_QueryInterface,
325 RpcChannelBuffer_AddRef,
326 ServerRpcChannelBuffer_Release,
327 ServerRpcChannelBuffer_GetBuffer,
328 RpcChannelBuffer_SendReceive,
329 ServerRpcChannelBuffer_FreeBuffer,
330 RpcChannelBuffer_GetDestCtx,
331 RpcChannelBuffer_IsConnected
334 /* returns a channel buffer for proxies */
335 HRESULT RPC_CreateClientChannel(const OXID *oxid, const IPID *ipid, IRpcChannelBuffer **chan)
337 ClientRpcChannelBuffer *This;
339 RPC_BINDING_HANDLE bind;
341 LPWSTR string_binding;
343 /* connect to the apartment listener thread */
344 get_rpc_endpoint(endpoint, oxid);
346 TRACE("proxy pipe: connecting to endpoint: %s\n", debugstr_w(endpoint));
348 status = RpcStringBindingComposeW(
356 if (status == RPC_S_OK)
358 status = RpcBindingFromStringBindingW(string_binding, &bind);
360 if (status == RPC_S_OK)
362 IPID ipid2 = *ipid; /* why can't RpcBindingSetObject take a const? */
363 status = RpcBindingSetObject(bind, &ipid2);
364 if (status != RPC_S_OK)
365 RpcBindingFree(&bind);
368 RpcStringFreeW(&string_binding);
371 if (status != RPC_S_OK)
373 ERR("Couldn't get binding for endpoint %s, status = %ld\n", debugstr_w(endpoint), status);
374 return HRESULT_FROM_WIN32(status);
377 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
380 RpcBindingFree(&bind);
381 return E_OUTOFMEMORY;
384 This->super.lpVtbl = &ClientRpcChannelBufferVtbl;
385 This->super.refs = 1;
388 *chan = (IRpcChannelBuffer*)This;
393 HRESULT RPC_CreateServerChannel(IRpcChannelBuffer **chan)
395 RpcChannelBuffer *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
397 return E_OUTOFMEMORY;
399 This->lpVtbl = &ServerRpcChannelBufferVtbl;
402 *chan = (IRpcChannelBuffer*)This;
408 HRESULT RPC_ExecuteCall(struct dispatch_params *params)
410 HRESULT hr = IRpcStubBuffer_Invoke(params->stub, params->msg, params->chan);
411 IRpcStubBuffer_Release(params->stub);
412 if (params->handle) SetEvent(params->handle);
416 static void __RPC_STUB dispatch_rpc(RPC_MESSAGE *msg)
418 struct dispatch_params *params;
419 IRpcStubBuffer *stub;
423 RpcBindingInqObject(msg->Handle, &ipid);
425 TRACE("ipid = %s, iMethod = %d\n", debugstr_guid(&ipid), msg->ProcNum);
427 params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*params));
428 if (!params) return RpcRaiseException(E_OUTOFMEMORY);
430 stub = ipid_to_apt_and_stubbuffer(&ipid, &apt);
433 if (apt) apartment_release(apt);
434 /* ipid_to_apt_and_stubbuffer will already have logged the error */
435 return RpcRaiseException(RPC_E_DISCONNECTED);
438 params->msg = (RPCOLEMESSAGE *)msg;
440 params->chan = NULL; /* FIXME: pass server channel */
441 params->status = RPC_S_OK;
443 /* Note: this is the important difference between STAs and MTAs - we
444 * always execute RPCs to STAs in the thread that originally created the
445 * apartment (i.e. the one that pumps messages to the window) */
446 if (apt->model & COINIT_APARTMENTTHREADED)
448 params->handle = CreateEventW(NULL, FALSE, FALSE, NULL);
450 TRACE("Calling apartment thread 0x%08lx...\n", apt->tid);
452 PostMessageW(apt->win, DM_EXECUTERPC, 0, (LPARAM)params);
453 WaitForSingleObject(params->handle, INFINITE);
454 CloseHandle(params->handle);
457 RPC_ExecuteCall(params);
459 HeapFree(GetProcessHeap(), 0, params);
461 apartment_release(apt);
464 /* stub registration */
465 HRESULT RPC_RegisterInterface(REFIID riid)
467 struct registered_if *rif;
471 TRACE("(%s)\n", debugstr_guid(riid));
473 EnterCriticalSection(&csRegIf);
474 LIST_FOR_EACH_ENTRY(rif, ®istered_interfaces, struct registered_if, entry)
476 if (IsEqualGUID(&rif->If.InterfaceId.SyntaxGUID, riid))
485 TRACE("Creating new interface\n");
487 rif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rif));
493 rif->If.Length = sizeof(RPC_SERVER_INTERFACE);
494 /* RPC interface ID = COM interface ID */
495 rif->If.InterfaceId.SyntaxGUID = *riid;
496 rif->If.DispatchTable = &rpc_dispatch;
497 /* all other fields are 0, including the version asCOM objects
498 * always have a version of 0.0 */
499 status = RpcServerRegisterIfEx(
500 (RPC_IF_HANDLE)&rif->If,
502 RPC_IF_OLE | RPC_IF_AUTOLISTEN,
503 RPC_C_LISTEN_MAX_CALLS_DEFAULT,
505 if (status == RPC_S_OK)
506 list_add_tail(®istered_interfaces, &rif->entry);
509 ERR("RpcServerRegisterIfEx failed with error %ld\n", status);
510 HeapFree(GetProcessHeap(), 0, rif);
511 hr = HRESULT_FROM_WIN32(status);
517 LeaveCriticalSection(&csRegIf);
521 /* stub unregistration */
522 void RPC_UnregisterInterface(REFIID riid)
524 struct registered_if *rif;
525 EnterCriticalSection(&csRegIf);
526 LIST_FOR_EACH_ENTRY(rif, ®istered_interfaces, struct registered_if, entry)
528 if (IsEqualGUID(&rif->If.InterfaceId.SyntaxGUID, riid))
532 #if 0 /* this is a stub in builtin and spams the console with FIXME's */
533 IID iid = *riid; /* RpcServerUnregisterIf doesn't take const IID */
534 RpcServerUnregisterIf((RPC_IF_HANDLE)&rif->If, &iid, 0);
535 list_remove(&rif->entry);
536 HeapFree(GetProcessHeap(), 0, rif);
542 LeaveCriticalSection(&csRegIf);
545 /* make the apartment reachable by other threads and processes and create the
546 * IRemUnknown object */
547 void RPC_StartRemoting(struct apartment *apt)
549 if (!InterlockedExchange(&apt->remoting_started, TRUE))
554 get_rpc_endpoint(endpoint, &apt->oxid);
556 status = RpcServerUseProtseqEpW(
558 RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
561 if (status != RPC_S_OK)
562 ERR("Couldn't register endpoint %s\n", debugstr_w(endpoint));
564 /* FIXME: move remote unknown exporting into this function */
566 start_apartment_remote_unknown();
570 static HRESULT create_server(REFCLSID rclsid)
572 static const WCHAR embedding[] = { ' ', '-','E','m','b','e','d','d','i','n','g',0 };
575 HRESULT hres = E_UNEXPECTED;
577 WCHAR exe[MAX_PATH+1];
578 DWORD exelen = sizeof(exe);
579 WCHAR command[MAX_PATH+sizeof(embedding)/sizeof(WCHAR)];
581 PROCESS_INFORMATION pinfo;
583 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
585 sprintf(buf,"CLSID\\%s\\LocalServer32",xclsid);
586 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, buf, 0, KEY_READ, &key);
588 if (hres != ERROR_SUCCESS) {
589 WARN("CLSID %s not registered as LocalServer32\n", xclsid);
590 return REGDB_E_READREGDB; /* Probably */
593 memset(exe,0,sizeof(exe));
594 hres= RegQueryValueExW(key, NULL, NULL, NULL, (LPBYTE)exe, &exelen);
597 WARN("No default value for LocalServer32 key\n");
598 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
601 memset(&sinfo,0,sizeof(sinfo));
602 sinfo.cb = sizeof(sinfo);
604 /* EXE servers are started with the -Embedding switch. MSDN also claims /Embedding is used,
605 * 9x does -Embedding, perhaps an 9x/NT difference?
608 strcpyW(command, exe);
609 strcatW(command, embedding);
611 TRACE("activating local server '%s' for %s\n", debugstr_w(command), xclsid);
613 if (!CreateProcessW(exe, command, NULL, NULL, FALSE, 0, NULL, NULL, &sinfo, &pinfo)) {
614 WARN("failed to run local server %s\n", debugstr_w(exe));
615 return HRESULT_FROM_WIN32(GetLastError());
617 CloseHandle(pinfo.hProcess);
618 CloseHandle(pinfo.hThread);
624 * start_local_service() - start a service given its name and parameters
626 static DWORD start_local_service(LPCWSTR name, DWORD num, LPWSTR *params)
628 SC_HANDLE handle, hsvc;
629 DWORD r = ERROR_FUNCTION_FAILED;
631 TRACE("Starting service %s %ld params\n", debugstr_w(name), num);
633 handle = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
636 hsvc = OpenServiceW(handle, name, SC_MANAGER_ALL_ACCESS);
639 if(StartServiceW(hsvc, num, (LPCWSTR*)params))
643 if (r == ERROR_SERVICE_ALREADY_RUNNING)
645 CloseServiceHandle(hsvc);
647 CloseServiceHandle(handle);
649 TRACE("StartService returned error %ld (%s)\n", r, r?"ok":"failed");
655 * create_local_service() - start a COM server in a service
657 * To start a Local Service, we read the AppID value under
658 * the class's CLSID key, then open the HKCR\\AppId key specified
659 * there and check for a LocalService value.
661 * Note: Local Services are not supported under Windows 9x
663 static HRESULT create_local_service(REFCLSID rclsid)
665 HRESULT hres = REGDB_E_READREGDB;
666 WCHAR buf[40], keyname[50];
667 static const WCHAR szClsId[] = { 'C','L','S','I','D','\\',0 };
668 static const WCHAR szAppId[] = { 'A','p','p','I','d',0 };
669 static const WCHAR szAppIdKey[] = { 'A','p','p','I','d','\\',0 };
670 static const WCHAR szLocalService[] = { 'L','o','c','a','l','S','e','r','v','i','c','e',0 };
671 static const WCHAR szServiceParams[] = {'S','e','r','v','i','c','e','P','a','r','a','m','s',0};
676 TRACE("Attempting to start Local service for %s\n", debugstr_guid(rclsid));
678 /* read the AppID value under the class's key */
679 strcpyW(keyname,szClsId);
680 StringFromGUID2(rclsid,&keyname[6],39);
681 r = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &hkey);
682 if (r!=ERROR_SUCCESS)
685 r = RegQueryValueExW(hkey, szAppId, NULL, &type, (LPBYTE)buf, &sz);
687 if (r!=ERROR_SUCCESS || type!=REG_SZ)
690 /* read the LocalService and ServiceParameters values from the AppID key */
691 strcpyW(keyname, szAppIdKey);
692 strcatW(keyname, buf);
693 r = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &hkey);
694 if (r!=ERROR_SUCCESS)
697 r = RegQueryValueExW(hkey, szLocalService, NULL, &type, (LPBYTE)buf, &sz);
698 if (r==ERROR_SUCCESS && type==REG_SZ)
701 LPWSTR args[1] = { NULL };
704 * FIXME: I'm not really sure how to deal with the service parameters.
705 * I suspect that the string returned from RegQueryValueExW
706 * should be split into a number of arguments by spaces.
707 * It would make more sense if ServiceParams contained a
708 * REG_MULTI_SZ here, but it's a REG_SZ for the services
709 * that I'm interested in for the moment.
711 r = RegQueryValueExW(hkey, szServiceParams, NULL, &type, NULL, &sz);
712 if (r == ERROR_SUCCESS && type == REG_SZ && sz)
714 args[0] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sz);
716 RegQueryValueExW(hkey, szServiceParams, NULL, &type, (LPBYTE)args[0], &sz);
718 r = start_local_service(buf, num_args, args);
719 if (r==ERROR_SUCCESS)
721 HeapFree(GetProcessHeap(),0,args[0]);
728 #define PIPEPREF "\\\\.\\pipe\\"
730 /* FIXME: should call to rpcss instead */
731 HRESULT RPC_GetLocalClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
736 DWORD res, bufferlen;
737 char marshalbuffer[200];
739 LARGE_INTEGER seekto;
740 ULARGE_INTEGER newpos;
743 static const int MAXTRIES = 30; /* 30 seconds */
745 TRACE("rclsid=%s, iid=%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
747 strcpy(pipefn,PIPEPREF);
748 WINE_StringFromCLSID(rclsid,pipefn+strlen(PIPEPREF));
750 while (tries++ < MAXTRIES) {
751 TRACE("waiting for %s\n", pipefn);
753 WaitNamedPipeA( pipefn, NMPWAIT_WAIT_FOREVER );
754 hPipe = CreateFileA(pipefn, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
755 if (hPipe == INVALID_HANDLE_VALUE) {
757 if ( (hres = create_server(rclsid)) &&
758 (hres = create_local_service(rclsid)) )
762 WARN("Connecting to %s, no response yet, retrying: le is %lx\n",pipefn,GetLastError());
768 if (!ReadFile(hPipe,marshalbuffer,sizeof(marshalbuffer),&bufferlen,NULL)) {
769 FIXME("Failed to read marshal id from classfactory of %s.\n",debugstr_guid(rclsid));
773 TRACE("read marshal id from pipe\n");
778 if (tries >= MAXTRIES)
779 return E_NOINTERFACE;
781 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
782 if (hres) return hres;
783 hres = IStream_Write(pStm,marshalbuffer,bufferlen,&res);
785 seekto.u.LowPart = 0;seekto.u.HighPart = 0;
786 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
788 TRACE("unmarshalling classfactory\n");
789 hres = CoUnmarshalInterface(pStm,&IID_IClassFactory,ppv);
791 IStream_Release(pStm);
796 struct local_server_params
802 /* FIXME: should call to rpcss instead */
803 static DWORD WINAPI local_server_thread(LPVOID param)
805 struct local_server_params * lsp = (struct local_server_params *)param;
809 IStream *pStm = lsp->stream;
811 unsigned char *buffer;
813 LARGE_INTEGER seekto;
814 ULARGE_INTEGER newpos;
817 TRACE("Starting threader for %s.\n",debugstr_guid(&lsp->clsid));
819 strcpy(pipefn,PIPEPREF);
820 WINE_StringFromCLSID(&lsp->clsid,pipefn+strlen(PIPEPREF));
822 HeapFree(GetProcessHeap(), 0, lsp);
824 hPipe = CreateNamedPipeA( pipefn, PIPE_ACCESS_DUPLEX,
825 PIPE_TYPE_BYTE|PIPE_WAIT, PIPE_UNLIMITED_INSTANCES,
826 4096, 4096, 500 /* 0.5 second timeout */, NULL );
828 if (hPipe == INVALID_HANDLE_VALUE)
830 FIXME("pipe creation failed for %s, le is %ld\n",pipefn,GetLastError());
835 if (!ConnectNamedPipe(hPipe,NULL)) {
836 ERR("Failure during ConnectNamedPipe %ld, ABORT!\n",GetLastError());
840 TRACE("marshalling IClassFactory to client\n");
842 hres = IStream_Stat(pStm,&ststg,0);
843 if (hres) return hres;
845 buflen = ststg.cbSize.u.LowPart;
846 buffer = HeapAlloc(GetProcessHeap(),0,buflen);
847 seekto.u.LowPart = 0;
848 seekto.u.HighPart = 0;
849 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
851 FIXME("IStream_Seek failed, %lx\n",hres);
855 hres = IStream_Read(pStm,buffer,buflen,&res);
857 FIXME("Stream Read failed, %lx\n",hres);
861 WriteFile(hPipe,buffer,buflen,&res,NULL);
862 FlushFileBuffers(hPipe);
863 DisconnectNamedPipe(hPipe);
865 TRACE("done marshalling IClassFactory\n");
868 IStream_Release(pStm);
872 void RPC_StartLocalServer(REFCLSID clsid, IStream *stream)
876 struct local_server_params *lsp = HeapAlloc(GetProcessHeap(), 0, sizeof(*lsp));
879 lsp->stream = stream;
881 thread = CreateThread(NULL, 0, local_server_thread, lsp, 0, &tid);
883 /* FIXME: failure handling */