ReactOS  0.4.13-dev-257-gfabbd7c
dllmain.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS system libraries
4  * FILE: dll/win32/kernel32/client/dllmain.c
5  * PURPOSE: Initialization
6  * PROGRAMMERS: Ariadne (ariadne@xs4all.nl)
7  * Aleksey Bragin (aleksey@reactos.org)
8  */
9 
10 /* INCLUDES ******************************************************************/
11 
12 #include <k32.h>
13 
14 #define NDEBUG
15 #include <debug.h>
16 
17 /* GLOBALS *******************************************************************/
18 
21 
23 
30 
31 /* Critical section for various kernel32 data structures */
33 
34 extern BOOL FASTCALL NlsInit(VOID);
35 extern VOID FASTCALL NlsUninit(VOID);
36 
37 #define WIN_OBJ_DIR L"\\Windows"
38 #define SESSION_DIR L"\\Sessions"
39 
40 /* FUNCTIONS *****************************************************************/
41 
43 NTAPI
46  OUT PHANDLE ThreadHandle)
47 {
49 
50  /* Create a Win32 thread */
51  *ThreadHandle = CreateRemoteThread(NtCurrentProcess(),
52  NULL,
53  0,
54  Function,
55  Parameter,
57  NULL);
58  if (!(*ThreadHandle))
59  {
60  /* Get the status value if we couldn't get a handle */
61  Status = NtCurrentTeb()->LastStatusValue;
63  }
64  else
65  {
66  /* Set success code */
68  }
69 
70  /* All done */
71  return Status;
72 }
73 
75 NTAPI
77 {
78  /* Exit the thread */
80  return STATUS_SUCCESS;
81 }
82 
83 BOOL
84 WINAPI
87  LPVOID lpReserved)
88 {
90  BASESRV_API_CONNECTINFO ConnectInfo;
91  ULONG ConnectInfoSize = sizeof(ConnectInfo);
92  WCHAR SessionDir[256];
93 
94  DPRINT("DllMain(hInst %p, dwReason %lu)\n",
95  hDll, dwReason);
96 
98 
99  /* Cache the PEB and Session ID */
100  Peb = NtCurrentPeb();
102 
103  switch (dwReason)
104  {
105  case DLL_PROCESS_ATTACH:
106  {
107  /* Set no filter initially */
109 
110  /* Enable the Rtl thread pool and timer queue to use proper Win32 thread */
112 
113  /* Register the manifest prober routine */
115 
116  /* Don't bother us for each thread */
118 
119  /* Initialize default path to NULL */
121 
122  /* Setup the Object Directory path */
123  if (!SessionId)
124  {
125  /* Use the raw path */
126  wcscpy(SessionDir, WIN_OBJ_DIR);
127  }
128  else
129  {
130  /* Use the session path */
131  swprintf(SessionDir,
132  L"%ws\\%ld%ws",
133  SESSION_DIR,
134  SessionId,
135  WIN_OBJ_DIR);
136  }
137 
138  /* Connect to the Base Server */
139  Status = CsrClientConnectToServer(SessionDir,
141  &ConnectInfo,
142  &ConnectInfoSize,
144  if (!NT_SUCCESS(Status))
145  {
146  DPRINT1("Failed to connect to CSR (Status %lx)\n", Status);
148  return FALSE;
149  }
150 
151  /* Get the server data */
155 
156  /* Check if we are running a CSR Server */
158  {
159  /* Set the termination port for the thread */
160  DPRINT("Creating new thread for CSR\n");
161  CsrNewThread();
162  }
163 
164  /* Initialize heap handle table */
166 
167  /* Set HMODULE for our DLL */
169 
170  /* Set the directories */
173 
174  /* Construct the default path (using the static buffer) */
176  sizeof(BaseDefaultPathBuffer),
177  L".;%wZ;%wZ\\system;%wZ;",
181  if (!NT_SUCCESS(Status))
182  {
183  DPRINT1("NLS Init failed\n");
184  return FALSE;
185  }
186 
190 
191  /* Use remaining part of the default path buffer for the append path */
195 
196  /* Initialize command line */
198 
199  /* Initialize the DLL critical section */
201 
202  /* Initialize the National Language Support routines */
203  if (!NlsInit())
204  {
205  DPRINT1("NLS Init failed\n");
206  return FALSE;
207  }
208 
209  /* Initialize Console Support */
210  if (!ConDllInitialize(dwReason, SessionDir))
211  {
212  DPRINT1("Failed to set up console\n");
213  return FALSE;
214  }
215 
216  /* Initialize application certification globals */
219 
220  /* Insert more dll attach stuff here! */
222  break;
223  }
224 
225  case DLL_PROCESS_DETACH:
226  {
227  if (DllInitialized != FALSE)
228  {
229  /* Uninitialize console support */
231 
232  /* Insert more dll detach stuff here! */
233  NlsUninit();
234 
235  /* Delete DLL critical section */
237  }
238  break;
239  }
240 
241  case DLL_THREAD_ATTACH:
242  {
243  /* ConDllInitialize sets the current console locale for the new thread */
244  return ConDllInitialize(dwReason, NULL);
245  }
246 
247  default:
248  break;
249  }
250 
251  return TRUE;
252 }
253 
254 /* EOF */
UNICODE_STRING BaseDefaultPath
Definition: path.c:21
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
HMODULE kernel32_handle
Definition: dllmain.c:26
PPEB Peb
Definition: dllmain.c:27
RTL_CRITICAL_SECTION gcsAppCert
Definition: proc.c:29
static BOOL DllInitialized
Definition: dllmain.c:29
NTSTATUS NTAPI RtlSetThreadPoolStartFunc(_In_ PRTL_START_POOL_THREAD StartPoolThread, _In_ PRTL_EXIT_POOL_THREAD ExitPoolThread)
VOID FASTCALL NlsUninit(VOID)
Definition: nls.c:126
PBASE_STATIC_SERVER_DATA BaseStaticServerData
Definition: dllmain.c:19
USHORT MaximumLength
Definition: env_spec_w32.h:370
#define DLL_THREAD_ATTACH
Definition: compat.h:121
uint16_t * PWSTR
Definition: typedefs.h:54
ULONG SessionId
Definition: dllmain.c:28
UNICODE_STRING WindowsDirectory
Definition: base.h:113
LONG NTSTATUS
Definition: precomp.h:26
#define SESSION_DIR
Definition: dllmain.c:38
BOOL FASTCALL NlsInit(VOID)
Definition: nls.c:69
#define DLL_PROCESS_ATTACH
Definition: compat.h:120
BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
Definition: dllmain.c:52
_In_ PVOID Parameter
Definition: ldrtypes.h:240
#define FASTCALL
Definition: nt_native.h:50
UNICODE_STRING WindowsSystemDirectory
Definition: base.h:114
ULONG(NTAPI * PTHREAD_START_ROUTINE)(PVOID Parameter)
Definition: rtltypes.h:562
uint32_t ULONG_PTR
Definition: typedefs.h:63
DWORD dwReason
Definition: misc.cpp:154
#define CREATE_SUSPENDED
Definition: winbase.h:178
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
static LPOVERLAPPED_COMPLETION_ROUTINE Function
Definition: sync.c:684
unsigned int BOOL
Definition: ntddk_ex.h:94
VOID NTAPI BaseDllInitializeMemoryManager(VOID)
Definition: heapmem.c:26
unsigned char BOOLEAN
NTSYSAPI PVOID WINAPI RtlEncodePointer(PVOID)
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:326
#define DLL_PROCESS_DETACH
Definition: compat.h:119
#define NtCurrentProcess()
Definition: nt_native.h:1657
HMODULE hCurrentModule
Definition: dllmain.c:25
HANDLE BaseNamedObjectDirectory
Definition: dllmain.c:24
BOOLEAN BaseRunningInServerProcess
Definition: dllmain.c:20
UNICODE_STRING BaseWindowsDirectory
Definition: path.c:20
#define BASESRV_SERVERDLL_INDEX
Definition: basemsg.h:15
__wchar_t WCHAR
Definition: xmlstorage.h:180
NTSYSAPI NTSTATUS NTAPI RtlInitializeCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define swprintf(buf, format,...)
Definition: sprintf.c:56
#define WINAPI
Definition: msvc.h:8
NTSTATUS NTAPI BaseExitThreadPoolThread(IN NTSTATUS ExitStatus)
Definition: dllmain.c:76
unsigned long DWORD
Definition: ntddk_ex.h:95
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
NTSTATUS NTAPI BaseCreateThreadPoolThread(IN PTHREAD_START_ROUTINE Function, IN PVOID Parameter, OUT PHANDLE ThreadHandle)
Definition: dllmain.c:44
VOID NTAPI LdrSetDllManifestProber(_In_ PLDR_MANIFEST_PROBER_ROUTINE Routine)
Definition: ldrapi.c:73
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
ULONG SessionId
Definition: btrfs_drv.h:1857
static const WCHAR L[]
Definition: oid.c:1250
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:414
WCHAR BaseDefaultPathBuffer[6140]
Definition: dllmain.c:22
static __inline NTSTATUS RtlStringCbPrintfW(_Out_writes_bytes_(cbDest) _Always_(_Post_z_) NTSTRSAFE_PWSTR pszDest, _In_ size_t cbDest, _In_ _Printf_format_string_ NTSTRSAFE_PCWSTR pszFormat,...)
Definition: ntstrsafe.h:1120
Status
Definition: gdiplustypes.h:24
NTSTATUS NTAPI CsrNewThread(VOID)
Definition: api.c:27
NTSTATUS NTAPI LdrDisableThreadCalloutsForDll(IN PVOID BaseAddress)
Definition: ldrapi.c:1187
RTL_CRITICAL_SECTION BaseDllDirectoryLock
Definition: dllmain.c:32
#define WIN_OBJ_DIR
Definition: dllmain.c:37
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
BOOLEAN WINAPI ConDllInitialize(IN ULONG Reason, IN PWSTR SessionDir)
Definition: init.c:338
#define NtCurrentPeb()
Definition: FLS.c:19
unsigned short USHORT
Definition: pedump.c:61
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
UNICODE_STRING BaseDefaultPathAppend
Definition: path.c:21
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
_In_ NTSTATUS ExitStatus
Definition: psfuncs.h:861
PRTL_CONVERT_STRING Basep8BitStringToUnicodeString
Definition: utils.c:26
#define DPRINT1
Definition: precomp.h:8
LIST_ENTRY BasepAppCertDllsList
Definition: proc.c:28
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
LPTOP_LEVEL_EXCEPTION_FILTER GlobalTopLevelExceptionFilter
Definition: except.c:158
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSTATUS NTAPI NtTerminateProcess(HANDLE ProcessHandle, LONG ExitStatus)
PVOID * ReadOnlyStaticServerData
Definition: ntddk_ex.h:263
VOID WINAPI InitCommandLines(VOID)
Definition: proc.c:872
NTSTATUS NTAPI BasepProbeForDllManifest(IN PVOID DllHandle, IN PCWSTR FullDllName, OUT PVOID *ActCtx)
Definition: actctx.c:136
NTSYSAPI NTSTATUS NTAPI RtlDeleteCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
return STATUS_SUCCESS
Definition: btrfs.c:2745
NTSTATUS NTAPI CsrClientConnectToServer(IN PWSTR ObjectDirectory, IN ULONG ServerId, IN PVOID ConnectionInfo, IN OUT PULONG ConnectionInfoSize, OUT PBOOLEAN ServerToServerCall)
Definition: connect.c:192
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
UNICODE_STRING BaseWindowsSystemDirectory
Definition: path.c:20
HANDLE WINAPI CreateRemoteThread(IN HANDLE hProcess, IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:133