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