2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/kernel32/misc/dllmain.c
5 * PURPOSE: Initialization
6 * PROGRAMMERS: Ariadne (ariadne@xs4all.nl)
7 * Aleksey Bragin (aleksey@reactos.org)
10 /* INCLUDES ******************************************************************/
17 /* GLOBALS *******************************************************************/
19 PBASE_STATIC_SERVER_DATA BaseStaticServerData
;
21 BOOLEAN BaseRunningInServerProcess
;
23 WCHAR BaseDefaultPathBuffer
[6140];
25 HANDLE BaseNamedObjectDirectory
;
26 HMODULE hCurrentModule
= NULL
;
27 HMODULE kernel32_handle
= NULL
;
30 static BOOL DllInitialized
= FALSE
;
32 /* Critical section for various kernel32 data structures */
33 RTL_CRITICAL_SECTION BaseDllDirectoryLock
;
35 extern BOOL FASTCALL
NlsInit(VOID
);
36 extern VOID FASTCALL
NlsUninit(VOID
);
38 #define WIN_OBJ_DIR L"\\Windows"
39 #define SESSION_DIR L"\\Sessions"
41 /* FUNCTIONS *****************************************************************/
45 BaseCreateThreadPoolThread(IN PTHREAD_START_ROUTINE Function
,
47 OUT PHANDLE ThreadHandle
)
51 /* Create a Win32 thread */
52 *ThreadHandle
= CreateRemoteThread(NtCurrentProcess(),
61 /* Get the status value if we couldn't get a handle */
62 Status
= NtCurrentTeb()->LastStatusValue
;
63 if (NT_SUCCESS(Status
)) Status
= STATUS_UNSUCCESSFUL
;
67 /* Set success code */
68 Status
= STATUS_SUCCESS
;
77 BaseExitThreadPoolThread(IN NTSTATUS ExitStatus
)
80 ExitThread(ExitStatus
);
81 return STATUS_SUCCESS
;
92 ULONG DummySize
= sizeof(Dummy
);
93 WCHAR SessionDir
[256];
95 DPRINT("DllMain(hInst %p, dwReason %lu)\n",
98 Basep8BitStringToUnicodeString
= RtlAnsiStringToUnicodeString
;
100 /* Cache the PEB and Session ID */
101 Peb
= NtCurrentPeb();
102 SessionId
= Peb
->SessionId
;
106 case DLL_PROCESS_ATTACH
:
108 /* Set no filter intially */
109 GlobalTopLevelExceptionFilter
= RtlEncodePointer(NULL
);
111 /* Enable the Rtl thread pool and timer queue to use proper Win32 thread */
112 RtlSetThreadPoolStartFunc(BaseCreateThreadPoolThread
, BaseExitThreadPoolThread
);
114 /* Don't bother us for each thread */
115 LdrDisableThreadCalloutsForDll((PVOID
)hDll
);
117 /* Initialize default path to NULL */
118 RtlInitUnicodeString(&BaseDefaultPath
, NULL
);
120 /* Setup the right Object Directory path */
123 /* Use the raw path */
124 wcscpy(SessionDir
, WIN_OBJ_DIR
);
128 /* Use the session path */
136 /* Connect to the base server */
137 Status
= CsrClientConnectToServer(SessionDir
,
138 BASESRV_SERVERDLL_INDEX
,
141 &BaseRunningInServerProcess
);
142 if (!NT_SUCCESS(Status
))
144 DPRINT1("Failed to connect to CSR (Status %lx)\n", Status
);
145 NtTerminateProcess(NtCurrentProcess(), Status
);
149 /* Get the server data */
150 ASSERT(Peb
->ReadOnlyStaticServerData
);
151 BaseStaticServerData
= Peb
->ReadOnlyStaticServerData
[BASESRV_SERVERDLL_INDEX
];
152 ASSERT(BaseStaticServerData
);
154 /* Check if we are running a CSR Server */
155 if (!BaseRunningInServerProcess
)
157 /* Set the termination port for the thread */
158 DPRINT("Creating new thread for CSR\n");
162 /* Initialize heap handle table */
163 BaseDllInitializeMemoryManager();
165 /* Set HMODULE for our DLL */
166 kernel32_handle
= hCurrentModule
= hDll
;
168 /* Set the directories */
169 BaseWindowsDirectory
= BaseStaticServerData
->WindowsDirectory
;
170 BaseWindowsSystemDirectory
= BaseStaticServerData
->WindowsSystemDirectory
;
172 /* Construct the default path (using the static buffer) */
173 _snwprintf(BaseDefaultPathBuffer
,
174 sizeof(BaseDefaultPathBuffer
) / sizeof(WCHAR
),
175 L
".;%wZ;%wZ\\system;%wZ;",
176 &BaseWindowsSystemDirectory
,
177 &BaseWindowsDirectory
,
178 &BaseWindowsDirectory
);
180 BaseDefaultPath
.Buffer
= BaseDefaultPathBuffer
;
181 BaseDefaultPath
.Length
= wcslen(BaseDefaultPathBuffer
) * sizeof(WCHAR
);
182 BaseDefaultPath
.MaximumLength
= sizeof(BaseDefaultPathBuffer
);
184 /* Use remaining part of the default path buffer for the append path */
185 BaseDefaultPathAppend
.Buffer
= (PWSTR
)((ULONG_PTR
)BaseDefaultPathBuffer
+ BaseDefaultPath
.Length
);
186 BaseDefaultPathAppend
.Length
= 0;
187 BaseDefaultPathAppend
.MaximumLength
= BaseDefaultPath
.MaximumLength
- BaseDefaultPath
.Length
;
189 /* Initialize command line */
192 /* Initialize the DLL critical section */
193 RtlInitializeCriticalSection(&BaseDllDirectoryLock
);
195 /* Initialize the National Language Support routines */
198 DPRINT1("NLS Init failed\n");
202 /* Initialize Console Support */
203 if (!BasepInitConsole())
205 DPRINT1("Failed to set up console\n");
209 /* Initialize application certification globals */
210 InitializeListHead(&BasepAppCertDllsList
);
211 RtlInitializeCriticalSection(&gcsAppCert
);
213 /* Insert more dll attach stuff here! */
214 DllInitialized
= TRUE
;
218 case DLL_PROCESS_DETACH
:
220 if (DllInitialized
== TRUE
)
222 /* Insert more dll detach stuff here! */
225 /* Uninitialize console support */
226 BasepUninitConsole();
228 /* Delete DLL critical section */
229 RtlDeleteCriticalSection(&BaseDllDirectoryLock
);