2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
5 * PURPOSE: Winsock Utility Functions
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
9 /* INCLUDES ******************************************************************/
12 /* DATA **********************************************************************/
14 /* FUNCTIONS *****************************************************************/
18 WsOpenRegistryRoot(VOID
)
22 ULONG CreateDisposition
;
24 /* Open Registry Key */
25 ErrorCode
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
31 /* Check if it wasn't found */
32 if (ErrorCode
== ERROR_FILE_NOT_FOUND
)
35 RegCreateKeyEx(HKEY_LOCAL_MACHINE
,
39 REG_OPTION_NON_VOLATILE
,
45 else if (ErrorCode
== ERROR_SUCCESS
)
47 /* It already exists */
48 CreateDisposition
= REG_OPENED_EXISTING_KEY
;
51 /* Check for failure */
52 if (ErrorCode
!= ERROR_SUCCESS
) return NULL
;
54 /* Check if we had to create a new key */
55 if (CreateDisposition
== REG_CREATED_NEW_KEY
)
57 /* Write the Winsock Version */
58 RegSetValueEx(WinsockRootKey
,
59 "WinSock_Registry_Version",
67 /* Read the Winsock Version */
71 return WinsockRootKey
;
76 WsCheckCatalogState(IN HANDLE Event
)
80 /* Wait for the object */
81 Return
= WaitForSingleObject(Event
, 0);
83 /* Check for the value */
84 if (Return
== WAIT_OBJECT_0
) return TRUE
;
86 /* If it timedout or anything else, return false */
92 WsApiProlog(OUT PWSPROCESS
*Process
,
93 OUT PWSTHREAD
*Thread
)
95 INT ErrorCode
= WSANOTINITIALISED
;
97 /* Try to get the current process */
98 if ((*Process
= WsGetProcess()))
100 /* And the current thread */
101 ErrorCode
= WsThreadGetCurrentThread(*Process
, Thread
);
104 /* Return init status */
115 /* Call the prolog */
116 return WsApiProlog(&Process
, &Thread
);
121 WsSlowPrologTid(OUT LPWSATHREADID
*ThreadId
)
127 /* Call the prolog */
128 ErrorCode
= WsApiProlog(&Process
, &Thread
);
130 /* Check for success */
131 if (ErrorCode
== ERROR_SUCCESS
)
133 /* Return the Thread ID */
134 *ThreadId
= &Thread
->WahThreadId
;
143 WsSetupCatalogProtection(IN HKEY CatalogKey
,
144 IN HANDLE CatalogEvent
,
145 OUT LPDWORD UniqueId
)
151 DWORD RegType
= REG_DWORD
;
152 DWORD RegSize
= sizeof(DWORD
);
158 /* Ask for notifications */
159 ErrorCode
= RegNotifyChangeKeyValue(CatalogKey
,
161 REG_NOTIFY_CHANGE_NAME
,
164 if (ErrorCode
!= ERROR_SUCCESS
)
166 /* Normalize error code */
167 ErrorCode
= WSASYSCALLFAILURE
;
172 /* Read the current ID */
173 ErrorCode
= RegQueryValueEx(CatalogKey
,
177 (LPBYTE
)&NewUniqueId
,
179 if (ErrorCode
!= ERROR_SUCCESS
)
181 /* Critical failure */
182 ErrorCode
= WSASYSCALLFAILURE
;
186 /* Try to open it for writing */
187 sprintf(KeyBuffer
, "%8.8lX", NewUniqueId
);
188 ErrorCode
= RegOpenKeyEx(CatalogKey
,
194 /* If the key doesn't exist or is being delete, that's ok for us */
195 if ((ErrorCode
== ERROR_FILE_NOT_FOUND
) ||
196 (ErrorCode
== ERROR_KEY_DELETED
))
198 /* Set success and return the new ID */
199 ErrorCode
= ERROR_SUCCESS
;
200 *UniqueId
= NewUniqueId
;
203 else if (ErrorCode
!= ERROR_SUCCESS
)
205 /* Any other failure is bad */
206 ErrorCode
= WSASYSCALLFAILURE
;
210 /* If we could actually open the key, someone is using it :/ */
211 ErrorCode
= RegCloseKey(RegistryKey
);
213 /* In case we break out prematurely */
214 ErrorCode
= WSANO_RECOVERY
;
216 /* Keep looping until they let go of the registry writing */
217 } while (!WaitForSingleObject(CatalogEvent
, 180 * 1000));
219 /* Return error code */
225 MapUnicodeProtocolInfoToAnsi(IN LPWSAPROTOCOL_INFOW UnicodeInfo
,
226 OUT LPWSAPROTOCOL_INFOA AnsiInfo
)
230 /* Copy all the data that doesn't need converting */
231 RtlCopyMemory(AnsiInfo
,
233 FIELD_OFFSET(WSAPROTOCOL_INFOA
, szProtocol
));
235 /* Now convert the protocol string */
236 ReturnValue
= WideCharToMultiByte(CP_ACP
,
238 UnicodeInfo
->szProtocol
,
240 AnsiInfo
->szProtocol
,
241 sizeof(AnsiInfo
->szProtocol
),
244 if (!ReturnValue
) return WSASYSCALLFAILURE
;
247 return ERROR_SUCCESS
;