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