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 ******************************************************************/
17 #include "../include/debug.h"
19 /* GLOBALS *******************************************************************/
21 extern UNICODE_STRING SystemDirectory
;
22 extern UNICODE_STRING WindowsDirectory
;
24 HANDLE hProcessHeap
= NULL
;
25 HMODULE hCurrentModule
= NULL
;
26 HANDLE hBaseDir
= NULL
;
30 static BOOL DllInitialized
= FALSE
;
31 static BOOL ConsoleInitialized
= FALSE
;
38 /* Critical section for various kernel32 data structures */
39 RTL_CRITICAL_SECTION DllLock
;
40 RTL_CRITICAL_SECTION ConsoleLock
;
42 extern BOOL WINAPI
DefaultConsoleCtrlHandler(DWORD Event
);
43 extern __declspec(noreturn
) VOID CALLBACK
ConsoleControlDispatcher(DWORD CodeAndFlag
);
45 extern BOOL FASTCALL
NlsInit();
46 extern VOID FASTCALL
NlsUninit();
47 BOOLEAN InWindows
= FALSE
;
51 DuplicateConsoleHandle(HANDLE hConsole
,
52 DWORD dwDesiredAccess
,
56 #define WIN_OBJ_DIR L"\\Windows"
57 #define SESSION_DIR L"\\Sessions"
59 /* FUNCTIONS *****************************************************************/
63 OpenBaseDirectory(PHANDLE DirHandle
)
65 OBJECT_ATTRIBUTES ObjectAttributes
;
66 UNICODE_STRING Name
= RTL_CONSTANT_STRING(L
"\\BaseNamedObjects");
69 InitializeObjectAttributes(&ObjectAttributes
,
75 Status
= NtOpenDirectoryObject(DirHandle
,
76 DIRECTORY_ALL_ACCESS
&
77 ~(DELETE
| WRITE_DAC
| WRITE_OWNER
),
79 if (!NT_SUCCESS(Status
))
81 /* FIXME: It's not our job to create the BNO directory, csr does it */
82 Status
= NtCreateDirectoryObject(DirHandle
,
85 if (!NT_SUCCESS(Status
))
87 DPRINT1("NtCreateDirectoryObject() failed\n");
91 DPRINT("Opened BNO: %lx\n", *DirHandle
);
100 BaseQueryModuleData(IN LPSTR ModuleName
,
106 DPRINT1("BaseQueryModuleData called: %s %s %x %x %x\n",
120 BaseProcessInitPostImport(VOID
)
122 /* FIXME: Initialize TS pointers */
123 return STATUS_SUCCESS
;
128 BasepInitConsole(VOID
)
130 CSR_API_MESSAGE Request
;
133 PRTL_USER_PROCESS_PARAMETERS Parameters
= NtCurrentPeb()->ProcessParameters
;
135 WCHAR lpTest
[MAX_PATH
];
136 GetModuleFileNameW(NULL
, lpTest
, MAX_PATH
);
137 DPRINT("BasepInitConsole for : %S\n", lpTest
);
138 DPRINT("Our current console handles are: %lx, %lx, %lx %lx\n",
139 Parameters
->ConsoleHandle
, Parameters
->StandardInput
,
140 Parameters
->StandardOutput
, Parameters
->StandardError
);
142 /* We have nothing to do if this isn't a console app... */
143 if (RtlImageNtHeader(GetModuleHandle(NULL
))->OptionalHeader
.Subsystem
!=
144 IMAGE_SUBSYSTEM_WINDOWS_CUI
)
146 DPRINT("Image is not a console application\n");
147 Parameters
->ConsoleHandle
= NULL
;
151 /* Assume one is needed */
152 Request
.Data
.AllocConsoleRequest
.ConsoleNeeded
= TRUE
;
154 /* Handle the special flags given to us by BasepInitializeEnvironment */
155 if (Parameters
->ConsoleHandle
== HANDLE_DETACHED_PROCESS
)
157 /* No console to create */
158 DPRINT("No console to create\n");
159 Parameters
->ConsoleHandle
= NULL
;
160 Request
.Data
.AllocConsoleRequest
.ConsoleNeeded
= FALSE
;
162 else if (Parameters
->ConsoleHandle
== HANDLE_CREATE_NEW_CONSOLE
)
164 /* We'll get the real one soon */
165 DPRINT("Creating new console\n");
166 Parameters
->ConsoleHandle
= NULL
;
168 else if (Parameters
->ConsoleHandle
== HANDLE_CREATE_NO_WINDOW
)
170 /* We'll get the real one soon */
171 DPRINT1("NOT SUPPORTED: HANDLE_CREATE_NO_WINDOW\n");
172 Parameters
->ConsoleHandle
= NULL
;
176 if (Parameters
->ConsoleHandle
== INVALID_HANDLE_VALUE
)
178 Parameters
->ConsoleHandle
= 0;
180 DPRINT("Using existing console: %x\n", Parameters
->ConsoleHandle
);
183 /* Initialize Console Ctrl Handler */
184 ConsoleInitialized
= TRUE
;
185 RtlInitializeCriticalSection(&ConsoleLock
);
186 SetConsoleCtrlHandler(DefaultConsoleCtrlHandler
, TRUE
);
188 /* Now use the proper console handle */
189 Request
.Data
.AllocConsoleRequest
.Console
= Parameters
->ConsoleHandle
;
192 * Normally, we should be connecting to the Console CSR Server...
193 * but we don't have one yet, so we will instead simply send a create
194 * console message to the Base Server. When we finally have a Console
195 * Server, this code should be changed to send connection data instead.
197 * Also note that this connection should be made for any console app, even
198 * in the case above where -we- return.
200 CsrRequest
= MAKE_CSR_API(ALLOC_CONSOLE
, CSR_CONSOLE
);
201 Request
.Data
.AllocConsoleRequest
.CtrlDispatcher
= ConsoleControlDispatcher
;
202 Status
= CsrClientCallServer(&Request
,
205 sizeof(CSR_API_MESSAGE
));
206 if(!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Request
.Status
))
208 DPRINT1("CSR Failed to give us a console\n");
209 /* We're lying here, so at least the process can load... */
213 /* We got the handles, let's set them */
214 if ((Parameters
->ConsoleHandle
= Request
.Data
.AllocConsoleRequest
.Console
))
216 /* If we already had some, don't use the new ones */
217 if (!Parameters
->StandardInput
)
219 Parameters
->StandardInput
= Request
.Data
.AllocConsoleRequest
.InputHandle
;
221 if (!Parameters
->StandardOutput
)
223 Parameters
->StandardOutput
= Request
.Data
.AllocConsoleRequest
.OutputHandle
;
225 if (!Parameters
->StandardError
)
227 Parameters
->StandardError
= Request
.Data
.AllocConsoleRequest
.OutputHandle
;
231 DPRINT("Console setup: %lx, %lx, %lx, %lx\n",
232 Parameters
->ConsoleHandle
,
233 Parameters
->StandardInput
,
234 Parameters
->StandardOutput
,
235 Parameters
->StandardError
);
249 ULONG DummySize
= sizeof(Dummy
);
250 WCHAR SessionDir
[256];
252 DPRINT("DllMain(hInst %lx, dwReason %lu)\n",
255 /* Cache the PEB and Session ID */
256 Peb
= NtCurrentPeb();
257 SessionId
= Peb
->SessionId
;
261 case DLL_PROCESS_ATTACH
:
263 /* OK, yes, this is really retarded but it works for now */
264 InWindows
= NtCurrentPeb()->BeingDebugged
;
267 * CreateProcess will run in the real kernel32 and it will write
268 * its own BaseProcessStartThunk EIP in the CONTEXT that ZwContinue
269 * will get. We'll be first called by Ldr while initializing, and we'll
270 * be wrapped in 3 layers of SEH, followed by two frames, finally
271 * followed by our CONTEXT on the stack. We'll modify the EIP in it
272 * to match the correct one (our own) and then everything works.
273 * Tested on XP and 2K3, probably doesn't work in 2K.
278 * Due to yet another bug in how Windows handles .local, LDR will
279 * actually end up loading us twice. The second time will be the
280 * "official" load, at a totally different address. It will be,
281 * it will be at -that- address that all the APIs will be called.
282 * However, that address is dynamic while this one will be static,
283 * so we'll do initilization with this one. Plus, at this one,
284 * we know exactly that we're within 3 SEH layers.
286 if (hDll
== (HANDLE
)0x7c800000)
289 Eip
= (PULONG
)*(PULONG
)*(PULONG
)NtCurrentTeb()->Tib
.ExceptionList
+
291 FIELD_OFFSET(CONTEXT
, Eip
) / sizeof(ULONG
);
292 *Eip
= (ULONG
)BaseProcessStartThunk
;
296 /* Don't bother us for each thread */
297 LdrDisableThreadCalloutsForDll((PVOID
)hDll
);
299 /* Setup the right Object Directory path */
302 /* Use the raw path */
303 wcscpy(SessionDir
, WIN_OBJ_DIR
);
307 /* Use the session path */
315 /* Connect to the base server */
316 DPRINT("Connecting to CSR...\n");
317 Status
= CsrClientConnectToServer(SessionDir
,
322 if (!NT_SUCCESS(Status
))
324 DPRINT1("Failed to connect to CSR (Status %lx)\n", Status
);
325 ZwTerminateProcess(NtCurrentProcess(), Status
);
329 /* Check if we are running a CSR Server */
332 /* Set the termination port for the thread */
333 DPRINT("Creating new thread for CSR\n");
337 hProcessHeap
= RtlGetProcessHeap();
338 hCurrentModule
= hDll
;
339 DPRINT("Heap: %p\n", hProcessHeap
);
342 * Initialize WindowsDirectory and SystemDirectory
344 DPRINT("NtSystemRoot: %S\n", SharedUserData
->NtSystemRoot
);
345 RtlCreateUnicodeString (&WindowsDirectory
, SharedUserData
->NtSystemRoot
);
346 SystemDirectory
.MaximumLength
= WindowsDirectory
.MaximumLength
+ 18;
347 SystemDirectory
.Length
= WindowsDirectory
.Length
+ 18;
348 SystemDirectory
.Buffer
= RtlAllocateHeap(hProcessHeap
,
350 SystemDirectory
.MaximumLength
);
351 wcscpy(SystemDirectory
.Buffer
, WindowsDirectory
.Buffer
);
352 wcscat(SystemDirectory
.Buffer
, L
"\\System32");
354 /* Open object base directory */
355 Status
= OpenBaseDirectory(&hBaseDir
);
356 if (!NT_SUCCESS(Status
))
358 DPRINT1("Failed to open object base directory (Status %lx)\n", Status
);
362 /* Initialize the DLL critical section */
363 RtlInitializeCriticalSection(&DllLock
);
365 /* Initialize the National Language Support routines */
368 DPRINT1("NLS Init failed\n");
372 /* Initialize Console Support */
373 if (!BasepInitConsole())
375 DPRINT1("Failure to set up console\n");
379 /* Insert more dll attach stuff here! */
380 DllInitialized
= TRUE
;
381 DPRINT("Initialization complete\n");
384 case DLL_PROCESS_DETACH
:
386 DPRINT("DLL_PROCESS_DETACH\n");
387 if (DllInitialized
== TRUE
)
389 /* Insert more dll detach stuff here! */
392 /* Delete DLL critical section */
393 if (ConsoleInitialized
== TRUE
)
395 RtlDeleteCriticalSection (&ConsoleLock
);
397 RtlDeleteCriticalSection (&DllLock
);
399 /* Close object base directory */
402 RtlFreeUnicodeString (&SystemDirectory
);
403 RtlFreeUnicodeString (&WindowsDirectory
);