include/csrss/csrss.h: Include a define for the size of the common
[reactos.git] / reactos / lib / kernel32 / misc / dllmain.c
1 /* $Id: dllmain.c,v 1.19 2001/11/25 15:21:09 dwelch Exp $
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 <napi/shared_data.h>
18 #include <windows.h>
19 #include <wchar.h>
20
21 #define NDEBUG
22 #include <kernel32/kernel32.h>
23
24 /* GLOBALS *******************************************************************/
25
26 extern UNICODE_STRING SystemDirectory;
27 extern UNICODE_STRING WindowsDirectory;
28
29 HANDLE hProcessHeap = NULL;
30 HANDLE hBaseDir = NULL;
31
32 static WINBOOL DllInitialized = FALSE;
33
34 WINBOOL STDCALL DllMain (HANDLE hInst,
35 ULONG ul_reason_for_call,
36 LPVOID lpReserved);
37
38 /* Critical section for various kernel32 data structures */
39 CRITICAL_SECTION DllLock;
40
41 /* FUNCTIONS *****************************************************************/
42
43 static NTSTATUS
44 OpenBaseDirectory(PHANDLE DirHandle)
45 {
46 OBJECT_ATTRIBUTES ObjectAttributes;
47 UNICODE_STRING Name;
48 NTSTATUS Status;
49
50 RtlInitUnicodeString(&Name,
51 L"\\BaseNamedObjects");
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 WINAPI
80 DllMainCRTStartup(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
81 {
82 return(DllMain(hDll,dwReason,lpReserved));
83 }
84
85 WINBOOL STDCALL
86 DllMain(HANDLE hInst,
87 ULONG ul_reason_for_call,
88 LPVOID lpReserved)
89 {
90 DPRINT("DllMain(hInst %x, ul_reason_for_call %d)\n",
91 hInst, ul_reason_for_call);
92
93 switch (ul_reason_for_call)
94 {
95 case DLL_PROCESS_ATTACH:
96 {
97 NTSTATUS Status;
98 PKUSER_SHARED_DATA SharedUserData =
99 (PKUSER_SHARED_DATA)USER_SHARED_DATA_BASE;
100
101 DPRINT("DLL_PROCESS_ATTACH\n");
102
103 LdrDisableThreadCalloutsForDll ((PVOID)hInst);
104
105 /*
106 * Connect to the csrss server
107 */
108 Status = CsrClientConnectToServer();
109 if (!NT_SUCCESS(Status))
110 {
111 DbgPrint("Failed to connect to csrss.exe: expect trouble "
112 "Status was %X\n", Status);
113 ZwTerminateProcess(NtCurrentProcess(), Status);
114 }
115
116 hProcessHeap = RtlGetProcessHeap();
117
118 /*
119 * Initialize WindowsDirectory and SystemDirectory
120 */
121 DPRINT("NtSystemRoot: %S\n",
122 SharedUserData->NtSystemRoot);
123 RtlCreateUnicodeString (&WindowsDirectory,
124 SharedUserData->NtSystemRoot);
125 SystemDirectory.MaximumLength = WindowsDirectory.MaximumLength + 18;
126 SystemDirectory.Length = WindowsDirectory.Length + 18;
127 SystemDirectory.Buffer = RtlAllocateHeap (hProcessHeap,
128 0,
129 SystemDirectory.MaximumLength);
130 wcscpy (SystemDirectory.Buffer, WindowsDirectory.Buffer);
131 wcscat (SystemDirectory.Buffer, L"\\System32");
132
133 /* Open object base directory */
134 Status = OpenBaseDirectory(&hBaseDir);
135 if (!NT_SUCCESS(Status))
136 {
137 DbgPrint("Failed to open object base directory: expect trouble\n");
138 }
139
140 /* Initialize the DLL critical section */
141 RtlInitializeCriticalSection(&DllLock);
142
143 /* Insert more dll attach stuff here! */
144
145 DllInitialized = TRUE;
146
147 break;
148 }
149 case DLL_PROCESS_DETACH:
150 {
151 DPRINT("DLL_PROCESS_DETACH\n");
152 if (DllInitialized == TRUE)
153 {
154 /* Insert more dll detach stuff here! */
155
156 /* Delete DLL critical section */
157 RtlDeleteCriticalSection (&DllLock);
158
159 /* Close object base directory */
160 NtClose(hBaseDir);
161
162 RtlFreeUnicodeString (&SystemDirectory);
163 RtlFreeUnicodeString (&WindowsDirectory);
164 }
165 break;
166 }
167 default:
168 break;
169 }
170 return TRUE;
171 }
172
173 /* EOF */