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
37 #include "wine/unicode.h"
38 #include "wine/winbase16.h"
39 #include "compobj_private.h"
42 #include "compobj_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
48 typedef struct _wine_rpc_request
{
50 HANDLE hPipe
; /* temp copy of handle */
51 wine_rpc_request_header reqh
;
52 wine_rpc_response_header resph
;
56 static wine_rpc_request
**reqs
= NULL
;
57 static int nrofreqs
= 0;
59 /* This pipe is _thread_ based */
60 typedef struct _wine_pipe
{
61 wine_marshal_id mid
; /* target mid */
62 DWORD tid
; /* thread in which we execute */
67 CRITICAL_SECTION crit
;
70 static wine_pipe
*pipes
= NULL
;
71 static int nrofpipes
= 0;
73 typedef struct _PipeBuf
{
74 ICOM_VTABLE(IRpcChannelBuffer
) *lpVtbl
;
81 static int nrofreaders
= 0;
84 _xread(HANDLE hf
, LPVOID ptr
, DWORD size
) {
86 if (!ReadFile(hf
,ptr
,size
,&res
,NULL
)) {
87 FIXME("Failed to read from %p, le is %lx\n",hf
,GetLastError());
91 FIXME("Read only %ld of %ld bytes from %p.\n",res
,size
,hf
);
103 memset(states
,0,sizeof(states
));
104 for (i
=nrofreqs
;i
--;)
105 states
[reqs
[i
]->state
]++;
106 FIXME("%lx/%s/%d: rq %d, w %d, rg %d, rsq %d, rsg %d, d %d\n",
107 GetCurrentProcessId(),
110 states
[REQSTATE_REQ_QUEUED
],
111 states
[REQSTATE_REQ_WAITING_FOR_REPLY
],
112 states
[REQSTATE_REQ_GOT
],
113 states
[REQSTATE_RESP_QUEUED
],
114 states
[REQSTATE_RESP_GOT
],
115 states
[REQSTATE_DONE
]
119 static HRESULT WINAPI
120 _xwrite(HANDLE hf
, LPVOID ptr
, DWORD size
) {
122 if (!WriteFile(hf
,ptr
,size
,&res
,NULL
)) {
123 FIXME("Failed to write to %p, le is %lx\n",hf
,GetLastError());
127 FIXME("Wrote only %ld of %ld bytes to %p.\n",res
,size
,hf
);
133 static DWORD WINAPI
_StubReaderThread(LPVOID
);
136 PIPE_RegisterPipe(wine_marshal_id
*mid
, HANDLE hPipe
, BOOL startreader
) {
139 wine_pipe
*new_pipes
;
141 for (i
=0;i
<nrofpipes
;i
++)
142 if (pipes
[i
].mid
.processid
==mid
->processid
)
145 new_pipes
=(wine_pipe
*)HeapReAlloc(GetProcessHeap(),0,pipes
,sizeof(pipes
[0])*(nrofpipes
+1));
147 new_pipes
=(wine_pipe
*)HeapAlloc(GetProcessHeap(),0,sizeof(pipes
[0]));
148 if (!new_pipes
) return E_OUTOFMEMORY
;
150 sprintf(pipefn
,OLESTUBMGR
"_%08lx",mid
->processid
);
151 memcpy(&(pipes
[nrofpipes
].mid
),mid
,sizeof(*mid
));
152 pipes
[nrofpipes
].hPipe
= hPipe
;
153 InitializeCriticalSection(&(pipes
[nrofpipes
].crit
));
156 pipes
[nrofpipes
-1].hThread
= CreateThread(NULL
,0,_StubReaderThread
,(LPVOID
)(pipes
+(nrofpipes
-1)),0,&(pipes
[nrofpipes
-1].tid
));
158 pipes
[nrofpipes
-1].tid
= GetCurrentThreadId();
164 PIPE_FindByMID(wine_marshal_id
*mid
) {
166 for (i
=0;i
<nrofpipes
;i
++)
167 if ((pipes
[i
].mid
.processid
==mid
->processid
) &&
168 (GetCurrentThreadId()==pipes
[i
].tid
)
170 return pipes
[i
].hPipe
;
171 return INVALID_HANDLE_VALUE
;
175 PIPE_GetFromMID(wine_marshal_id
*mid
) {
177 for (i
=0;i
<nrofpipes
;i
++) {
178 if ((pipes
[i
].mid
.processid
==mid
->processid
) &&
179 (GetCurrentThreadId()==pipes
[i
].tid
)
187 RPC_GetRequest(wine_rpc_request
**req
) {
188 static int reqid
= 0xdeadbeef;
191 for (i
=0;i
<nrofreqs
;i
++) { /* try to reuse */
192 if (reqs
[i
]->state
== REQSTATE_DONE
) {
193 reqs
[i
]->reqh
.reqid
= reqid
++;
194 reqs
[i
]->resph
.reqid
= reqs
[i
]->reqh
.reqid
;
195 reqs
[i
]->hPipe
= INVALID_HANDLE_VALUE
;
197 reqs
[i
]->state
= REQSTATE_START
;
203 reqs
= (wine_rpc_request
**)HeapReAlloc(
207 sizeof(wine_rpc_request
*)*(nrofreqs
+1)
210 reqs
= (wine_rpc_request
**)HeapAlloc(
213 sizeof(wine_rpc_request
*)
216 return E_OUTOFMEMORY
;
217 reqs
[nrofreqs
] = (wine_rpc_request
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(wine_rpc_request
));
218 reqs
[nrofreqs
]->reqh
.reqid
= reqid
++;
219 reqs
[nrofreqs
]->resph
.reqid
= reqs
[nrofreqs
]->reqh
.reqid
;
220 reqs
[nrofreqs
]->hPipe
= INVALID_HANDLE_VALUE
;
221 *req
= reqs
[nrofreqs
];
222 reqs
[nrofreqs
]->state
= REQSTATE_START
;
228 RPC_FreeRequest(wine_rpc_request
*req
) {
229 req
->state
= REQSTATE_DONE
; /* Just reuse slot. */
233 static HRESULT WINAPI
234 PipeBuf_QueryInterface(
235 LPRPCCHANNELBUFFER iface
,REFIID riid
,LPVOID
*ppv
238 if (IsEqualIID(riid
,&IID_IRpcChannelBuffer
) || IsEqualIID(riid
,&IID_IUnknown
)) {
239 *ppv
= (LPVOID
)iface
;
240 IUnknown_AddRef(iface
);
243 return E_NOINTERFACE
;
247 PipeBuf_AddRef(LPRPCCHANNELBUFFER iface
) {
248 ICOM_THIS(PipeBuf
,iface
);
254 PipeBuf_Release(LPRPCCHANNELBUFFER iface
) {
255 ICOM_THIS(PipeBuf
,iface
);
259 ERR("Free all stuff.\n");
260 HeapFree(GetProcessHeap(),0,This
);
264 static HRESULT WINAPI
266 LPRPCCHANNELBUFFER iface
,RPCOLEMESSAGE
* msg
,REFIID riid
268 /*ICOM_THIS(PipeBuf,iface);*/
270 TRACE("(%p,%s), slightly wrong.\n",msg
,debugstr_guid(riid
));
271 /* probably reuses IID in real. */
272 if (msg
->cbBuffer
&& (msg
->Buffer
== NULL
))
273 msg
->Buffer
= HeapAlloc(GetProcessHeap(),0,msg
->cbBuffer
);
278 _invoke_onereq(wine_rpc_request
*req
) {
279 IRpcStubBuffer
*stub
;
284 hres
= MARSHAL_Find_Stub_Buffer(&(req
->reqh
.mid
),&stub
);
286 ERR("Stub not found?\n");
289 msg
.Buffer
= req
->Buffer
;
290 msg
.iMethod
= req
->reqh
.iMethod
;
291 msg
.cbBuffer
= req
->reqh
.cbBuffer
;
292 req
->state
= REQSTATE_INVOKING
;
293 req
->resph
.retval
= IRpcStubBuffer_Invoke(stub
,&msg
,NULL
);
294 req
->Buffer
= msg
.Buffer
;
295 req
->resph
.cbBuffer
= msg
.cbBuffer
;
296 reqtype
= REQTYPE_RESPONSE
;
297 hres
= _xwrite(req
->hPipe
,&reqtype
,sizeof(reqtype
));
298 if (hres
) return hres
;
299 hres
= _xwrite(req
->hPipe
,&(req
->resph
),sizeof(req
->resph
));
300 if (hres
) return hres
;
301 hres
= _xwrite(req
->hPipe
,req
->Buffer
,req
->resph
.cbBuffer
);
302 if (hres
) return hres
;
303 req
->state
= REQSTATE_DONE
;
308 static HRESULT
_read_one(wine_pipe
*xpipe
);
311 RPC_QueueRequestAndWait(wine_rpc_request
*req
) {
313 wine_rpc_request
*xreq
;
316 wine_pipe
*xpipe
= PIPE_GetFromMID(&(req
->reqh
.mid
));
319 FIXME("no pipe found.\n");
322 if (GetCurrentProcessId() == req
->reqh
.mid
.processid
) {
323 ERR("In current process?\n");
326 req
->hPipe
= xpipe
->hPipe
;
327 req
->state
= REQSTATE_REQ_WAITING_FOR_REPLY
;
328 reqtype
= REQTYPE_REQUEST
;
329 hres
= _xwrite(req
->hPipe
,&reqtype
,sizeof(reqtype
));
330 if (hres
) return hres
;
331 hres
= _xwrite(req
->hPipe
,&(req
->reqh
),sizeof(req
->reqh
));
332 if (hres
) return hres
;
333 hres
= _xwrite(req
->hPipe
,req
->Buffer
,req
->reqh
.cbBuffer
);
334 if (hres
) return hres
;
337 /*WaitForSingleObject(hRpcChanged,INFINITE);*/
338 hres
= _read_one(xpipe
);
341 for (i
=0;i
<nrofreqs
;i
++) {
343 if ((xreq
->state
==REQSTATE_REQ_GOT
) && (xreq
->hPipe
==req
->hPipe
)) {
344 _invoke_onereq(xreq
);
347 if (req
->state
== REQSTATE_RESP_GOT
)
353 static HRESULT WINAPI
355 LPRPCCHANNELBUFFER iface
,RPCOLEMESSAGE
* msg
,ULONG
*status
357 ICOM_THIS(PipeBuf
,iface
);
358 wine_rpc_request
*req
;
363 if (This
->mid
.processid
== GetCurrentProcessId()) {
364 ERR("Need to call directly!\n");
368 hres
= RPC_GetRequest(&req
);
369 if (hres
) return hres
;
370 req
->reqh
.iMethod
= msg
->iMethod
;
371 req
->reqh
.cbBuffer
= msg
->cbBuffer
;
372 memcpy(&(req
->reqh
.mid
),&(This
->mid
),sizeof(This
->mid
));
373 req
->Buffer
= msg
->Buffer
;
374 hres
= RPC_QueueRequestAndWait(req
);
376 RPC_FreeRequest(req
);
379 msg
->cbBuffer
= req
->resph
.cbBuffer
;
380 msg
->Buffer
= req
->Buffer
;
381 *status
= req
->resph
.retval
;
382 RPC_FreeRequest(req
);
387 static HRESULT WINAPI
388 PipeBuf_FreeBuffer(LPRPCCHANNELBUFFER iface
,RPCOLEMESSAGE
* msg
) {
389 FIXME("(%p), stub!\n",msg
);
393 static HRESULT WINAPI
395 LPRPCCHANNELBUFFER iface
,DWORD
* pdwDestContext
,void** ppvDestContext
397 FIXME("(%p,%p), stub!\n",pdwDestContext
,ppvDestContext
);
401 static HRESULT WINAPI
402 PipeBuf_IsConnected(LPRPCCHANNELBUFFER iface
) {
403 FIXME("(), stub!\n");
407 static ICOM_VTABLE(IRpcChannelBuffer
) pipebufvt
= {
408 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
409 PipeBuf_QueryInterface
,
420 PIPE_GetNewPipeBuf(wine_marshal_id
*mid
, IRpcChannelBuffer
**pipebuf
) {
421 wine_marshal_id ourid
;
427 hPipe
= PIPE_FindByMID(mid
);
428 if (hPipe
== INVALID_HANDLE_VALUE
) {
430 sprintf(pipefn
,OLESTUBMGR
"_%08lx",mid
->processid
);
433 GENERIC_READ
|GENERIC_WRITE
,
440 if (hPipe
== INVALID_HANDLE_VALUE
) {
441 FIXME("Could not open named pipe %s, le is %lx\n",pipefn
,GetLastError());
444 hres
= PIPE_RegisterPipe(mid
, hPipe
, FALSE
);
445 if (hres
) return hres
;
446 memset(&ourid
,0,sizeof(ourid
));
447 ourid
.processid
= GetCurrentProcessId();
448 if (!WriteFile(hPipe
,&ourid
,sizeof(ourid
),&res
,NULL
)||(res
!=sizeof(ourid
))) {
449 ERR("Failed writing startup mid!\n");
453 pbuf
= (PipeBuf
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(PipeBuf
));
454 pbuf
->lpVtbl
= &pipebufvt
;
456 memcpy(&(pbuf
->mid
),mid
,sizeof(*mid
));
457 *pipebuf
= (IRpcChannelBuffer
*)pbuf
;
462 create_server(REFCLSID rclsid
) {
465 HRESULT hres
= E_UNEXPECTED
;
467 WCHAR dllName
[MAX_PATH
+1];
468 DWORD dllNameLen
= sizeof(dllName
);
470 PROCESS_INFORMATION pinfo
;
472 WINE_StringFromCLSID((LPCLSID
)rclsid
,xclsid
);
474 sprintf(buf
,"CLSID\\%s\\LocalServer32",xclsid
);
475 hres
= RegOpenKeyExA(HKEY_CLASSES_ROOT
, buf
, 0, KEY_READ
, &key
);
477 if (hres
!= ERROR_SUCCESS
)
478 return REGDB_E_READREGDB
; /* Probably */
480 memset(dllName
,0,sizeof(dllName
));
481 hres
= RegQueryValueExW(key
,NULL
,NULL
,NULL
,(LPBYTE
)dllName
,&dllNameLen
);
484 return REGDB_E_CLASSNOTREG
; /* FIXME: check retval */
485 memset(&sinfo
,0,sizeof(sinfo
));
486 sinfo
.cb
= sizeof(sinfo
);
487 if (!CreateProcessW(NULL
,dllName
,NULL
,NULL
,FALSE
,0,NULL
,NULL
,&sinfo
,&pinfo
))
491 /* http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp, Figure 4 */
492 HRESULT
create_marshalled_proxy(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
) {
497 char marshalbuffer
[200];
499 LARGE_INTEGER seekto
;
500 ULARGE_INTEGER newpos
;
502 #define MAXTRIES 10000
504 strcpy(pipefn
,PIPEPREF
);
505 WINE_StringFromCLSID(rclsid
,pipefn
+strlen(PIPEPREF
));
507 while (tries
++<MAXTRIES
) {
510 GENERIC_READ
|GENERIC_WRITE
,
517 if (hPipe
== INVALID_HANDLE_VALUE
) {
519 if ((hres
= create_server(rclsid
)))
523 WARN("Could not open named pipe to broker %s, le is %lx\n",pipefn
,GetLastError());
529 if (!ReadFile(hPipe
,marshalbuffer
,sizeof(marshalbuffer
),&bufferlen
,NULL
)) {
530 FIXME("Failed to read marshal id from classfactory of %s.\n",debugstr_guid(rclsid
));
538 return E_NOINTERFACE
;
539 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
540 if (hres
) return hres
;
541 hres
= IStream_Write(pStm
,marshalbuffer
,bufferlen
,&res
);
543 seekto
.s
.LowPart
= 0;seekto
.s
.HighPart
= 0;
544 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
545 hres
= CoUnmarshalInterface(pStm
,&IID_IClassFactory
,ppv
);
547 IStream_Release(pStm
);
553 PIPE_StartRequestThread(HANDLE xhPipe
) {
554 wine_marshal_id remoteid
;
557 hres
= _xread(xhPipe
,&remoteid
,sizeof(remoteid
));
559 ERR("Failed to read remote mid!\n");
562 PIPE_RegisterPipe(&remoteid
,xhPipe
, TRUE
);
566 _read_one(wine_pipe
*xpipe
) {
569 HANDLE xhPipe
= xpipe
->hPipe
;
571 /*FIXME("%lx %d reading reqtype\n",GetCurrentProcessId(),xhPipe);*/
572 hres
= _xread(xhPipe
,&reqtype
,sizeof(reqtype
));
574 EnterCriticalSection(&(xpipe
->crit
));
575 /*FIXME("%lx got reqtype %ld\n",GetCurrentProcessId(),reqtype);*/
577 if (reqtype
== REQTYPE_REQUEST
) {
578 wine_rpc_request
*xreq
;
579 RPC_GetRequest(&xreq
);
580 xreq
->hPipe
= xhPipe
;
581 hres
= _xread(xhPipe
,&(xreq
->reqh
),sizeof(xreq
->reqh
));
583 xreq
->resph
.reqid
= xreq
->reqh
.reqid
;
584 xreq
->Buffer
= HeapAlloc(GetProcessHeap(),0, xreq
->reqh
.cbBuffer
);
585 hres
= _xread(xhPipe
,xreq
->Buffer
,xreq
->reqh
.cbBuffer
);
587 xreq
->state
= REQSTATE_REQ_GOT
;
590 if (reqtype
== REQTYPE_RESPONSE
) {
591 wine_rpc_response_header resph
;
594 hres
= _xread(xhPipe
,&resph
,sizeof(resph
));
596 for (i
=nrofreqs
;i
--;) {
597 wine_rpc_request
*xreq
= reqs
[i
];
598 if (xreq
->state
!= REQSTATE_REQ_WAITING_FOR_REPLY
)
600 if (xreq
->reqh
.reqid
== resph
.reqid
) {
601 memcpy(&(xreq
->resph
),&resph
,sizeof(resph
));
602 xreq
->Buffer
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,xreq
->Buffer
,xreq
->resph
.cbBuffer
);
603 hres
= _xread(xhPipe
,xreq
->Buffer
,xreq
->resph
.cbBuffer
);
605 xreq
->state
= REQSTATE_RESP_GOT
;
606 /*PulseEvent(hRpcChanged);*/
610 ERR("Did not find request for id %lx\n",resph
.reqid
);
614 ERR("Unknown reqtype %ld\n",reqtype
);
617 LeaveCriticalSection(&(xpipe
->crit
));
622 _StubReaderThread(LPVOID param
) {
623 wine_pipe
*xpipe
= (wine_pipe
*)param
;
624 HANDLE xhPipe
= xpipe
->hPipe
;
627 TRACE("STUB reader thread %lx\n",GetCurrentProcessId());
630 hres
= _read_one(xpipe
);
633 for (i
=nrofreqs
;i
--;) {
634 wine_rpc_request
*xreq
= reqs
[i
];
635 if ((xreq
->state
== REQSTATE_REQ_GOT
) && (xreq
->hPipe
== xhPipe
)) {
636 _invoke_onereq(xreq
);
640 FIXME("Failed with hres %lx\n",hres
);
646 _StubMgrThread(LPVOID param
) {
650 sprintf(pipefn
,OLESTUBMGR
"_%08lx",GetCurrentProcessId());
651 TRACE("Stub Manager Thread starting on (%s)\n",pipefn
);
653 listenPipe
= CreateNamedPipeA(
656 PIPE_TYPE_BYTE
|PIPE_WAIT
,
657 PIPE_UNLIMITED_INSTANCES
,
660 NMPWAIT_USE_DEFAULT_WAIT
,
663 if (listenPipe
== INVALID_HANDLE_VALUE
) {
664 FIXME("pipe creation failed for %s, le is %lx\n",pipefn
,GetLastError());
665 return 1; /* permanent failure, so quit stubmgr thread */
669 if (!ConnectNamedPipe(listenPipe
,NULL
)) {
670 ERR("Failure during ConnectNamedPipe %lx!\n",GetLastError());
671 CloseHandle(listenPipe
);
674 PIPE_StartRequestThread(listenPipe
);
675 listenPipe
= CreateNamedPipeA(
678 PIPE_TYPE_BYTE
|PIPE_WAIT
,
679 PIPE_UNLIMITED_INSTANCES
,
682 NMPWAIT_USE_DEFAULT_WAIT
,
685 if (listenPipe
== INVALID_HANDLE_VALUE
) {
686 FIXME("pipe creation failed for %s, le is %lx\n",pipefn
,GetLastError());
687 return 1; /* permanent failure, so quit stubmgr thread */
695 static BOOL stubMgrRunning
= FALSE
;
698 if (!stubMgrRunning
) {
699 stubMgrRunning
= TRUE
;
700 CreateThread(NULL
,0,_StubMgrThread
,NULL
,0,&tid
);
701 Sleep(2000); /* actually we just try opening the pipe until it succeeds */