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