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
;
20 BOOLEAN BaseRunningInServerProcess
= FALSE
;
22 WCHAR BaseDefaultPathBuffer
[6140];
24 HANDLE BaseNamedObjectDirectory
;
25 HMODULE hCurrentModule
= NULL
;
26 HMODULE kernel32_handle
= NULL
;
29 static BOOL DllInitialized
= FALSE
;
31 /* Critical section for various kernel32 data structures */
32 RTL_CRITICAL_SECTION BaseDllDirectoryLock
;
34 extern BOOL FASTCALL
NlsInit(VOID
);
35 extern VOID FASTCALL
NlsUninit(VOID
);
37 #define WIN_OBJ_DIR L"\\Windows"
38 #define SESSION_DIR L"\\Sessions"
40 /* FUNCTIONS *****************************************************************/
44 BaseCreateThreadPoolThread(IN PTHREAD_START_ROUTINE Function
,
46 OUT PHANDLE ThreadHandle
)
50 /* Create a Win32 thread */
51 *ThreadHandle
= CreateRemoteThread(NtCurrentProcess(),
60 /* Get the status value if we couldn't get a handle */
61 Status
= NtCurrentTeb()->LastStatusValue
;
62 if (NT_SUCCESS(Status
)) Status
= STATUS_UNSUCCESSFUL
;
66 /* Set success code */
67 Status
= STATUS_SUCCESS
;
76 BaseExitThreadPoolThread(IN NTSTATUS ExitStatus
)
79 ExitThread(ExitStatus
);
80 return STATUS_SUCCESS
;
90 BASESRV_API_CONNECTINFO ConnectInfo
;
91 ULONG ConnectInfoSize
= sizeof(ConnectInfo
);
92 WCHAR SessionDir
[256];
94 DPRINT("DllMain(hInst %p, dwReason %lu)\n",
97 Basep8BitStringToUnicodeString
= RtlAnsiStringToUnicodeString
;
99 /* Cache the PEB and Session ID */
100 Peb
= NtCurrentPeb();
101 SessionId
= Peb
->SessionId
;
105 case DLL_PROCESS_ATTACH
:
107 /* Set no filter intially */
108 GlobalTopLevelExceptionFilter
= RtlEncodePointer(NULL
);
110 /* Enable the Rtl thread pool and timer queue to use proper Win32 thread */
111 RtlSetThreadPoolStartFunc(BaseCreateThreadPoolThread
, BaseExitThreadPoolThread
);
113 /* Don't bother us for each thread */
114 LdrDisableThreadCalloutsForDll((PVOID
)hDll
);
116 /* Initialize default path to NULL */
117 RtlInitUnicodeString(&BaseDefaultPath
, NULL
);
119 /* Setup the Object Directory path */
122 /* Use the raw path */
123 wcscpy(SessionDir
, WIN_OBJ_DIR
);
127 /* Use the session path */
135 /* Connect to the Base Server */
136 Status
= CsrClientConnectToServer(SessionDir
,
137 BASESRV_SERVERDLL_INDEX
,
140 &BaseRunningInServerProcess
);
141 if (!NT_SUCCESS(Status
))
143 DPRINT1("Failed to connect to CSR (Status %lx)\n", Status
);
144 NtTerminateProcess(NtCurrentProcess(), Status
);
148 /* Get the server data */
149 ASSERT(Peb
->ReadOnlyStaticServerData
);
150 BaseStaticServerData
= Peb
->ReadOnlyStaticServerData
[BASESRV_SERVERDLL_INDEX
];
151 ASSERT(BaseStaticServerData
);
153 /* Check if we are running a CSR Server */
154 if (!BaseRunningInServerProcess
)
156 /* Set the termination port for the thread */
157 DPRINT("Creating new thread for CSR\n");
161 /* Initialize heap handle table */
162 BaseDllInitializeMemoryManager();
164 /* Set HMODULE for our DLL */
165 kernel32_handle
= hCurrentModule
= hDll
;
167 /* Set the directories */
168 BaseWindowsDirectory
= BaseStaticServerData
->WindowsDirectory
;
169 BaseWindowsSystemDirectory
= BaseStaticServerData
->WindowsSystemDirectory
;
171 /* Construct the default path (using the static buffer) */
172 _snwprintf(BaseDefaultPathBuffer
,
173 sizeof(BaseDefaultPathBuffer
) / sizeof(WCHAR
),
174 L
".;%wZ;%wZ\\system;%wZ;",
175 &BaseWindowsSystemDirectory
,
176 &BaseWindowsDirectory
,
177 &BaseWindowsDirectory
);
179 BaseDefaultPath
.Buffer
= BaseDefaultPathBuffer
;
180 BaseDefaultPath
.Length
= wcslen(BaseDefaultPathBuffer
) * sizeof(WCHAR
);
181 BaseDefaultPath
.MaximumLength
= sizeof(BaseDefaultPathBuffer
);
183 /* Use remaining part of the default path buffer for the append path */
184 BaseDefaultPathAppend
.Buffer
= (PWSTR
)((ULONG_PTR
)BaseDefaultPathBuffer
+ BaseDefaultPath
.Length
);
185 BaseDefaultPathAppend
.Length
= 0;
186 BaseDefaultPathAppend
.MaximumLength
= BaseDefaultPath
.MaximumLength
- BaseDefaultPath
.Length
;
188 /* Initialize command line */
191 /* Initialize the DLL critical section */
192 RtlInitializeCriticalSection(&BaseDllDirectoryLock
);
194 /* Initialize the National Language Support routines */
197 DPRINT1("NLS Init failed\n");
201 /* Initialize Console Support */
202 if (!ConDllInitialize(dwReason
, SessionDir
))
204 DPRINT1("Failed to set up console\n");
208 /* Initialize application certification globals */
209 InitializeListHead(&BasepAppCertDllsList
);
210 RtlInitializeCriticalSection(&gcsAppCert
);
212 /* Insert more dll attach stuff here! */
213 DllInitialized
= TRUE
;
217 case DLL_PROCESS_DETACH
:
219 if (DllInitialized
== TRUE
)
221 /* Uninitialize console support */
222 ConDllInitialize(dwReason
, NULL
);
224 /* Insert more dll detach stuff here! */
227 /* Delete DLL critical section */
228 RtlDeleteCriticalSection(&BaseDllDirectoryLock
);
233 case DLL_THREAD_ATTACH
:
235 /* ConDllInitialize sets the current console locale for the new thread */
236 return ConDllInitialize(dwReason
, NULL
);