f8e9d4c588f19c4a0e356daddff136892a4299ea
[reactos.git] / reactos / lib / kernel32 / misc / dllmain.c
1 /* $Id: dllmain.c,v 1.36 2004/08/24 17:21:11 navaraf Exp $
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 <roscfg.h>
15 #include <k32.h>
16
17 #define NDEBUG
18 #include "../include/debug.h"
19
20 /* GLOBALS *******************************************************************/
21
22 extern UNICODE_STRING SystemDirectory;
23 extern UNICODE_STRING WindowsDirectory;
24
25 HANDLE hProcessHeap = NULL;
26 HMODULE hCurrentModule = NULL;
27 HANDLE hBaseDir = NULL;
28
29 static BOOL DllInitialized = FALSE;
30
31 BOOL STDCALL
32 DllMain(HANDLE hInst,
33 DWORD dwReason,
34 LPVOID lpReserved);
35
36 /* Critical section for various kernel32 data structures */
37 CRITICAL_SECTION DllLock;
38 CRITICAL_SECTION ConsoleLock;
39
40 extern BOOL WINAPI DefaultConsoleCtrlHandler(DWORD Event);
41 extern BOOL FASTCALL PROFILE_Init();
42
43 extern BOOL FASTCALL NlsInit();
44 extern VOID FASTCALL NlsUninit();
45
46 /* FUNCTIONS *****************************************************************/
47
48 static NTSTATUS
49 OpenBaseDirectory(PHANDLE DirHandle)
50 {
51 OBJECT_ATTRIBUTES ObjectAttributes;
52 UNICODE_STRING Name = ROS_STRING_INITIALIZER(L"\\BaseNamedObjects");
53 NTSTATUS Status;
54
55 InitializeObjectAttributes(&ObjectAttributes,
56 &Name,
57 OBJ_PERMANENT,
58 NULL,
59 NULL);
60
61 Status = NtOpenDirectoryObject(DirHandle,
62 DIRECTORY_ALL_ACCESS,
63 &ObjectAttributes);
64 if (!NT_SUCCESS(Status))
65 {
66 Status = NtCreateDirectoryObject(DirHandle,
67 DIRECTORY_ALL_ACCESS,
68 &ObjectAttributes);
69 if (!NT_SUCCESS(Status))
70 {
71 DbgPrint("NtCreateDirectoryObject() failed\n");
72 }
73
74 return Status;
75 }
76
77 return STATUS_SUCCESS;
78 }
79
80
81 BOOL STDCALL
82 DllMain(HANDLE hDll,
83 DWORD dwReason,
84 LPVOID lpReserved)
85 {
86 NTSTATUS Status;
87
88 (void)lpReserved;
89
90 DPRINT("DllMain(hInst %lx, dwReason %lu)\n",
91 hDll, dwReason);
92
93 switch (dwReason)
94 {
95 case DLL_PROCESS_ATTACH:
96 DPRINT("DLL_PROCESS_ATTACH\n");
97
98 #if !defined(REGTESTS)
99 /*
100 * When running regression tests, this module need to receive
101 * thread attach/detach notifications. This is needed because
102 * the module is already loaded when the regression test suite
103 * driver would load this module using LoadLibrary() so a
104 * DLL_PROCESS_ATTACH notification is not sent. The regression
105 * test suite driver sends thread notifications instead in this
106 * case.
107 */
108 LdrDisableThreadCalloutsForDll ((PVOID)hDll);
109 #endif
110
111 /*
112 * Connect to the csrss server
113 */
114 Status = CsrClientConnectToServer();
115 if (!NT_SUCCESS(Status))
116 {
117 DbgPrint("Failed to connect to csrss.exe (Status %lx)\n",
118 Status);
119 ZwTerminateProcess(NtCurrentProcess(), Status);
120 return FALSE;
121 }
122
123 hProcessHeap = RtlGetProcessHeap();
124 hCurrentModule = hDll;
125
126 /*
127 * Initialize WindowsDirectory and SystemDirectory
128 */
129 DPRINT("NtSystemRoot: %S\n",
130 SharedUserData->NtSystemRoot);
131 RtlCreateUnicodeString (&WindowsDirectory,
132 SharedUserData->NtSystemRoot);
133 SystemDirectory.MaximumLength = WindowsDirectory.MaximumLength + 18;
134 SystemDirectory.Length = WindowsDirectory.Length + 18;
135 SystemDirectory.Buffer = RtlAllocateHeap (hProcessHeap,
136 0,
137 SystemDirectory.MaximumLength);
138 wcscpy (SystemDirectory.Buffer, WindowsDirectory.Buffer);
139 wcscat (SystemDirectory.Buffer, L"\\System32");
140
141 /* Open object base directory */
142 Status = OpenBaseDirectory(&hBaseDir);
143 if (!NT_SUCCESS(Status))
144 {
145 DbgPrint("Failed to open object base directory (Status %lx)\n",
146 Status);
147 return FALSE;
148 }
149
150 /* Initialize the DLL critical section */
151 RtlInitializeCriticalSection(&DllLock);
152
153 /* Initialize the profile (.ini) routines */
154 if (! PROFILE_Init())
155 {
156 return FALSE;
157 }
158
159 /* Initialize the National Language Support routines */
160 if (! NlsInit())
161 {
162 return FALSE;
163 }
164
165 /* Initialize console ctrl handler */
166 RtlInitializeCriticalSection(&ConsoleLock);
167 SetConsoleCtrlHandler(DefaultConsoleCtrlHandler, TRUE);
168
169 /* Insert more dll attach stuff here! */
170
171 DllInitialized = TRUE;
172 break;
173
174 case DLL_PROCESS_DETACH:
175 DPRINT("DLL_PROCESS_DETACH\n");
176 if (DllInitialized == TRUE)
177 {
178 /* Insert more dll detach stuff here! */
179
180 NlsUninit();
181
182 /* Delete DLL critical section */
183 RtlDeleteCriticalSection (&ConsoleLock);
184 RtlDeleteCriticalSection (&DllLock);
185
186 /* Close object base directory */
187 NtClose(hBaseDir);
188
189 RtlFreeUnicodeString (&SystemDirectory);
190 RtlFreeUnicodeString (&WindowsDirectory);
191 }
192 break;
193
194 default:
195 break;
196 }
197
198 PREPARE_TESTS
199
200 return TRUE;
201 }
202
203 /* EOF */