1 /* $Id: dllmain.c,v 1.19 2001/11/25 15:21:09 dwelch Exp $
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)
12 /* INCLUDES ******************************************************************/
14 #include <ddk/ntddk.h>
15 #include <ntdll/csr.h>
16 #include <ntdll/ldr.h>
17 #include <napi/shared_data.h>
22 #include <kernel32/kernel32.h>
24 /* GLOBALS *******************************************************************/
26 extern UNICODE_STRING SystemDirectory
;
27 extern UNICODE_STRING WindowsDirectory
;
29 HANDLE hProcessHeap
= NULL
;
30 HANDLE hBaseDir
= NULL
;
32 static WINBOOL DllInitialized
= FALSE
;
34 WINBOOL STDCALL
DllMain (HANDLE hInst
,
35 ULONG ul_reason_for_call
,
38 /* Critical section for various kernel32 data structures */
39 CRITICAL_SECTION DllLock
;
41 /* FUNCTIONS *****************************************************************/
44 OpenBaseDirectory(PHANDLE DirHandle
)
46 OBJECT_ATTRIBUTES ObjectAttributes
;
50 RtlInitUnicodeString(&Name
,
51 L
"\\BaseNamedObjects");
53 InitializeObjectAttributes(&ObjectAttributes
,
59 Status
= NtOpenDirectoryObject(DirHandle
,
62 if (!NT_SUCCESS(Status
))
64 Status
= NtCreateDirectoryObject(DirHandle
,
67 if (!NT_SUCCESS(Status
))
69 DbgPrint("NtCreateDirectoryObject() failed\n");
75 return STATUS_SUCCESS
;
80 DllMainCRTStartup(HANDLE hDll
, DWORD dwReason
, LPVOID lpReserved
)
82 return(DllMain(hDll
,dwReason
,lpReserved
));
87 ULONG ul_reason_for_call
,
90 DPRINT("DllMain(hInst %x, ul_reason_for_call %d)\n",
91 hInst
, ul_reason_for_call
);
93 switch (ul_reason_for_call
)
95 case DLL_PROCESS_ATTACH
:
98 PKUSER_SHARED_DATA SharedUserData
=
99 (PKUSER_SHARED_DATA
)USER_SHARED_DATA_BASE
;
101 DPRINT("DLL_PROCESS_ATTACH\n");
103 LdrDisableThreadCalloutsForDll ((PVOID
)hInst
);
106 * Connect to the csrss server
108 Status
= CsrClientConnectToServer();
109 if (!NT_SUCCESS(Status
))
111 DbgPrint("Failed to connect to csrss.exe: expect trouble "
112 "Status was %X\n", Status
);
113 ZwTerminateProcess(NtCurrentProcess(), Status
);
116 hProcessHeap
= RtlGetProcessHeap();
119 * Initialize WindowsDirectory and SystemDirectory
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
,
129 SystemDirectory
.MaximumLength
);
130 wcscpy (SystemDirectory
.Buffer
, WindowsDirectory
.Buffer
);
131 wcscat (SystemDirectory
.Buffer
, L
"\\System32");
133 /* Open object base directory */
134 Status
= OpenBaseDirectory(&hBaseDir
);
135 if (!NT_SUCCESS(Status
))
137 DbgPrint("Failed to open object base directory: expect trouble\n");
140 /* Initialize the DLL critical section */
141 RtlInitializeCriticalSection(&DllLock
);
143 /* Insert more dll attach stuff here! */
145 DllInitialized
= TRUE
;
149 case DLL_PROCESS_DETACH
:
151 DPRINT("DLL_PROCESS_DETACH\n");
152 if (DllInitialized
== TRUE
)
154 /* Insert more dll detach stuff here! */
156 /* Delete DLL critical section */
157 RtlDeleteCriticalSection (&DllLock
);
159 /* Close object base directory */
162 RtlFreeUnicodeString (&SystemDirectory
);
163 RtlFreeUnicodeString (&WindowsDirectory
);