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 ******************************************************************/
19 /* GLOBALS *******************************************************************/
21 extern UNICODE_STRING SystemDirectory
;
22 extern UNICODE_STRING WindowsDirectory
;
24 HANDLE hProcessHeap
= NULL
;
25 HMODULE hCurrentModule
= NULL
;
26 HANDLE hBaseDir
= NULL
;
29 BOOL ConsoleInitialized
= FALSE
;
31 static BOOL DllInitialized
= 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
);
44 extern PHANDLER_ROUTINE InitialHandler
[1];
45 extern PHANDLER_ROUTINE
* CtrlHandlers
;
46 extern ULONG NrCtrlHandlers
;
47 extern ULONG NrAllocatedHandlers
;
48 extern BOOL FASTCALL
NlsInit(VOID
);
49 extern VOID FASTCALL
NlsUninit(VOID
);
50 BOOLEAN InWindows
= FALSE
;
54 DuplicateConsoleHandle(HANDLE hConsole
,
55 DWORD dwDesiredAccess
,
59 #define WIN_OBJ_DIR L"\\Windows"
60 #define SESSION_DIR L"\\Sessions"
62 SYSTEM_BASIC_INFORMATION BaseCachedSysInfo
;
64 /* FUNCTIONS *****************************************************************/
68 OpenBaseDirectory(PHANDLE DirHandle
)
70 OBJECT_ATTRIBUTES ObjectAttributes
;
71 UNICODE_STRING Name
= RTL_CONSTANT_STRING(L
"\\BaseNamedObjects");
74 InitializeObjectAttributes(&ObjectAttributes
,
80 Status
= NtOpenDirectoryObject(DirHandle
,
81 DIRECTORY_ALL_ACCESS
&
82 ~(DELETE
| WRITE_DAC
| WRITE_OWNER
),
84 if (!NT_SUCCESS(Status
))
89 DPRINT("Opened BNO: %lx\n", *DirHandle
);
98 BaseQueryModuleData(IN LPSTR ModuleName
,
104 DPRINT1("BaseQueryModuleData called: %s %s %x %x %x\n",
118 BaseProcessInitPostImport(VOID
)
120 /* FIXME: Initialize TS pointers */
121 return STATUS_SUCCESS
;
126 BasepInitConsole(VOID
)
128 CSR_API_MESSAGE Request
;
131 BOOLEAN NotConsole
= FALSE
;
132 PRTL_USER_PROCESS_PARAMETERS Parameters
= NtCurrentPeb()->ProcessParameters
;
134 WCHAR lpTest
[MAX_PATH
];
135 GetModuleFileNameW(NULL
, lpTest
, MAX_PATH
);
136 DPRINT("BasepInitConsole for : %S\n", lpTest
);
137 DPRINT("Our current console handles are: %lx, %lx, %lx %lx\n",
138 Parameters
->ConsoleHandle
, Parameters
->StandardInput
,
139 Parameters
->StandardOutput
, Parameters
->StandardError
);
141 /* We have nothing to do if this isn't a console app... */
142 if (RtlImageNtHeader(GetModuleHandle(NULL
))->OptionalHeader
.Subsystem
!=
143 IMAGE_SUBSYSTEM_WINDOWS_CUI
)
145 DPRINT("Image is not a console application\n");
146 Parameters
->ConsoleHandle
= NULL
;
147 Request
.Data
.AllocConsoleRequest
.ConsoleNeeded
= FALSE
;
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
);
184 /* Initialize Console Ctrl Handler */
185 ConsoleInitialized
= TRUE
;
186 RtlInitializeCriticalSection(&ConsoleLock
);
187 NrAllocatedHandlers
= 1;
189 CtrlHandlers
= InitialHandler
;
190 CtrlHandlers
[0] = DefaultConsoleCtrlHandler
;
192 /* Now use the proper console handle */
193 Request
.Data
.AllocConsoleRequest
.Console
= Parameters
->ConsoleHandle
;
196 * Normally, we should be connecting to the Console CSR Server...
197 * but we don't have one yet, so we will instead simply send a create
198 * console message to the Base Server. When we finally have a Console
199 * Server, this code should be changed to send connection data instead.
201 CsrRequest
= MAKE_CSR_API(ALLOC_CONSOLE
, CSR_CONSOLE
);
202 Request
.Data
.AllocConsoleRequest
.CtrlDispatcher
= ConsoleControlDispatcher
;
203 Status
= CsrClientCallServer(&Request
,
206 sizeof(CSR_API_MESSAGE
));
207 if(!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Request
.Status
))
209 DPRINT1("CSR Failed to give us a console\n");
210 /* We're lying here, so at least the process can load... */
214 if (NotConsole
) return TRUE
;
216 /* We got the handles, let's set them */
217 if ((Parameters
->ConsoleHandle
= Request
.Data
.AllocConsoleRequest
.Console
))
219 /* If we already had some, don't use the new ones */
220 if (!Parameters
->StandardInput
)
222 Parameters
->StandardInput
= Request
.Data
.AllocConsoleRequest
.InputHandle
;
224 if (!Parameters
->StandardOutput
)
226 Parameters
->StandardOutput
= Request
.Data
.AllocConsoleRequest
.OutputHandle
;
228 if (!Parameters
->StandardError
)
230 Parameters
->StandardError
= Request
.Data
.AllocConsoleRequest
.OutputHandle
;
234 DPRINT("Console setup: %lx, %lx, %lx, %lx\n",
235 Parameters
->ConsoleHandle
,
236 Parameters
->StandardInput
,
237 Parameters
->StandardOutput
,
238 Parameters
->StandardError
);
252 ULONG DummySize
= sizeof(Dummy
);
253 WCHAR SessionDir
[256];
255 DPRINT("DllMain(hInst %lx, dwReason %lu)\n",
258 Basep8BitStringToUnicodeString
= RtlAnsiStringToUnicodeString
;
260 /* Cache the PEB and Session ID */
261 Peb
= NtCurrentPeb();
262 SessionId
= Peb
->SessionId
;
266 case DLL_PROCESS_ATTACH
:
268 /* Don't bother us for each thread */
269 LdrDisableThreadCalloutsForDll((PVOID
)hDll
);
271 /* Setup the right Object Directory path */
274 /* Use the raw path */
275 wcscpy(SessionDir
, WIN_OBJ_DIR
);
279 /* Use the session path */
287 /* Connect to the base server */
288 DPRINT("Connecting to CSR...\n");
289 Status
= CsrClientConnectToServer(SessionDir
,
294 if (!NT_SUCCESS(Status
))
296 DPRINT1("Failed to connect to CSR (Status %lx)\n", Status
);
297 ZwTerminateProcess(NtCurrentProcess(), Status
);
301 /* Check if we are running a CSR Server */
304 /* Set the termination port for the thread */
305 DPRINT("Creating new thread for CSR\n");
309 hProcessHeap
= RtlGetProcessHeap();
310 RtlInitializeHandleTable(0xFFFF,
311 sizeof(BASE_HEAP_HANDLE_ENTRY
),
312 &BaseHeapHandleTable
);
313 hCurrentModule
= hDll
;
314 DPRINT("Heap: %p\n", hProcessHeap
);
317 * Initialize WindowsDirectory and SystemDirectory
319 DPRINT("NtSystemRoot: %S\n", SharedUserData
->NtSystemRoot
);
320 RtlCreateUnicodeString (&WindowsDirectory
, SharedUserData
->NtSystemRoot
);
321 SystemDirectory
.MaximumLength
= WindowsDirectory
.MaximumLength
+ 18;
322 SystemDirectory
.Length
= WindowsDirectory
.Length
+ 18;
323 SystemDirectory
.Buffer
= RtlAllocateHeap(hProcessHeap
,
325 SystemDirectory
.MaximumLength
);
326 if(SystemDirectory
.Buffer
== NULL
)
330 wcscpy(SystemDirectory
.Buffer
, WindowsDirectory
.Buffer
);
331 wcscat(SystemDirectory
.Buffer
, L
"\\System32");
333 /* Initialize command line */
336 /* Open object base directory */
337 Status
= OpenBaseDirectory(&hBaseDir
);
338 if (!NT_SUCCESS(Status
))
340 DPRINT1("Failed to open object base directory (Status %lx)\n", Status
);
344 /* Initialize the DLL critical section */
345 RtlInitializeCriticalSection(&DllLock
);
347 /* Initialize the National Language Support routines */
350 DPRINT1("NLS Init failed\n");
354 /* Initialize Console Support */
355 if (!BasepInitConsole())
357 DPRINT1("Failure to set up console\n");
361 /* Cache static system information */
362 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
364 sizeof(BaseCachedSysInfo
),
366 if (!NT_SUCCESS(Status
))
368 DPRINT1("Failure to get system information\n");
372 /* Insert more dll attach stuff here! */
373 DllInitialized
= TRUE
;
374 DPRINT("Initialization complete\n");
377 case DLL_PROCESS_DETACH
:
379 DPRINT("DLL_PROCESS_DETACH\n");
380 if (DllInitialized
== TRUE
)
382 /* Insert more dll detach stuff here! */
385 /* Delete DLL critical section */
386 if (ConsoleInitialized
== TRUE
)
388 ConsoleInitialized
= FALSE
;
389 RtlDeleteCriticalSection (&ConsoleLock
);
391 RtlDeleteCriticalSection (&DllLock
);
393 /* Close object base directory */
396 RtlFreeUnicodeString (&SystemDirectory
);
397 RtlFreeUnicodeString (&WindowsDirectory
);
408 #undef InterlockedIncrement
409 #undef InterlockedDecrement
410 #undef InterlockedExchange
411 #undef InterlockedExchangeAdd
412 #undef InterlockedCompareExchange
416 InterlockedIncrement(IN OUT LONG
volatile *lpAddend
)
418 return _InterlockedIncrement(lpAddend
);
423 InterlockedDecrement(IN OUT LONG
volatile *lpAddend
)
425 return _InterlockedDecrement(lpAddend
);
430 InterlockedExchange(IN OUT LONG
volatile *Target
,
433 return _InterlockedExchange(Target
, Value
);
438 InterlockedExchangeAdd(IN OUT LONG
volatile *Addend
,
441 return _InterlockedExchangeAdd(Addend
, Value
);
446 InterlockedCompareExchange(IN OUT LONG
volatile *Destination
,
450 return _InterlockedCompareExchange(Destination
, Exchange
, Comperand
);