:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[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 <ddk/ntddk.h>
15 #include <ntdll/csr.h>
16 #include <ntdll/ldr.h>
17 #include <windows.h>
18 #include <wchar.h>
19
20 #define NDEBUG
21 #include <kernel32/kernel32.h>
22
23 /* GLOBALS *******************************************************************/
24
25 extern UNICODE_STRING SystemDirectory;
26 extern UNICODE_STRING WindowsDirectory;
27
28 HANDLE hProcessHeap = NULL;
29 HANDLE hBaseDir = NULL;
30
31 static WINBOOL DllInitialized = FALSE;
32
33 WINBOOL STDCALL DllMain (HANDLE hInst,
34                          ULONG ul_reason_for_call,
35                          LPVOID lpReserved);
36
37 /* Critical section for various kernel32 data structures */
38 CRITICAL_SECTION DllLock;
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 WINAPI
76 DllMainCRTStartup(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
77 {
78    return(DllMain(hDll,dwReason,lpReserved));
79 }
80
81 WINBOOL STDCALL
82 DllMain(HANDLE hInst,
83         ULONG ul_reason_for_call,
84         LPVOID lpReserved)
85 {
86    DPRINT("DllMain(hInst %x, ul_reason_for_call %d)\n",
87           hInst, ul_reason_for_call);
88
89    switch (ul_reason_for_call)
90      {
91       case DLL_PROCESS_ATTACH:
92           {
93              NTSTATUS Status;
94
95              DPRINT("DLL_PROCESS_ATTACH\n");
96
97              LdrDisableThreadCalloutsForDll ((PVOID)hInst);
98
99              /*
100               * Connect to the csrss server
101               */
102              Status = CsrClientConnectToServer();
103              if (!NT_SUCCESS(Status))
104                {
105                   DbgPrint("Failed to connect to csrss.exe: expect trouble "
106                            "Status was %X\n", Status);
107                   ZwTerminateProcess(NtCurrentProcess(), Status);
108                }
109
110              hProcessHeap = RtlGetProcessHeap();
111
112              /*
113               * Initialize WindowsDirectory and SystemDirectory
114               */
115              DPRINT("NtSystemRoot: %S\n",
116                     SharedUserData->NtSystemRoot);
117              RtlCreateUnicodeString (&WindowsDirectory,
118                                      SharedUserData->NtSystemRoot);
119              SystemDirectory.MaximumLength = WindowsDirectory.MaximumLength + 18;
120              SystemDirectory.Length = WindowsDirectory.Length + 18;
121              SystemDirectory.Buffer = RtlAllocateHeap (hProcessHeap,
122                                                        0,
123                                                        SystemDirectory.MaximumLength);
124              wcscpy (SystemDirectory.Buffer, WindowsDirectory.Buffer);
125              wcscat (SystemDirectory.Buffer, L"\\System32");
126
127              /* Open object base directory */
128              Status = OpenBaseDirectory(&hBaseDir);
129              if (!NT_SUCCESS(Status))
130                {
131                   DbgPrint("Failed to open object base directory: expect trouble\n");
132                }
133
134              /* Initialize the DLL critical section */
135              RtlInitializeCriticalSection(&DllLock);
136              
137              /* Insert more dll attach stuff here! */
138
139              DllInitialized = TRUE;
140
141              break;
142           }
143       case DLL_PROCESS_DETACH:
144           {
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 (&DllLock);
152
153                  /* Close object base directory */
154                  NtClose(hBaseDir);
155                  
156                  RtlFreeUnicodeString (&SystemDirectory);
157                  RtlFreeUnicodeString (&WindowsDirectory);
158                }
159              break;
160           }
161       default:
162         break;
163     }
164    return TRUE;
165 }
166
167 /* EOF */