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