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
);
45 extern BOOL FASTCALL
NlsInit(VOID
);
46 extern VOID FASTCALL
NlsUninit(VOID
);
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 SYSTEM_BASIC_INFORMATION BaseCachedSysInfo
;
61 /* FUNCTIONS *****************************************************************/
65 OpenBaseDirectory(PHANDLE DirHandle
)
67 OBJECT_ATTRIBUTES ObjectAttributes
;
68 UNICODE_STRING Name
= RTL_CONSTANT_STRING(L
"\\BaseNamedObjects");
71 InitializeObjectAttributes(&ObjectAttributes
,
77 Status
= NtOpenDirectoryObject(DirHandle
,
78 DIRECTORY_ALL_ACCESS
&
79 ~(DELETE
| WRITE_DAC
| WRITE_OWNER
),
81 if (!NT_SUCCESS(Status
))
86 DPRINT("Opened BNO: %lx\n", *DirHandle
);
95 BaseQueryModuleData(IN LPSTR ModuleName
,
101 DPRINT1("BaseQueryModuleData called: %s %s %x %x %x\n",
115 BaseProcessInitPostImport(VOID
)
117 /* FIXME: Initialize TS pointers */
118 return STATUS_SUCCESS
;
123 BasepInitConsole(VOID
)
125 CSR_API_MESSAGE Request
;
128 PRTL_USER_PROCESS_PARAMETERS Parameters
= NtCurrentPeb()->ProcessParameters
;
130 WCHAR lpTest
[MAX_PATH
];
131 GetModuleFileNameW(NULL
, lpTest
, MAX_PATH
);
132 DPRINT("BasepInitConsole for : %S\n", lpTest
);
133 DPRINT("Our current console handles are: %lx, %lx, %lx %lx\n",
134 Parameters
->ConsoleHandle
, Parameters
->StandardInput
,
135 Parameters
->StandardOutput
, Parameters
->StandardError
);
137 /* We have nothing to do if this isn't a console app... */
138 if (RtlImageNtHeader(GetModuleHandle(NULL
))->OptionalHeader
.Subsystem
!=
139 IMAGE_SUBSYSTEM_WINDOWS_CUI
)
141 DPRINT("Image is not a console application\n");
142 Parameters
->ConsoleHandle
= NULL
;
146 /* Assume one is needed */
147 Request
.Data
.AllocConsoleRequest
.ConsoleNeeded
= TRUE
;
149 /* Handle the special flags given to us by BasepInitializeEnvironment */
150 if (Parameters
->ConsoleHandle
== HANDLE_DETACHED_PROCESS
)
152 /* No console to create */
153 DPRINT("No console to create\n");
154 Parameters
->ConsoleHandle
= NULL
;
155 Request
.Data
.AllocConsoleRequest
.ConsoleNeeded
= FALSE
;
157 else if (Parameters
->ConsoleHandle
== HANDLE_CREATE_NEW_CONSOLE
)
159 /* We'll get the real one soon */
160 DPRINT("Creating new console\n");
161 Parameters
->ConsoleHandle
= NULL
;
163 else if (Parameters
->ConsoleHandle
== HANDLE_CREATE_NO_WINDOW
)
165 /* We'll get the real one soon */
166 DPRINT1("NOT SUPPORTED: HANDLE_CREATE_NO_WINDOW\n");
167 Parameters
->ConsoleHandle
= NULL
;
171 if (Parameters
->ConsoleHandle
== INVALID_HANDLE_VALUE
)
173 Parameters
->ConsoleHandle
= 0;
175 DPRINT("Using existing console: %x\n", Parameters
->ConsoleHandle
);
178 /* Initialize Console Ctrl Handler */
179 ConsoleInitialized
= TRUE
;
180 RtlInitializeCriticalSection(&ConsoleLock
);
181 SetConsoleCtrlHandler(DefaultConsoleCtrlHandler
, TRUE
);
183 /* Now use the proper console handle */
184 Request
.Data
.AllocConsoleRequest
.Console
= Parameters
->ConsoleHandle
;
187 * Normally, we should be connecting to the Console CSR Server...
188 * but we don't have one yet, so we will instead simply send a create
189 * console message to the Base Server. When we finally have a Console
190 * Server, this code should be changed to send connection data instead.
192 * Also note that this connection should be made for any console app, even
193 * in the case above where -we- return.
195 CsrRequest
= MAKE_CSR_API(ALLOC_CONSOLE
, CSR_CONSOLE
);
196 Request
.Data
.AllocConsoleRequest
.CtrlDispatcher
= ConsoleControlDispatcher
;
197 Status
= CsrClientCallServer(&Request
,
200 sizeof(CSR_API_MESSAGE
));
201 if(!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Request
.Status
))
203 DPRINT1("CSR Failed to give us a console\n");
204 /* We're lying here, so at least the process can load... */
208 /* We got the handles, let's set them */
209 if ((Parameters
->ConsoleHandle
= Request
.Data
.AllocConsoleRequest
.Console
))
211 /* If we already had some, don't use the new ones */
212 if (!Parameters
->StandardInput
)
214 Parameters
->StandardInput
= Request
.Data
.AllocConsoleRequest
.InputHandle
;
216 if (!Parameters
->StandardOutput
)
218 Parameters
->StandardOutput
= Request
.Data
.AllocConsoleRequest
.OutputHandle
;
220 if (!Parameters
->StandardError
)
222 Parameters
->StandardError
= Request
.Data
.AllocConsoleRequest
.OutputHandle
;
226 DPRINT("Console setup: %lx, %lx, %lx, %lx\n",
227 Parameters
->ConsoleHandle
,
228 Parameters
->StandardInput
,
229 Parameters
->StandardOutput
,
230 Parameters
->StandardError
);
244 ULONG DummySize
= sizeof(Dummy
);
245 WCHAR SessionDir
[256];
247 DPRINT("DllMain(hInst %lx, dwReason %lu)\n",
250 Basep8BitStringToUnicodeString
= RtlAnsiStringToUnicodeString
;
252 /* Cache the PEB and Session ID */
253 Peb
= NtCurrentPeb();
254 SessionId
= Peb
->SessionId
;
258 case DLL_PROCESS_ATTACH
:
260 /* Don't bother us for each thread */
261 LdrDisableThreadCalloutsForDll((PVOID
)hDll
);
263 /* Setup the right Object Directory path */
266 /* Use the raw path */
267 wcscpy(SessionDir
, WIN_OBJ_DIR
);
271 /* Use the session path */
279 /* Connect to the base server */
280 DPRINT("Connecting to CSR...\n");
281 Status
= CsrClientConnectToServer(SessionDir
,
286 if (!NT_SUCCESS(Status
))
288 DPRINT1("Failed to connect to CSR (Status %lx)\n", Status
);
289 ZwTerminateProcess(NtCurrentProcess(), Status
);
293 /* Check if we are running a CSR Server */
296 /* Set the termination port for the thread */
297 DPRINT("Creating new thread for CSR\n");
301 hProcessHeap
= RtlGetProcessHeap();
302 RtlInitializeHandleTable(0xFFFF,
303 sizeof(BASE_HEAP_HANDLE_ENTRY
),
304 &BaseHeapHandleTable
);
305 hCurrentModule
= hDll
;
306 DPRINT("Heap: %p\n", hProcessHeap
);
309 * Initialize WindowsDirectory and SystemDirectory
311 DPRINT("NtSystemRoot: %S\n", SharedUserData
->NtSystemRoot
);
312 RtlCreateUnicodeString (&WindowsDirectory
, SharedUserData
->NtSystemRoot
);
313 SystemDirectory
.MaximumLength
= WindowsDirectory
.MaximumLength
+ 18;
314 SystemDirectory
.Length
= WindowsDirectory
.Length
+ 18;
315 SystemDirectory
.Buffer
= RtlAllocateHeap(hProcessHeap
,
317 SystemDirectory
.MaximumLength
);
318 if(SystemDirectory
.Buffer
== NULL
)
322 wcscpy(SystemDirectory
.Buffer
, WindowsDirectory
.Buffer
);
323 wcscat(SystemDirectory
.Buffer
, L
"\\System32");
325 /* Open object base directory */
326 Status
= OpenBaseDirectory(&hBaseDir
);
327 if (!NT_SUCCESS(Status
))
329 DPRINT1("Failed to open object base directory (Status %lx)\n", Status
);
333 /* Initialize the DLL critical section */
334 RtlInitializeCriticalSection(&DllLock
);
336 /* Initialize the National Language Support routines */
339 DPRINT1("NLS Init failed\n");
343 /* Initialize Console Support */
344 if (!BasepInitConsole())
346 DPRINT1("Failure to set up console\n");
350 /* Cache static system information */
351 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
353 sizeof(BaseCachedSysInfo
),
355 if (!NT_SUCCESS(Status
))
357 DPRINT1("Failure to get system information\n");
361 /* Insert more dll attach stuff here! */
362 DllInitialized
= TRUE
;
363 DPRINT("Initialization complete\n");
366 case DLL_PROCESS_DETACH
:
368 DPRINT("DLL_PROCESS_DETACH\n");
369 if (DllInitialized
== TRUE
)
371 /* Insert more dll detach stuff here! */
374 /* Delete DLL critical section */
375 if (ConsoleInitialized
== TRUE
)
377 ConsoleInitialized
= FALSE
;
378 RtlDeleteCriticalSection (&ConsoleLock
);
380 RtlDeleteCriticalSection (&DllLock
);
382 /* Close object base directory */
385 RtlFreeUnicodeString (&SystemDirectory
);
386 RtlFreeUnicodeString (&WindowsDirectory
);
399 InterlockedIncrement(IN OUT LONG
volatile *lpAddend
)
401 return _InterlockedIncrement(lpAddend
);
406 InterlockedDecrement(IN OUT LONG
volatile *lpAddend
)
408 return _InterlockedDecrement(lpAddend
);
413 InterlockedExchange(IN OUT LONG
volatile *Target
,
416 return _InterlockedExchange(Target
, Value
);
421 InterlockedExchangeAdd(IN OUT LONG
volatile *Addend
,
424 return _InterlockedExchangeAdd(Addend
, Value
);
429 InterlockedCompareExchange(IN OUT LONG
volatile *Destination
,
433 return _InterlockedCompareExchange(Destination
, Exchange
, Comperand
);