update for HEAD-2003091401
[reactos.git] / 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 <kernel32/kernel32.h>
18
19 /* GLOBALS *******************************************************************/
20
21 extern UNICODE_STRING SystemDirectory;
22 extern UNICODE_STRING WindowsDirectory;
23
24 HANDLE hProcessHeap = NULL;
25 HANDLE hBaseDir = NULL;
26
27 static WINBOOL DllInitialized = FALSE;
28
29 BOOL STDCALL
30 DllMain(HANDLE hInst,
31         DWORD dwReason,
32         LPVOID lpReserved);
33
34 /* Critical section for various kernel32 data structures */
35 CRITICAL_SECTION DllLock;
36 CRITICAL_SECTION ConsoleLock;
37
38 extern BOOL WINAPI DefaultConsoleCtrlHandler(DWORD Event);
39
40 /* FUNCTIONS *****************************************************************/
41
42 static NTSTATUS
43 OpenBaseDirectory(PHANDLE DirHandle)
44 {
45   OBJECT_ATTRIBUTES ObjectAttributes;
46   UNICODE_STRING Name = UNICODE_STRING_INITIALIZER(L"\\BaseNamedObjects");
47   NTSTATUS Status;
48
49   InitializeObjectAttributes(&ObjectAttributes,
50                              &Name,
51                              OBJ_PERMANENT,
52                              NULL,
53                              NULL);
54
55   Status = NtOpenDirectoryObject(DirHandle,
56                                  DIRECTORY_ALL_ACCESS,
57                                  &ObjectAttributes);
58   if (!NT_SUCCESS(Status))
59     {
60       Status = NtCreateDirectoryObject(DirHandle,
61                                        DIRECTORY_ALL_ACCESS,
62                                        &ObjectAttributes);
63       if (!NT_SUCCESS(Status))
64         {
65           DbgPrint("NtCreateDirectoryObject() failed\n");
66         }
67
68       return Status;
69     }
70
71   return STATUS_SUCCESS;
72 }
73
74
75 BOOL STDCALL
76 DllMain(HANDLE hDll,
77         DWORD dwReason,
78         LPVOID lpReserved)
79 {
80   NTSTATUS Status;
81
82   (void)lpReserved;
83
84   DPRINT("DllMain(hInst %lx, dwReason %lu)\n",
85          hInst, dwReason);
86
87   switch (dwReason)
88     {
89       case DLL_PROCESS_ATTACH:
90         DPRINT("DLL_PROCESS_ATTACH\n");
91
92         LdrDisableThreadCalloutsForDll ((PVOID)hDll);
93
94         /*
95          * Connect to the csrss server
96          */
97         Status = CsrClientConnectToServer();
98         if (!NT_SUCCESS(Status))
99           {
100             DbgPrint("Failed to connect to csrss.exe (Status %lx)\n",
101                      Status);
102             ZwTerminateProcess(NtCurrentProcess(), Status);
103             return FALSE;
104           }
105
106         hProcessHeap = RtlGetProcessHeap();
107
108         /*
109          * Initialize WindowsDirectory and SystemDirectory
110          */
111         DPRINT("NtSystemRoot: %S\n",
112                SharedUserData->NtSystemRoot);
113         RtlCreateUnicodeString (&WindowsDirectory,
114                                 SharedUserData->NtSystemRoot);
115         SystemDirectory.MaximumLength = WindowsDirectory.MaximumLength + 18;
116         SystemDirectory.Length = WindowsDirectory.Length + 18;
117         SystemDirectory.Buffer = RtlAllocateHeap (hProcessHeap,
118                                                   0,
119                                                   SystemDirectory.MaximumLength);
120         wcscpy (SystemDirectory.Buffer, WindowsDirectory.Buffer);
121         wcscat (SystemDirectory.Buffer, L"\\System32");
122
123         /* Open object base directory */
124         Status = OpenBaseDirectory(&hBaseDir);
125         if (!NT_SUCCESS(Status))
126           {
127             DbgPrint("Failed to open object base directory (Status %lx)\n",
128                      Status);
129             return FALSE;
130           }
131
132         /* Initialize the DLL critical section */
133         RtlInitializeCriticalSection(&DllLock);
134
135         /* Initialize console ctrl handler */
136         RtlInitializeCriticalSection(&ConsoleLock);
137         SetConsoleCtrlHandler(DefaultConsoleCtrlHandler, TRUE);
138
139         /* Insert more dll attach stuff here! */
140
141         DllInitialized = TRUE;
142         break;
143
144       case DLL_PROCESS_DETACH:
145         DPRINT("DLL_PROCESS_DETACH\n");
146         if (DllInitialized == TRUE)
147           {
148             /* Insert more dll detach stuff here! */
149
150             /* Delete DLL critical section */
151             RtlDeleteCriticalSection (&ConsoleLock);
152             RtlDeleteCriticalSection (&DllLock);
153
154             /* Close object base directory */
155             NtClose(hBaseDir);
156
157             RtlFreeUnicodeString (&SystemDirectory);
158             RtlFreeUnicodeString (&WindowsDirectory);
159           }
160         break;
161
162       default:
163         break;
164     }
165
166    return TRUE;
167 }
168
169 /* EOF */