b2697ba6e86ffab506a589ca1a6a947cc57694c1
[reactos.git] / reactos / lib / kernel32 / misc / dllmain.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/misc/dllmain.c
6 * PURPOSE: Initialization
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * UPDATE HISTORY:
9 * Created 01/11/98
10 */
11
12 /* INCLUDES ******************************************************************/
13
14 #include <k32.h>
15
16 #define NDEBUG
17 #include "../include/debug.h"
18
19 /* GLOBALS *******************************************************************/
20
21 extern UNICODE_STRING SystemDirectory;
22 extern UNICODE_STRING WindowsDirectory;
23
24 HANDLE hProcessHeap = NULL;
25 HMODULE hCurrentModule = NULL;
26 HANDLE hBaseDir = NULL;
27
28 static BOOL DllInitialized = FALSE;
29
30 BOOL STDCALL
31 DllMain(HANDLE hInst,
32 DWORD dwReason,
33 LPVOID lpReserved);
34
35 /* Critical section for various kernel32 data structures */
36 RTL_CRITICAL_SECTION DllLock;
37 RTL_CRITICAL_SECTION ConsoleLock;
38
39 extern BOOL WINAPI DefaultConsoleCtrlHandler(DWORD Event);
40
41 extern BOOL FASTCALL NlsInit();
42 extern VOID FASTCALL NlsUninit();
43
44 /* FUNCTIONS *****************************************************************/
45
46 static NTSTATUS
47 OpenBaseDirectory(PHANDLE DirHandle)
48 {
49 OBJECT_ATTRIBUTES ObjectAttributes;
50 UNICODE_STRING Name = RTL_CONSTANT_STRING(L"\\BaseNamedObjects");
51 NTSTATUS Status;
52
53 InitializeObjectAttributes(&ObjectAttributes,
54 &Name,
55 OBJ_PERMANENT,
56 NULL,
57 NULL);
58
59 Status = NtOpenDirectoryObject(DirHandle,
60 DIRECTORY_ALL_ACCESS,
61 &ObjectAttributes);
62 if (!NT_SUCCESS(Status))
63 {
64 Status = NtCreateDirectoryObject(DirHandle,
65 DIRECTORY_ALL_ACCESS,
66 &ObjectAttributes);
67 if (!NT_SUCCESS(Status))
68 {
69 DbgPrint("NtCreateDirectoryObject() failed\n");
70 }
71
72 return Status;
73 }
74
75 return STATUS_SUCCESS;
76 }
77
78
79 BOOL STDCALL
80 DllMain(HANDLE hDll,
81 DWORD dwReason,
82 LPVOID lpReserved)
83 {
84 NTSTATUS Status;
85
86 (void)lpReserved;
87
88 DPRINT("DllMain(hInst %lx, dwReason %lu)\n",
89 hDll, dwReason);
90
91 switch (dwReason)
92 {
93 case DLL_PROCESS_ATTACH:
94 DPRINT("DLL_PROCESS_ATTACH\n");
95
96 LdrDisableThreadCalloutsForDll ((PVOID)hDll);
97
98 /*
99 * Connect to the csrss server
100 */
101 Status = CsrClientConnectToServer(NULL,
102 0,
103 NULL,
104 NULL,
105 0,
106 NULL);
107 if (!NT_SUCCESS(Status))
108 {
109 DbgPrint("Failed to connect to csrss.exe (Status %lx)\n",
110 Status);
111 ZwTerminateProcess(NtCurrentProcess(), Status);
112 return FALSE;
113 }
114
115 hProcessHeap = RtlGetProcessHeap();
116 hCurrentModule = hDll;
117
118 /*
119 * Initialize WindowsDirectory and SystemDirectory
120 */
121 DPRINT("NtSystemRoot: %S\n",
122 SharedUserData->NtSystemRoot);
123 RtlCreateUnicodeString (&WindowsDirectory,
124 SharedUserData->NtSystemRoot);
125 SystemDirectory.MaximumLength = WindowsDirectory.MaximumLength + 18;
126 SystemDirectory.Length = WindowsDirectory.Length + 18;
127 SystemDirectory.Buffer = RtlAllocateHeap (hProcessHeap,
128 0,
129 SystemDirectory.MaximumLength);
130 wcscpy (SystemDirectory.Buffer, WindowsDirectory.Buffer);
131 wcscat (SystemDirectory.Buffer, L"\\System32");
132
133 /* Open object base directory */
134 Status = OpenBaseDirectory(&hBaseDir);
135 if (!NT_SUCCESS(Status))
136 {
137 DbgPrint("Failed to open object base directory (Status %lx)\n",
138 Status);
139 return FALSE;
140 }
141
142 /* Initialize the DLL critical section */
143 RtlInitializeCriticalSection(&DllLock);
144
145 /* Initialize the National Language Support routines */
146 if (! NlsInit())
147 {
148 return FALSE;
149 }
150
151 /* Initialize console ctrl handler */
152 RtlInitializeCriticalSection(&ConsoleLock);
153 SetConsoleCtrlHandler(DefaultConsoleCtrlHandler, TRUE);
154
155
156 /* Insert more dll attach stuff here! */
157
158 DllInitialized = TRUE;
159 break;
160
161 case DLL_PROCESS_DETACH:
162 DPRINT("DLL_PROCESS_DETACH\n");
163 if (DllInitialized == TRUE)
164 {
165 /* Insert more dll detach stuff here! */
166
167 NlsUninit();
168
169 /* Delete DLL critical section */
170 RtlDeleteCriticalSection (&ConsoleLock);
171 RtlDeleteCriticalSection (&DllLock);
172
173 /* Close object base directory */
174 NtClose(hBaseDir);
175
176 RtlFreeUnicodeString (&SystemDirectory);
177 RtlFreeUnicodeString (&WindowsDirectory);
178 }
179 break;
180
181 default:
182 break;
183 }
184
185 return TRUE;
186 }
187
188 /* EOF */