Merge 13159:13510 from trunk
[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 = ROS_STRING_INITIALIZER(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();
102 if (!NT_SUCCESS(Status))
103 {
104 DbgPrint("Failed to connect to csrss.exe (Status %lx)\n",
105 Status);
106 ZwTerminateProcess(NtCurrentProcess(), Status);
107 return FALSE;
108 }
109
110 hProcessHeap = RtlGetProcessHeap();
111 hCurrentModule = hDll;
112
113 /*
114 * Initialize WindowsDirectory and SystemDirectory
115 */
116 DPRINT("NtSystemRoot: %S\n",
117 SharedUserData->NtSystemRoot);
118 RtlCreateUnicodeString (&WindowsDirectory,
119 SharedUserData->NtSystemRoot);
120 SystemDirectory.MaximumLength = WindowsDirectory.MaximumLength + 18;
121 SystemDirectory.Length = WindowsDirectory.Length + 18;
122 SystemDirectory.Buffer = RtlAllocateHeap (hProcessHeap,
123 0,
124 SystemDirectory.MaximumLength);
125 wcscpy (SystemDirectory.Buffer, WindowsDirectory.Buffer);
126 wcscat (SystemDirectory.Buffer, L"\\System32");
127
128 /* Open object base directory */
129 Status = OpenBaseDirectory(&hBaseDir);
130 if (!NT_SUCCESS(Status))
131 {
132 DbgPrint("Failed to open object base directory (Status %lx)\n",
133 Status);
134 return FALSE;
135 }
136
137 /* Initialize the DLL critical section */
138 RtlInitializeCriticalSection(&DllLock);
139
140 /* Initialize the National Language Support routines */
141 if (! NlsInit())
142 {
143 return FALSE;
144 }
145
146 /* Initialize console ctrl handler */
147 RtlInitializeCriticalSection(&ConsoleLock);
148 SetConsoleCtrlHandler(DefaultConsoleCtrlHandler, TRUE);
149
150 /* Insert more dll attach stuff here! */
151
152 DllInitialized = TRUE;
153 break;
154
155 case DLL_PROCESS_DETACH:
156 DPRINT("DLL_PROCESS_DETACH\n");
157 if (DllInitialized == TRUE)
158 {
159 /* Insert more dll detach stuff here! */
160
161 NlsUninit();
162
163 /* Delete DLL critical section */
164 RtlDeleteCriticalSection (&ConsoleLock);
165 RtlDeleteCriticalSection (&DllLock);
166
167 /* Close object base directory */
168 NtClose(hBaseDir);
169
170 RtlFreeUnicodeString (&SystemDirectory);
171 RtlFreeUnicodeString (&WindowsDirectory);
172 }
173 break;
174
175 default:
176 break;
177 }
178
179 return TRUE;
180 }
181
182 /* EOF */