4 * Copyright 2000 Huw D M Davies for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * WINE RPC TODO's (and a few TODONT's)
22 * - Ove's decreasingly incomplete widl is an IDL compiler for wine. For widl
23 * to be wine's only IDL compiler, a fair bit of work remains to be done.
24 * until then we have used some midl-generated stuff. (What?)
25 * widl currently doesn't generate stub/proxy files required by wine's (O)RPC
26 * capabilities -- nor does it make those lovely format strings :(
27 * The MS MIDL compiler does some really esoteric stuff. Of course Ove has
28 * started with the less esoteric stuff. There are also lots of nice
29 * comments in there if you want to flex your bison and help build this monster.
31 * - RPC has a quite featureful error handling mechanism; basically none of this is
32 * implemented right now. We also have deficiencies on the compiler side, where
33 * wine's __TRY / __EXCEPT / __FINALLY macros are not even used for RpcTryExcept & co,
34 * due to syntactic differences! (we can fix it with widl by using __TRY)
36 * - There are several different memory allocation schemes for MSRPC.
37 * I don't even understand what they all are yet, much less have them
38 * properly implemented. Surely we are supposed to be doing something with
39 * the user-provided allocation/deallocation functions, but so far,
40 * I don't think we are doing this...
42 * - MSRPC provides impersonation capabilities which currently are not possible
43 * to implement in wine. At the very least we should implement the authorization
44 * API's & gracefully ignore the irrelevant stuff (to an extent we already do).
46 * - Some transports are not yet implemented. The existing transport implementations
47 * are incomplete and may be bug-infested.
49 * - The various transports that we do support ought to be supported in a more
50 * object-oriented manner, as in DCE's RPC implementation, instead of cluttering
51 * up the code with conditionals like we do now.
53 * - Data marshalling: So far, only the beginnings of a full implementation
54 * exist in wine. NDR protocol itself is documented, but the MS API's to
55 * convert data-types in memory into NDR are not. This is challenging work,
56 * and has supposedly been "at the top of Greg's queue" for several months now.
58 * - ORPC is RPC for OLE; once we have a working RPC framework, we can
59 * use it to implement out-of-process OLE client/server communications.
60 * ATM there is maybe a disconnect between the marshalling in the OLE DLLs
61 * and the marshalling going on here [TODO: well, is there or not?]
63 * - In-source API Documentation, at least for those functions which we have
64 * implemented, but preferably for everything we can document, would be nice,
65 * since some of this stuff is quite obscure.
67 * - Name services... [TODO: what about them]
69 * - Protocol Towers: Totally unimplemented.... I think.
71 * - Context Handle Rundown: whatever that is.
73 * - Nested RPC's: Totally unimplemented.
75 * - Statistics: we are supposed to be keeping various counters. we aren't.
77 * - Async RPC: Unimplemented.
79 * - XML/http RPC: Somewhere there's an XML fiend that wants to do this! Betcha
80 * we could use these as a transport for RPC's across computers without a
81 * permissions and/or licensing crisis.
83 * - The NT "ports" API, aka LPC. Greg claims this is on his radar. Might (or
84 * might not) enable users to get some kind of meaningful result out of
85 * NT-based native rpcrt4's. Commonly-used transport for self-to-self RPC's.
87 * - ...? More stuff I haven't thought of. If you think of more RPC todo's
88 * drop me an e-mail <gmturner007@ameritech.net> or send a patch to the
89 * wine-patches mailing list.
100 #include "winerror.h"
104 #include "winternl.h"
106 #include "iphlpapi.h"
107 #include "wine/unicode.h"
112 #include "rpcproxy.h"
114 #include "rpc_binding.h"
115 #include "rpcss_np_client.h"
117 #include "wine/debug.h"
119 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
121 static UUID uuid_nil;
122 static HANDLE master_mutex;
124 HANDLE RPCRT4_GetMasterMutex(void)
129 static CRITICAL_SECTION uuid_cs;
130 static CRITICAL_SECTION_DEBUG critsect_debug =
133 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
134 0, 0, { (DWORD_PTR)(__FILE__ ": uuid_cs") }
136 static CRITICAL_SECTION uuid_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
138 static CRITICAL_SECTION threaddata_cs;
139 static CRITICAL_SECTION_DEBUG threaddata_cs_debug =
141 0, 0, &threaddata_cs,
142 { &threaddata_cs_debug.ProcessLocksList, &threaddata_cs_debug.ProcessLocksList },
143 0, 0, { (DWORD_PTR)(__FILE__ ": threaddata_cs") }
145 static CRITICAL_SECTION threaddata_cs = { &threaddata_cs_debug, -1, 0, 0, 0, 0 };
147 struct list threaddata_list = LIST_INIT(threaddata_list);
154 RpcConnection *connection;
155 RpcBinding *server_binding;
158 /***********************************************************************
162 * hinstDLL [I] handle to the DLL's instance
164 * lpvReserved [I] reserved, must be NULL
171 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
173 struct threaddata *tdata;
176 case DLL_PROCESS_ATTACH:
177 master_mutex = CreateMutexA( NULL, FALSE, RPCSS_MASTER_MUTEX_NAME);
179 ERR("Failed to create master mutex\n");
182 case DLL_THREAD_DETACH:
183 tdata = NtCurrentTeb()->ReservedForNtRpc;
186 EnterCriticalSection(&threaddata_cs);
187 list_remove(&tdata->entry);
188 LeaveCriticalSection(&threaddata_cs);
190 DeleteCriticalSection(&tdata->cs);
191 if (tdata->connection)
192 ERR("tdata->connection should be NULL but is still set to %p\n", tdata->connection);
193 if (tdata->server_binding)
194 ERR("tdata->server_binding should be NULL but is still set to %p\n", tdata->server_binding);
195 HeapFree(GetProcessHeap(), 0, tdata);
199 case DLL_PROCESS_DETACH:
200 CloseHandle(master_mutex);
208 /*************************************************************************
209 * RpcStringFreeA [RPCRT4.@]
211 * Frees a character string allocated by the RPC run-time library.
215 * S_OK if successful.
217 RPC_STATUS WINAPI RpcStringFreeA(RPC_CSTR* String)
219 HeapFree( GetProcessHeap(), 0, *String);
224 /*************************************************************************
225 * RpcStringFreeW [RPCRT4.@]
227 * Frees a character string allocated by the RPC run-time library.
231 * S_OK if successful.
233 RPC_STATUS WINAPI RpcStringFreeW(RPC_WSTR* String)
235 HeapFree( GetProcessHeap(), 0, *String);
240 /*************************************************************************
241 * RpcRaiseException [RPCRT4.@]
243 * Raises an exception.
245 void DECLSPEC_NORETURN WINAPI RpcRaiseException(RPC_STATUS exception)
247 /* shouldn't return */
248 RaiseException(exception, 0, 0, NULL);
249 ERR("handler continued execution\n");
253 /*************************************************************************
254 * UuidCompare [RPCRT4.@]
257 * UUID *Uuid1 [I] Uuid to compare
258 * UUID *Uuid2 [I] Uuid to compare
259 * RPC_STATUS *Status [O] returns RPC_S_OK
262 * -1 if Uuid1 is less than Uuid2
263 * 0 if Uuid1 and Uuid2 are equal
264 * 1 if Uuid1 is greater than Uuid2
266 int WINAPI UuidCompare(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
270 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
274 if (!Uuid1) Uuid1 = &uuid_nil;
275 if (!Uuid2) Uuid2 = &uuid_nil;
277 if (Uuid1 == Uuid2) return 0;
279 if (Uuid1->Data1 != Uuid2->Data1)
280 return Uuid1->Data1 < Uuid2->Data1 ? -1 : 1;
282 if (Uuid1->Data2 != Uuid2->Data2)
283 return Uuid1->Data2 < Uuid2->Data2 ? -1 : 1;
285 if (Uuid1->Data3 != Uuid2->Data3)
286 return Uuid1->Data3 < Uuid2->Data3 ? -1 : 1;
288 for (i = 0; i < 8; i++) {
289 if (Uuid1->Data4[i] < Uuid2->Data4[i])
291 if (Uuid1->Data4[i] > Uuid2->Data4[i])
298 /*************************************************************************
299 * UuidEqual [RPCRT4.@]
302 * UUID *Uuid1 [I] Uuid to compare
303 * UUID *Uuid2 [I] Uuid to compare
304 * RPC_STATUS *Status [O] returns RPC_S_OK
309 int WINAPI UuidEqual(UUID *Uuid1, UUID *Uuid2, RPC_STATUS *Status)
311 TRACE("(%s,%s)\n", debugstr_guid(Uuid1), debugstr_guid(Uuid2));
312 return !UuidCompare(Uuid1, Uuid2, Status);
315 /*************************************************************************
316 * UuidIsNil [RPCRT4.@]
319 * UUID *Uuid [I] Uuid to compare
320 * RPC_STATUS *Status [O] retuns RPC_S_OK
325 int WINAPI UuidIsNil(UUID *Uuid, RPC_STATUS *Status)
327 TRACE("(%s)\n", debugstr_guid(Uuid));
328 if (!Uuid) return TRUE;
329 return !UuidCompare(Uuid, &uuid_nil, Status);
332 /*************************************************************************
333 * UuidCreateNil [RPCRT4.@]
336 * UUID *Uuid [O] returns a nil UUID
341 RPC_STATUS WINAPI UuidCreateNil(UUID *Uuid)
347 /* Number of 100ns ticks per clock tick. To be safe, assume that the clock
348 resolution is at least 1000 * 100 * (1/1000000) = 1/10 of a second */
349 #define TICKS_PER_CLOCK_TICK 1000
350 #define SECSPERDAY 86400
351 #define TICKSPERSEC 10000000
352 /* UUID system time starts at October 15, 1582 */
353 #define SECS_15_OCT_1582_TO_1601 ((17 + 30 + 31 + 365 * 18 + 5) * SECSPERDAY)
354 #define TICKS_15_OCT_1582_TO_1601 ((ULONGLONG)SECS_15_OCT_1582_TO_1601 * TICKSPERSEC)
356 static void RPC_UuidGetSystemTime(ULONGLONG *time)
360 GetSystemTimeAsFileTime(&ft);
362 *time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
363 *time += TICKS_15_OCT_1582_TO_1601;
366 /* Assume that a hardware address is at least 6 bytes long */
367 #define ADDRESS_BYTES_NEEDED 6
369 static RPC_STATUS RPC_UuidGetNodeAddress(BYTE *address)
372 DWORD status = RPC_S_OK;
374 ULONG buflen = sizeof(IP_ADAPTER_INFO);
375 PIP_ADAPTER_INFO adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
377 if (GetAdaptersInfo(adapter, &buflen) == ERROR_BUFFER_OVERFLOW) {
378 HeapFree(GetProcessHeap(), 0, adapter);
379 adapter = HeapAlloc(GetProcessHeap(), 0, buflen);
382 if (GetAdaptersInfo(adapter, &buflen) == NO_ERROR) {
383 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
384 address[i] = adapter->Address[i];
387 /* We can't get a hardware address, just use random numbers.
388 Set the multicast bit to prevent conflicts with real cards. */
390 for (i = 0; i < ADDRESS_BYTES_NEEDED; i++) {
391 address[i] = rand() & 0xff;
395 status = RPC_S_UUID_LOCAL_ONLY;
398 HeapFree(GetProcessHeap(), 0, adapter);
402 /*************************************************************************
403 * UuidCreate [RPCRT4.@]
405 * Creates a 128bit UUID.
409 * RPC_S_OK if successful.
410 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
412 * FIXME: No compensation for changes across reloading
413 * this dll or across reboots (e.g. clock going
414 * backwards and swapped network cards). The RFC
415 * suggests using NVRAM for storing persistent
418 RPC_STATUS WINAPI UuidCreate(UUID *Uuid)
420 static int initialised, count;
423 static ULONGLONG timelast;
424 static WORD sequence;
427 static BYTE address[MAX_ADAPTER_ADDRESS_LENGTH];
429 EnterCriticalSection(&uuid_cs);
432 RPC_UuidGetSystemTime(&timelast);
433 count = TICKS_PER_CLOCK_TICK;
435 sequence = ((rand() & 0xff) << 8) + (rand() & 0xff);
438 status = RPC_UuidGetNodeAddress(address);
442 /* Generate time element of the UUID. Account for going faster
443 than our clock as well as the clock going backwards. */
445 RPC_UuidGetSystemTime(&time);
446 if (time > timelast) {
450 if (time < timelast) {
451 sequence = (sequence + 1) & 0x1fff;
455 if (count < TICKS_PER_CLOCK_TICK) {
464 /* Pack the information into the UUID structure. */
466 Uuid->Data1 = (unsigned long)(time & 0xffffffff);
467 Uuid->Data2 = (unsigned short)((time >> 32) & 0xffff);
468 Uuid->Data3 = (unsigned short)((time >> 48) & 0x0fff);
470 /* This is a version 1 UUID */
471 Uuid->Data3 |= (1 << 12);
473 Uuid->Data4[0] = sequence & 0xff;
474 Uuid->Data4[1] = (sequence & 0x3f00) >> 8;
475 Uuid->Data4[1] |= 0x80;
477 Uuid->Data4[2] = address[0];
478 Uuid->Data4[3] = address[1];
479 Uuid->Data4[4] = address[2];
480 Uuid->Data4[5] = address[3];
481 Uuid->Data4[6] = address[4];
482 Uuid->Data4[7] = address[5];
484 LeaveCriticalSection(&uuid_cs);
486 TRACE("%s\n", debugstr_guid(Uuid));
491 /*************************************************************************
492 * UuidCreateSequential [RPCRT4.@]
494 * Creates a 128bit UUID.
498 * RPC_S_OK if successful.
499 * RPC_S_UUID_LOCAL_ONLY if UUID is only locally unique.
502 RPC_STATUS WINAPI UuidCreateSequential(UUID *Uuid)
504 return UuidCreate(Uuid);
508 /*************************************************************************
509 * UuidHash [RPCRT4.@]
511 * Generates a hash value for a given UUID
513 * Code based on FreeDCE implementation
516 unsigned short WINAPI UuidHash(UUID *uuid, RPC_STATUS *Status)
518 BYTE *data = (BYTE*)uuid;
519 short c0 = 0, c1 = 0, x, y;
522 if (!uuid) data = (BYTE*)(uuid = &uuid_nil);
524 TRACE("(%s)\n", debugstr_guid(uuid));
526 for (i=0; i<sizeof(UUID); i++) {
541 /*************************************************************************
542 * UuidToStringA [RPCRT4.@]
544 * Converts a UUID to a string.
546 * UUID format is 8 hex digits, followed by a hyphen then three groups of
547 * 4 hex digits each followed by a hyphen and then 12 hex digits
551 * S_OK if successful.
552 * S_OUT_OF_MEMORY if unsuccessful.
554 RPC_STATUS WINAPI UuidToStringA(UUID *Uuid, RPC_CSTR* StringUuid)
556 *StringUuid = HeapAlloc( GetProcessHeap(), 0, sizeof(char) * 37);
559 return RPC_S_OUT_OF_MEMORY;
561 if (!Uuid) Uuid = &uuid_nil;
563 sprintf( (char*)*StringUuid, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
564 Uuid->Data1, Uuid->Data2, Uuid->Data3,
565 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
566 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
567 Uuid->Data4[6], Uuid->Data4[7] );
572 /*************************************************************************
573 * UuidToStringW [RPCRT4.@]
575 * Converts a UUID to a string.
577 * S_OK if successful.
578 * S_OUT_OF_MEMORY if unsuccessful.
580 RPC_STATUS WINAPI UuidToStringW(UUID *Uuid, RPC_WSTR* StringUuid)
584 if (!Uuid) Uuid = &uuid_nil;
586 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
587 Uuid->Data1, Uuid->Data2, Uuid->Data3,
588 Uuid->Data4[0], Uuid->Data4[1], Uuid->Data4[2],
589 Uuid->Data4[3], Uuid->Data4[4], Uuid->Data4[5],
590 Uuid->Data4[6], Uuid->Data4[7] );
592 *StringUuid = RPCRT4_strdupAtoW(buf);
595 return RPC_S_OUT_OF_MEMORY;
600 static const BYTE hex2bin[] =
602 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 */
603 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 */
604 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x20 */
605 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0x30 */
606 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0, /* 0x40 */
607 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x50 */
608 0,10,11,12,13,14,15 /* 0x60 */
611 /***********************************************************************
612 * UuidFromStringA (RPCRT4.@)
614 RPC_STATUS WINAPI UuidFromStringA(RPC_CSTR s, UUID *uuid)
618 if (!s) return UuidCreateNil( uuid );
620 if (strlen((char*)s) != 36) return RPC_S_INVALID_STRING_UUID;
622 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
623 return RPC_S_INVALID_STRING_UUID;
627 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
628 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
631 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
633 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
634 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
635 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
636 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
638 /* these are just sequential bytes */
639 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
640 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
641 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
642 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
643 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
644 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
645 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
646 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
651 /***********************************************************************
652 * UuidFromStringW (RPCRT4.@)
654 RPC_STATUS WINAPI UuidFromStringW(RPC_WSTR s, UUID *uuid)
658 if (!s) return UuidCreateNil( uuid );
660 if (strlenW(s) != 36) return RPC_S_INVALID_STRING_UUID;
662 if ((s[8]!='-') || (s[13]!='-') || (s[18]!='-') || (s[23]!='-'))
663 return RPC_S_INVALID_STRING_UUID;
667 if ((i == 8)||(i == 13)||(i == 18)||(i == 23)) continue;
668 if (s[i] > 'f' || (!hex2bin[s[i]] && s[i] != '0')) return RPC_S_INVALID_STRING_UUID;
671 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
673 uuid->Data1 = (hex2bin[s[0]] << 28 | hex2bin[s[1]] << 24 | hex2bin[s[2]] << 20 | hex2bin[s[3]] << 16 |
674 hex2bin[s[4]] << 12 | hex2bin[s[5]] << 8 | hex2bin[s[6]] << 4 | hex2bin[s[7]]);
675 uuid->Data2 = hex2bin[s[9]] << 12 | hex2bin[s[10]] << 8 | hex2bin[s[11]] << 4 | hex2bin[s[12]];
676 uuid->Data3 = hex2bin[s[14]] << 12 | hex2bin[s[15]] << 8 | hex2bin[s[16]] << 4 | hex2bin[s[17]];
678 /* these are just sequential bytes */
679 uuid->Data4[0] = hex2bin[s[19]] << 4 | hex2bin[s[20]];
680 uuid->Data4[1] = hex2bin[s[21]] << 4 | hex2bin[s[22]];
681 uuid->Data4[2] = hex2bin[s[24]] << 4 | hex2bin[s[25]];
682 uuid->Data4[3] = hex2bin[s[26]] << 4 | hex2bin[s[27]];
683 uuid->Data4[4] = hex2bin[s[28]] << 4 | hex2bin[s[29]];
684 uuid->Data4[5] = hex2bin[s[30]] << 4 | hex2bin[s[31]];
685 uuid->Data4[6] = hex2bin[s[32]] << 4 | hex2bin[s[33]];
686 uuid->Data4[7] = hex2bin[s[34]] << 4 | hex2bin[s[35]];
690 /***********************************************************************
691 * DllRegisterServer (RPCRT4.@)
694 HRESULT WINAPI DllRegisterServer( void )
696 FIXME( "(): stub\n" );
700 static BOOL RPCRT4_StartRPCSS(void)
702 PROCESS_INFORMATION pi;
707 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
708 ZeroMemory(&si, sizeof(STARTUPINFOA));
709 si.cb = sizeof(STARTUPINFOA);
711 /* apparently it's not OK to use a constant string below */
712 CopyMemory(cmd, "rpcss", 6);
714 /* FIXME: will this do the right thing when run as a test? */
715 rslt = CreateProcessA(
716 NULL, /* executable */
717 cmd, /* command line */
718 NULL, /* process security attributes */
719 NULL, /* primary thread security attributes */
720 FALSE, /* inherit handles */
721 0, /* creation flags */
722 NULL, /* use parent's environment */
723 NULL, /* use parent's current directory */
724 &si, /* STARTUPINFO pointer */
725 &pi /* PROCESS_INFORMATION */
729 CloseHandle(pi.hProcess);
730 CloseHandle(pi.hThread);
736 /***********************************************************************
737 * RPCRT4_RPCSSOnDemandCall (internal)
739 * Attempts to send a message to the RPCSS process
740 * on the local machine, invoking it if necessary.
741 * For remote RPCSS calls, use.... your imagination.
744 * msg [I] pointer to the RPCSS message
745 * vardata_payload [I] pointer vardata portion of the RPCSS message
746 * reply [O] pointer to reply structure
752 BOOL RPCRT4_RPCSSOnDemandCall(PRPCSS_NP_MESSAGE msg, char *vardata_payload, PRPCSS_NP_REPLY reply)
754 HANDLE client_handle;
758 TRACE("(msg == %p, vardata_payload == %p, reply == %p)\n", msg, vardata_payload, reply);
760 client_handle = RPCRT4_RpcssNPConnect();
762 while (INVALID_HANDLE_VALUE == client_handle) {
763 /* start the RPCSS process */
764 if (!RPCRT4_StartRPCSS()) {
765 ERR("Unable to start RPCSS process.\n");
768 /* wait for a connection (w/ periodic polling) */
769 for (i = 0; i < 60; i++) {
771 client_handle = RPCRT4_RpcssNPConnect();
772 if (INVALID_HANDLE_VALUE != client_handle) break;
774 /* we are only willing to try twice */
778 if (INVALID_HANDLE_VALUE == client_handle) {
780 ERR("Unable to connect to RPCSS process!\n");
781 SetLastError(RPC_E_SERVER_DIED_DNE);
785 /* great, we're connected. now send the message */
787 if (!RPCRT4_SendReceiveNPMsg(client_handle, msg, vardata_payload, reply)) {
788 ERR("Something is amiss: RPC_SendReceive failed.\n");
791 CloseHandle(client_handle);
796 #define MAX_RPC_ERROR_TEXT 256
798 /******************************************************************************
799 * DceErrorInqTextW (rpcrt4.@)
802 * 1. On passing a NULL pointer the code does bomb out.
803 * 2. The size of the required buffer is not defined in the documentation.
804 * It appears to be 256.
805 * 3. The function is defined to return RPC_S_INVALID_ARG but I don't know
806 * of any value for which it does.
807 * 4. The MSDN documentation currently declares that the second argument is
808 * unsigned char *, even for the W version. I don't believe it.
810 RPC_STATUS RPC_ENTRY DceErrorInqTextW (RPC_STATUS e, RPC_WSTR buffer)
813 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
814 FORMAT_MESSAGE_IGNORE_INSERTS,
815 NULL, e, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
818 count = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
819 FORMAT_MESSAGE_IGNORE_INSERTS,
820 NULL, RPC_S_NOT_RPC_ERROR, 0, buffer, MAX_RPC_ERROR_TEXT, NULL);
823 ERR ("Failed to translate error\n");
824 return RPC_S_INVALID_ARG;
830 /******************************************************************************
831 * DceErrorInqTextA (rpcrt4.@)
833 RPC_STATUS RPC_ENTRY DceErrorInqTextA (RPC_STATUS e, RPC_CSTR buffer)
836 WCHAR bufferW [MAX_RPC_ERROR_TEXT];
837 if ((status = DceErrorInqTextW (e, bufferW)) == RPC_S_OK)
839 if (!WideCharToMultiByte(CP_ACP, 0, bufferW, -1, (LPSTR)buffer, MAX_RPC_ERROR_TEXT,
842 ERR ("Failed to translate error\n");
843 status = RPC_S_INVALID_ARG;
849 /******************************************************************************
850 * I_RpcAllocate (rpcrt4.@)
852 void * WINAPI I_RpcAllocate(unsigned int Size)
854 return HeapAlloc(GetProcessHeap(), 0, Size);
857 /******************************************************************************
858 * I_RpcFree (rpcrt4.@)
860 void WINAPI I_RpcFree(void *Object)
862 HeapFree(GetProcessHeap(), 0, Object);
865 /******************************************************************************
866 * I_RpcMapWin32Status (rpcrt4.@)
868 LONG WINAPI I_RpcMapWin32Status(RPC_STATUS status)
870 FIXME("(%ld): stub\n", status);
874 /******************************************************************************
875 * RpcErrorStartEnumeration (rpcrt4.@)
877 RPC_STATUS RPC_ENTRY RpcErrorStartEnumeration(RPC_ERROR_ENUM_HANDLE* EnumHandle)
879 FIXME("(%p): stub\n", EnumHandle);
880 return RPC_S_ENTRY_NOT_FOUND;
883 /******************************************************************************
884 * RpcMgmtSetCancelTimeout (rpcrt4.@)
886 RPC_STATUS RPC_ENTRY RpcMgmtSetCancelTimeout(LONG Timeout)
888 FIXME("(%d): stub\n", Timeout);
892 static struct threaddata *get_or_create_threaddata(void)
894 struct threaddata *tdata = NtCurrentTeb()->ReservedForNtRpc;
897 tdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*tdata));
898 if (!tdata) return NULL;
900 InitializeCriticalSection(&tdata->cs);
901 tdata->thread_id = GetCurrentThreadId();
903 EnterCriticalSection(&threaddata_cs);
904 list_add_tail(&threaddata_list, &tdata->entry);
905 LeaveCriticalSection(&threaddata_cs);
907 NtCurrentTeb()->ReservedForNtRpc = tdata;
913 void RPCRT4_SetThreadCurrentConnection(RpcConnection *Connection)
915 struct threaddata *tdata = get_or_create_threaddata();
918 EnterCriticalSection(&tdata->cs);
919 tdata->connection = Connection;
920 LeaveCriticalSection(&tdata->cs);
923 void RPCRT4_SetThreadCurrentCallHandle(RpcBinding *Binding)
925 struct threaddata *tdata = get_or_create_threaddata();
928 tdata->server_binding = Binding;
931 RpcBinding *RPCRT4_GetThreadCurrentCallHandle(void)
933 struct threaddata *tdata = get_or_create_threaddata();
934 if (!tdata) return NULL;
936 return tdata->server_binding;
939 /******************************************************************************
940 * RpcCancelThread (rpcrt4.@)
942 RPC_STATUS RPC_ENTRY RpcCancelThread(void* ThreadHandle)
945 struct threaddata *tdata;
947 TRACE("(%p)\n", ThreadHandle);
949 target_tid = GetThreadId(ThreadHandle);
951 return RPC_S_INVALID_ARG;
953 EnterCriticalSection(&threaddata_cs);
954 LIST_FOR_EACH_ENTRY(tdata, &threaddata_list, struct threaddata, entry)
955 if (tdata->thread_id == target_tid)
957 EnterCriticalSection(&tdata->cs);
958 if (tdata->connection) rpcrt4_conn_cancel_call(tdata->connection);
959 LeaveCriticalSection(&tdata->cs);
962 LeaveCriticalSection(&threaddata_cs);