1 /* $Id: dllmain.c,v 1.21 2002/08/20 20:37:10 hyperion 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>
21 #include <kernel32/kernel32.h>
23 /* GLOBALS *******************************************************************/
25 extern UNICODE_STRING SystemDirectory
;
26 extern UNICODE_STRING WindowsDirectory
;
28 HANDLE hProcessHeap
= NULL
;
29 HANDLE hBaseDir
= NULL
;
31 static WINBOOL DllInitialized
= FALSE
;
33 WINBOOL STDCALL
DllMain (HANDLE hInst
,
34 ULONG ul_reason_for_call
,
37 /* Critical section for various kernel32 data structures */
38 CRITICAL_SECTION DllLock
;
40 /* FUNCTIONS *****************************************************************/
43 OpenBaseDirectory(PHANDLE DirHandle
)
45 OBJECT_ATTRIBUTES ObjectAttributes
;
46 UNICODE_STRING Name
= UNICODE_STRING_INITIALIZER(L
"\\BaseNamedObjects");
49 InitializeObjectAttributes(&ObjectAttributes
,
55 Status
= NtOpenDirectoryObject(DirHandle
,
58 if (!NT_SUCCESS(Status
))
60 Status
= NtCreateDirectoryObject(DirHandle
,
63 if (!NT_SUCCESS(Status
))
65 DbgPrint("NtCreateDirectoryObject() failed\n");
71 return STATUS_SUCCESS
;
76 DllMainCRTStartup(HANDLE hDll
, DWORD dwReason
, LPVOID lpReserved
)
78 return(DllMain(hDll
,dwReason
,lpReserved
));
83 ULONG ul_reason_for_call
,
86 DPRINT("DllMain(hInst %x, ul_reason_for_call %d)\n",
87 hInst
, ul_reason_for_call
);
89 switch (ul_reason_for_call
)
91 case DLL_PROCESS_ATTACH
:
95 DPRINT("DLL_PROCESS_ATTACH\n");
97 LdrDisableThreadCalloutsForDll ((PVOID
)hInst
);
100 * Connect to the csrss server
102 Status
= CsrClientConnectToServer();
103 if (!NT_SUCCESS(Status
))
105 DbgPrint("Failed to connect to csrss.exe: expect trouble "
106 "Status was %X\n", Status
);
107 ZwTerminateProcess(NtCurrentProcess(), Status
);
110 hProcessHeap
= RtlGetProcessHeap();
113 * Initialize WindowsDirectory and SystemDirectory
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
,
123 SystemDirectory
.MaximumLength
);
124 wcscpy (SystemDirectory
.Buffer
, WindowsDirectory
.Buffer
);
125 wcscat (SystemDirectory
.Buffer
, L
"\\System32");
127 /* Open object base directory */
128 Status
= OpenBaseDirectory(&hBaseDir
);
129 if (!NT_SUCCESS(Status
))
131 DbgPrint("Failed to open object base directory: expect trouble\n");
134 /* Initialize the DLL critical section */
135 RtlInitializeCriticalSection(&DllLock
);
137 /* Insert more dll attach stuff here! */
139 DllInitialized
= TRUE
;
143 case DLL_PROCESS_DETACH
:
145 DPRINT("DLL_PROCESS_DETACH\n");
146 if (DllInitialized
== TRUE
)
148 /* Insert more dll detach stuff here! */
150 /* Delete DLL critical section */
151 RtlDeleteCriticalSection (&DllLock
);
153 /* Close object base directory */
156 RtlFreeUnicodeString (&SystemDirectory
);
157 RtlFreeUnicodeString (&WindowsDirectory
);