4 * Copyright 2002 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
42 #include "wine/unicode.h"
43 #include "wine/winbase16.h"
44 #include "compobj_private.h"
47 #include "compobj_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
53 #define REQTYPE_REQUEST 0
54 typedef struct _wine_rpc_request_header
{
59 } wine_rpc_request_header
;
61 #define REQTYPE_RESPONSE 1
62 typedef struct _wine_rpc_response_header
{
66 } wine_rpc_response_header
;
68 #define REQSTATE_START 0
69 #define REQSTATE_REQ_QUEUED 1
70 #define REQSTATE_REQ_WAITING_FOR_REPLY 2
71 #define REQSTATE_REQ_GOT 3
72 #define REQSTATE_INVOKING 4
73 #define REQSTATE_RESP_QUEUED 5
74 #define REQSTATE_RESP_GOT 6
75 #define REQSTATE_DONE 6
77 typedef struct _wine_rpc_request
{
79 HANDLE hPipe
; /* temp copy of handle */
80 wine_rpc_request_header reqh
;
81 wine_rpc_response_header resph
;
85 static wine_rpc_request
**reqs
= NULL
;
86 static int nrofreqs
= 0;
88 /* This pipe is _thread_ based */
89 typedef struct _wine_pipe
{
90 wine_marshal_id mid
; /* target mid */
91 DWORD tid
; /* thread in which we execute */
96 CRITICAL_SECTION crit
;
99 static wine_pipe
*pipes
= NULL
;
100 static int nrofpipes
= 0;
102 typedef struct _PipeBuf
{
103 ICOM_VTABLE(IRpcChannelBuffer
) *lpVtbl
;
110 static HRESULT WINAPI
111 _xread(HANDLE hf
, LPVOID ptr
, DWORD size
) {
113 if (!ReadFile(hf
,ptr
,size
,&res
,NULL
)) {
114 FIXME("Failed to read from %p, le is %lx\n",hf
,GetLastError());
118 FIXME("Read only %ld of %ld bytes from %p.\n",res
,size
,hf
);
127 static int nrofreaders
= 0;
131 memset(states
,0,sizeof(states
));
132 for (i
=nrofreqs
;i
--;)
133 states
[reqs
[i
]->state
]++;
134 FIXME("%lx/%s/%d: rq %d, w %d, rg %d, rsq %d, rsg %d, d %d\n",
135 GetCurrentProcessId(),
138 states
[REQSTATE_REQ_QUEUED
],
139 states
[REQSTATE_REQ_WAITING_FOR_REPLY
],
140 states
[REQSTATE_REQ_GOT
],
141 states
[REQSTATE_RESP_QUEUED
],
142 states
[REQSTATE_RESP_GOT
],
143 states
[REQSTATE_DONE
]
150 static HRESULT WINAPI
151 _xwrite(HANDLE hf
, LPVOID ptr
, DWORD size
) {
153 if (!WriteFile(hf
,ptr
,size
,&res
,NULL
)) {
154 FIXME("Failed to write to %p, le is %lx\n",hf
,GetLastError());
158 FIXME("Wrote only %ld of %ld bytes to %p.\n",res
,size
,hf
);
164 static DWORD WINAPI
_StubReaderThread(LPVOID
);
167 PIPE_RegisterPipe(wine_marshal_id
*mid
, HANDLE hPipe
, BOOL startreader
) {
170 wine_pipe
*new_pipes
;
172 for (i
=0;i
<nrofpipes
;i
++)
173 if (pipes
[i
].mid
.processid
==mid
->processid
)
176 new_pipes
=(wine_pipe
*)HeapReAlloc(GetProcessHeap(),0,pipes
,sizeof(pipes
[0])*(nrofpipes
+1));
178 new_pipes
=(wine_pipe
*)HeapAlloc(GetProcessHeap(),0,sizeof(pipes
[0]));
179 if (!new_pipes
) return E_OUTOFMEMORY
;
181 sprintf(pipefn
,OLESTUBMGR
"_%08lx",mid
->processid
);
182 memcpy(&(pipes
[nrofpipes
].mid
),mid
,sizeof(*mid
));
183 pipes
[nrofpipes
].hPipe
= hPipe
;
184 InitializeCriticalSection(&(pipes
[nrofpipes
].crit
));
187 pipes
[nrofpipes
-1].hThread
= CreateThread(NULL
,0,_StubReaderThread
,(LPVOID
)(pipes
+(nrofpipes
-1)),0,&(pipes
[nrofpipes
-1].tid
));
189 pipes
[nrofpipes
-1].tid
= GetCurrentThreadId();
195 PIPE_FindByMID(wine_marshal_id
*mid
) {
197 for (i
=0;i
<nrofpipes
;i
++)
198 if ((pipes
[i
].mid
.processid
==mid
->processid
) &&
199 (GetCurrentThreadId()==pipes
[i
].tid
)
201 return pipes
[i
].hPipe
;
202 return INVALID_HANDLE_VALUE
;
206 PIPE_GetFromMID(wine_marshal_id
*mid
) {
208 for (i
=0;i
<nrofpipes
;i
++) {
209 if ((pipes
[i
].mid
.processid
==mid
->processid
) &&
210 (GetCurrentThreadId()==pipes
[i
].tid
)
218 RPC_GetRequest(wine_rpc_request
**req
) {
219 static int reqid
= 0xdeadbeef;
222 for (i
=0;i
<nrofreqs
;i
++) { /* try to reuse */
223 if (reqs
[i
]->state
== REQSTATE_DONE
) {
224 reqs
[i
]->reqh
.reqid
= reqid
++;
225 reqs
[i
]->resph
.reqid
= reqs
[i
]->reqh
.reqid
;
226 reqs
[i
]->hPipe
= INVALID_HANDLE_VALUE
;
228 reqs
[i
]->state
= REQSTATE_START
;
234 reqs
= (wine_rpc_request
**)HeapReAlloc(
238 sizeof(wine_rpc_request
*)*(nrofreqs
+1)
241 reqs
= (wine_rpc_request
**)HeapAlloc(
244 sizeof(wine_rpc_request
*)
247 return E_OUTOFMEMORY
;
248 reqs
[nrofreqs
] = (wine_rpc_request
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(wine_rpc_request
));
249 reqs
[nrofreqs
]->reqh
.reqid
= reqid
++;
250 reqs
[nrofreqs
]->resph
.reqid
= reqs
[nrofreqs
]->reqh
.reqid
;
251 reqs
[nrofreqs
]->hPipe
= INVALID_HANDLE_VALUE
;
252 *req
= reqs
[nrofreqs
];
253 reqs
[nrofreqs
]->state
= REQSTATE_START
;
259 RPC_FreeRequest(wine_rpc_request
*req
) {
260 req
->state
= REQSTATE_DONE
; /* Just reuse slot. */
264 static HRESULT WINAPI
265 PipeBuf_QueryInterface(
266 LPRPCCHANNELBUFFER iface
,REFIID riid
,LPVOID
*ppv
269 if (IsEqualIID(riid
,&IID_IRpcChannelBuffer
) || IsEqualIID(riid
,&IID_IUnknown
)) {
270 *ppv
= (LPVOID
)iface
;
271 IUnknown_AddRef(iface
);
274 return E_NOINTERFACE
;
278 PipeBuf_AddRef(LPRPCCHANNELBUFFER iface
) {
279 ICOM_THIS(PipeBuf
,iface
);
285 PipeBuf_Release(LPRPCCHANNELBUFFER iface
) {
286 ICOM_THIS(PipeBuf
,iface
);
290 ERR("Free all stuff.\n");
291 HeapFree(GetProcessHeap(),0,This
);
295 static HRESULT WINAPI
297 LPRPCCHANNELBUFFER iface
,RPCOLEMESSAGE
* msg
,REFIID riid
299 /*ICOM_THIS(PipeBuf,iface);*/
301 TRACE("(%p,%s), slightly wrong.\n",msg
,debugstr_guid(riid
));
302 /* probably reuses IID in real. */
303 if (msg
->cbBuffer
&& (msg
->Buffer
== NULL
))
304 msg
->Buffer
= HeapAlloc(GetProcessHeap(),0,msg
->cbBuffer
);
309 _invoke_onereq(wine_rpc_request
*req
) {
310 IRpcStubBuffer
*stub
;
315 hres
= MARSHAL_Find_Stub_Buffer(&(req
->reqh
.mid
),&stub
);
317 ERR("Stub not found?\n");
320 msg
.Buffer
= req
->Buffer
;
321 msg
.iMethod
= req
->reqh
.iMethod
;
322 msg
.cbBuffer
= req
->reqh
.cbBuffer
;
323 req
->state
= REQSTATE_INVOKING
;
324 req
->resph
.retval
= IRpcStubBuffer_Invoke(stub
,&msg
,NULL
);
325 IUnknown_Release(stub
);
326 req
->Buffer
= msg
.Buffer
;
327 req
->resph
.cbBuffer
= msg
.cbBuffer
;
328 reqtype
= REQTYPE_RESPONSE
;
329 hres
= _xwrite(req
->hPipe
,&reqtype
,sizeof(reqtype
));
330 if (hres
) return hres
;
331 hres
= _xwrite(req
->hPipe
,&(req
->resph
),sizeof(req
->resph
));
332 if (hres
) return hres
;
333 hres
= _xwrite(req
->hPipe
,req
->Buffer
,req
->resph
.cbBuffer
);
334 if (hres
) return hres
;
335 req
->state
= REQSTATE_DONE
;
340 static HRESULT
_read_one(wine_pipe
*xpipe
);
343 RPC_QueueRequestAndWait(wine_rpc_request
*req
) {
345 wine_rpc_request
*xreq
;
348 wine_pipe
*xpipe
= PIPE_GetFromMID(&(req
->reqh
.mid
));
351 FIXME("no pipe found.\n");
354 if (GetCurrentProcessId() == req
->reqh
.mid
.processid
) {
355 ERR("In current process?\n");
358 req
->hPipe
= xpipe
->hPipe
;
359 req
->state
= REQSTATE_REQ_WAITING_FOR_REPLY
;
360 reqtype
= REQTYPE_REQUEST
;
361 hres
= _xwrite(req
->hPipe
,&reqtype
,sizeof(reqtype
));
362 if (hres
) return hres
;
363 hres
= _xwrite(req
->hPipe
,&(req
->reqh
),sizeof(req
->reqh
));
364 if (hres
) return hres
;
365 hres
= _xwrite(req
->hPipe
,req
->Buffer
,req
->reqh
.cbBuffer
);
366 if (hres
) return hres
;
369 /*WaitForSingleObject(hRpcChanged,INFINITE);*/
370 hres
= _read_one(xpipe
);
373 for (i
=0;i
<nrofreqs
;i
++) {
375 if ((xreq
->state
==REQSTATE_REQ_GOT
) && (xreq
->hPipe
==req
->hPipe
)) {
376 _invoke_onereq(xreq
);
379 if (req
->state
== REQSTATE_RESP_GOT
)
385 static HRESULT WINAPI
387 LPRPCCHANNELBUFFER iface
,RPCOLEMESSAGE
* msg
,ULONG
*status
389 ICOM_THIS(PipeBuf
,iface
);
390 wine_rpc_request
*req
;
395 if (This
->mid
.processid
== GetCurrentProcessId()) {
396 ERR("Need to call directly!\n");
400 hres
= RPC_GetRequest(&req
);
401 if (hres
) return hres
;
402 req
->reqh
.iMethod
= msg
->iMethod
;
403 req
->reqh
.cbBuffer
= msg
->cbBuffer
;
404 memcpy(&(req
->reqh
.mid
),&(This
->mid
),sizeof(This
->mid
));
405 req
->Buffer
= msg
->Buffer
;
406 hres
= RPC_QueueRequestAndWait(req
);
408 RPC_FreeRequest(req
);
411 msg
->cbBuffer
= req
->resph
.cbBuffer
;
412 msg
->Buffer
= req
->Buffer
;
413 *status
= req
->resph
.retval
;
414 RPC_FreeRequest(req
);
419 static HRESULT WINAPI
420 PipeBuf_FreeBuffer(LPRPCCHANNELBUFFER iface
,RPCOLEMESSAGE
* msg
) {
421 FIXME("(%p), stub!\n",msg
);
425 static HRESULT WINAPI
427 LPRPCCHANNELBUFFER iface
,DWORD
* pdwDestContext
,void** ppvDestContext
429 FIXME("(%p,%p), stub!\n",pdwDestContext
,ppvDestContext
);
433 static HRESULT WINAPI
434 PipeBuf_IsConnected(LPRPCCHANNELBUFFER iface
) {
435 FIXME("(), stub!\n");
439 static ICOM_VTABLE(IRpcChannelBuffer
) pipebufvt
= {
440 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
441 PipeBuf_QueryInterface
,
452 PIPE_GetNewPipeBuf(wine_marshal_id
*mid
, IRpcChannelBuffer
**pipebuf
) {
453 wine_marshal_id ourid
;
459 hPipe
= PIPE_FindByMID(mid
);
460 if (hPipe
== INVALID_HANDLE_VALUE
) {
462 sprintf(pipefn
,OLESTUBMGR
"_%08lx",mid
->processid
);
465 GENERIC_READ
|GENERIC_WRITE
,
472 if (hPipe
== INVALID_HANDLE_VALUE
) {
473 FIXME("Could not open named pipe %s, le is %lx\n",pipefn
,GetLastError());
476 hres
= PIPE_RegisterPipe(mid
, hPipe
, FALSE
);
477 if (hres
) return hres
;
478 memset(&ourid
,0,sizeof(ourid
));
479 ourid
.processid
= GetCurrentProcessId();
480 if (!WriteFile(hPipe
,&ourid
,sizeof(ourid
),&res
,NULL
)||(res
!=sizeof(ourid
))) {
481 ERR("Failed writing startup mid!\n");
485 pbuf
= (PipeBuf
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(PipeBuf
));
486 pbuf
->lpVtbl
= &pipebufvt
;
488 memcpy(&(pbuf
->mid
),mid
,sizeof(*mid
));
489 *pipebuf
= (IRpcChannelBuffer
*)pbuf
;
494 create_server(REFCLSID rclsid
) {
495 static const WCHAR embedding
[] = { ' ', '-','E','m','b','e','d','d','i','n','g',0 };
498 HRESULT hres
= E_UNEXPECTED
;
500 WCHAR exe
[MAX_PATH
+1];
501 DWORD exelen
= sizeof(exe
);
502 WCHAR command
[MAX_PATH
+sizeof(embedding
)/sizeof(WCHAR
)];
504 PROCESS_INFORMATION pinfo
;
506 WINE_StringFromCLSID((LPCLSID
)rclsid
,xclsid
);
508 sprintf(buf
,"CLSID\\%s\\LocalServer32",xclsid
);
509 hres
= RegOpenKeyExA(HKEY_CLASSES_ROOT
, buf
, 0, KEY_READ
, &key
);
511 if (hres
!= ERROR_SUCCESS
) {
512 WARN("CLSID %s not registered as LocalServer32\n", xclsid
);
513 return REGDB_E_READREGDB
; /* Probably */
516 memset(exe
,0,sizeof(exe
));
517 hres
= RegQueryValueExW(key
, NULL
, NULL
, NULL
, (LPBYTE
)exe
, &exelen
);
520 WARN("No default value for LocalServer32 key\n");
521 return REGDB_E_CLASSNOTREG
; /* FIXME: check retval */
524 memset(&sinfo
,0,sizeof(sinfo
));
525 sinfo
.cb
= sizeof(sinfo
);
527 /* EXE servers are started with the -Embedding switch. MSDN also claims /Embedding is used,
528 9x does -Embedding, perhaps an 9x/NT difference? */
530 strcpyW(command
, exe
);
531 strcatW(command
, embedding
);
533 TRACE("activating local server '%s' for %s\n", debugstr_w(command
), xclsid
);
535 if (!CreateProcessW(exe
, command
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &sinfo
, &pinfo
)) {
536 WARN("failed to run local server %s\n", debugstr_w(exe
));
542 /* http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp, Figure 4 */
543 HRESULT
create_marshalled_proxy(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
) {
548 char marshalbuffer
[200];
550 LARGE_INTEGER seekto
;
551 ULARGE_INTEGER newpos
;
553 #define MAXTRIES 10000
555 TRACE("rclsid=%s, iid=%s\n", debugstr_guid(rclsid
), debugstr_guid(iid
));
557 strcpy(pipefn
,PIPEPREF
);
558 WINE_StringFromCLSID(rclsid
,pipefn
+strlen(PIPEPREF
));
560 while (tries
++<MAXTRIES
) {
561 WaitNamedPipeA( pipefn
, NMPWAIT_WAIT_FOREVER
);
564 GENERIC_READ
|GENERIC_WRITE
,
571 if (hPipe
== INVALID_HANDLE_VALUE
) {
573 if ((hres
= create_server(rclsid
)))
577 WARN("Could not open named pipe to broker %s, le is %lx\n",pipefn
,GetLastError());
583 if (!ReadFile(hPipe
,marshalbuffer
,sizeof(marshalbuffer
),&bufferlen
,NULL
)) {
584 FIXME("Failed to read marshal id from classfactory of %s.\n",debugstr_guid(rclsid
));
592 return E_NOINTERFACE
;
593 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
594 if (hres
) return hres
;
595 hres
= IStream_Write(pStm
,marshalbuffer
,bufferlen
,&res
);
597 seekto
.u
.LowPart
= 0;seekto
.u
.HighPart
= 0;
598 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
599 hres
= CoUnmarshalInterface(pStm
,&IID_IClassFactory
,ppv
);
601 IStream_Release(pStm
);
607 PIPE_StartRequestThread(HANDLE xhPipe
) {
608 wine_marshal_id remoteid
;
611 hres
= _xread(xhPipe
,&remoteid
,sizeof(remoteid
));
613 ERR("Failed to read remote mid!\n");
616 PIPE_RegisterPipe(&remoteid
,xhPipe
, TRUE
);
620 _read_one(wine_pipe
*xpipe
) {
623 HANDLE xhPipe
= xpipe
->hPipe
;
625 /*FIXME("%lx %d reading reqtype\n",GetCurrentProcessId(),xhPipe);*/
626 hres
= _xread(xhPipe
,&reqtype
,sizeof(reqtype
));
628 EnterCriticalSection(&(xpipe
->crit
));
629 /*FIXME("%lx got reqtype %ld\n",GetCurrentProcessId(),reqtype);*/
631 if (reqtype
== REQTYPE_REQUEST
) {
632 wine_rpc_request
*xreq
;
633 RPC_GetRequest(&xreq
);
634 xreq
->hPipe
= xhPipe
;
635 hres
= _xread(xhPipe
,&(xreq
->reqh
),sizeof(xreq
->reqh
));
637 xreq
->resph
.reqid
= xreq
->reqh
.reqid
;
638 xreq
->Buffer
= HeapAlloc(GetProcessHeap(),0, xreq
->reqh
.cbBuffer
);
639 hres
= _xread(xhPipe
,xreq
->Buffer
,xreq
->reqh
.cbBuffer
);
641 xreq
->state
= REQSTATE_REQ_GOT
;
644 if (reqtype
== REQTYPE_RESPONSE
) {
645 wine_rpc_response_header resph
;
648 hres
= _xread(xhPipe
,&resph
,sizeof(resph
));
650 for (i
=nrofreqs
;i
--;) {
651 wine_rpc_request
*xreq
= reqs
[i
];
652 if (xreq
->state
!= REQSTATE_REQ_WAITING_FOR_REPLY
)
654 if (xreq
->reqh
.reqid
== resph
.reqid
) {
655 memcpy(&(xreq
->resph
),&resph
,sizeof(resph
));
658 xreq
->Buffer
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,xreq
->Buffer
,xreq
->resph
.cbBuffer
);
660 xreq
->Buffer
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,xreq
->resph
.cbBuffer
);
662 hres
= _xread(xhPipe
,xreq
->Buffer
,xreq
->resph
.cbBuffer
);
664 xreq
->state
= REQSTATE_RESP_GOT
;
665 /*PulseEvent(hRpcChanged);*/
669 ERR("Did not find request for id %lx\n",resph
.reqid
);
673 ERR("Unknown reqtype %ld\n",reqtype
);
676 LeaveCriticalSection(&(xpipe
->crit
));
681 _StubReaderThread(LPVOID param
) {
682 wine_pipe
*xpipe
= (wine_pipe
*)param
;
683 HANDLE xhPipe
= xpipe
->hPipe
;
686 TRACE("STUB reader thread %lx\n",GetCurrentProcessId());
689 hres
= _read_one(xpipe
);
692 for (i
=nrofreqs
;i
--;) {
693 wine_rpc_request
*xreq
= reqs
[i
];
694 if ((xreq
->state
== REQSTATE_REQ_GOT
) && (xreq
->hPipe
== xhPipe
)) {
695 _invoke_onereq(xreq
);
699 FIXME("Failed with hres %lx\n",hres
);
705 _StubMgrThread(LPVOID param
) {
709 sprintf(pipefn
,OLESTUBMGR
"_%08lx",GetCurrentProcessId());
710 TRACE("Stub Manager Thread starting on (%s)\n",pipefn
);
713 listenPipe
= CreateNamedPipeA(
716 PIPE_TYPE_BYTE
|PIPE_WAIT
,
717 PIPE_UNLIMITED_INSTANCES
,
720 NMPWAIT_USE_DEFAULT_WAIT
,
723 if (listenPipe
== INVALID_HANDLE_VALUE
) {
724 FIXME("pipe creation failed for %s, le is %lx\n",pipefn
,GetLastError());
725 return 1; /* permanent failure, so quit stubmgr thread */
727 if (!ConnectNamedPipe(listenPipe
,NULL
)) {
728 ERR("Failure during ConnectNamedPipe %lx!\n",GetLastError());
729 CloseHandle(listenPipe
);
732 PIPE_StartRequestThread(listenPipe
);
739 static BOOL stubMgrRunning
= FALSE
;
742 if (!stubMgrRunning
) {
743 stubMgrRunning
= TRUE
;
744 CreateThread(NULL
,0,_StubMgrThread
,NULL
,0,&tid
);
745 Sleep(2000); /* actually we just try opening the pipe until it succeeds */