Sync with trunk r43123
[reactos.git] / reactos / dll / win32 / ws2_32_new / src / dllmain.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
4 * FILE: dllmain.c
5 * PURPOSE: DLL Entrypoint
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 */
8
9 /*
10 * BUGS:
11 * - GetAddrInfoW is completely IPv4 hacked and has some dirty code.
12 * - LookupNodeByAddr is missing some IPv6 support.
13 */
14
15 /* INCLUDES ******************************************************************/
16 #include "ws2_32.h"
17
18 /* DATA **********************************************************************/
19
20 HANDLE WsSockHeap;
21 HINSTANCE WsDllHandle;
22 DWORD GlobalTlsIndex = TLS_OUT_OF_INDEXES;
23
24 /* FUNCTIONS *****************************************************************/
25
26 BOOL
27 APIENTRY
28 DllMain(HANDLE hModule,
29 DWORD dwReason,
30 LPVOID lpReserved)
31 {
32 PWSPROCESS WsProcess;
33
34 /* Main Entrypoint */
35 switch (dwReason)
36 {
37 case DLL_PROCESS_ATTACH:
38 /* Save DLL Handle */
39 WsDllHandle = hModule;
40
41 /* Get Global Heap */
42 WsSockHeap = GetProcessHeap();
43
44 /* TLS Allocation */
45 if (GlobalTlsIndex == TLS_OUT_OF_INDEXES)
46 {
47 GlobalTlsIndex = TlsAlloc();
48 if (GlobalTlsIndex == TLS_OUT_OF_INDEXES)
49 {
50 return FALSE;
51 }
52 }
53
54 /* Initialize some critical sections */
55 WsCreateStartupSynchronization();
56 WsAsyncGlobalInitialize();
57 WsRasInitializeAutodial();
58 break;
59
60 case DLL_THREAD_ATTACH:
61 break;
62
63 case DLL_THREAD_DETACH:
64 /* Destroy the attached Winsock Thread */
65 WsThreadDestroyCurrentThread();
66 break;
67
68 case DLL_PROCESS_DETACH:
69 /* Make sure we were initialized */
70 if (!WsDllHandle) break;
71
72 /* Check if this was a FreeLibrary call (ie: not process cleanup) */
73 if (lpReserved)
74 {
75 /* Destroy the thread which is exiting */
76 WsThreadDestroyCurrentThread();
77
78 /* Check if we have a process and destroy it */
79 WsProcess = WsGetProcess();
80 if (WsProcess) WsProcDelete(WsProcess);
81
82 /* Cleanup the Thread and Socket managers */
83 WsThreadCleanup();
84 WsSockCleanup();
85
86 /* Cleanup critical sections */
87 WsDestroyStartupSynchronization();
88 WsAsyncGlobalTerminate();
89
90 /* Free the TLS Index */
91 TlsFree(GlobalTlsIndex);
92 }
93
94 /* Cleanup RAS auto-dial helper */
95 WsRasUninitializeAutodial();
96
97 /* Clear our handle */
98 WsDllHandle = NULL;
99 break;
100 }
101
102 /* Return to OS */
103 return TRUE;
104 }
105
106 /*
107 * @implemented
108 */
109 INT
110 WSAAPI
111 WSAGetLastError(VOID)
112 {
113 /* Let the Windows Function do the work */
114 return GetLastError();
115 }
116
117 /*
118 * @implemented
119 */
120 VOID
121 WSAAPI
122 WSASetLastError(IN INT iError)
123 {
124 /* Let the Windows Function do the work */
125 SetLastError(iError);
126 }