ReactOS  0.4.14-dev-593-g1793dcc
dllmain.c File Reference
#include <k32.h>
#include <debug.h>
Include dependency graph for dllmain.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define WIN_OBJ_DIR   L"\\Windows"
 
#define SESSION_DIR   L"\\Sessions"
 

Functions

BOOL FASTCALL NlsInit (VOID)
 
VOID FASTCALL NlsUninit (VOID)
 
NTSTATUS NTAPI BaseCreateThreadPoolThread (IN PTHREAD_START_ROUTINE Function, IN PVOID Parameter, OUT PHANDLE ThreadHandle)
 
NTSTATUS NTAPI BaseExitThreadPoolThread (IN NTSTATUS ExitStatus)
 
BOOL WINAPI DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
 

Variables

PBASE_STATIC_SERVER_DATA BaseStaticServerData
 
BOOLEAN BaseRunningInServerProcess = FALSE
 
WCHAR BaseDefaultPathBuffer [6140]
 
HANDLE BaseNamedObjectDirectory
 
HMODULE hCurrentModule = NULL
 
HMODULE kernel32_handle = NULL
 
PPEB Peb
 
ULONG SessionId
 
static BOOL DllInitialized = FALSE
 
RTL_CRITICAL_SECTION BaseDllDirectoryLock
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file dllmain.c.

◆ SESSION_DIR

#define SESSION_DIR   L"\\Sessions"

Definition at line 38 of file dllmain.c.

◆ WIN_OBJ_DIR

#define WIN_OBJ_DIR   L"\\Windows"

Definition at line 37 of file dllmain.c.

Function Documentation

◆ BaseCreateThreadPoolThread()

NTSTATUS NTAPI BaseCreateThreadPoolThread ( IN PTHREAD_START_ROUTINE  Function,
IN PVOID  Parameter,
OUT PHANDLE  ThreadHandle 
)

Definition at line 44 of file dllmain.c.

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 }
LONG NTSTATUS
Definition: precomp.h:26
_In_ PVOID Parameter
Definition: ldrtypes.h:241
#define CREATE_SUSPENDED
Definition: winbase.h:178
static LPOVERLAPPED_COMPLETION_ROUTINE Function
Definition: sync.c:684
smooth NULL
Definition: ftsmooth.c:416
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
Status
Definition: gdiplustypes.h:24
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
return STATUS_SUCCESS
Definition: btrfs.c:2938
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:158

Referenced by DllMain().

◆ BaseExitThreadPoolThread()

NTSTATUS NTAPI BaseExitThreadPoolThread ( IN NTSTATUS  ExitStatus)

Definition at line 76 of file dllmain.c.

77 {
78  /* Exit the thread */
80  return STATUS_SUCCESS;
81 }
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:364
_In_ NTSTATUS ExitStatus
Definition: psfuncs.h:859
return STATUS_SUCCESS
Definition: btrfs.c:2938

Referenced by DllMain().

◆ DllMain()

BOOL WINAPI DllMain ( HANDLE  hDll,
DWORD  dwReason,
LPVOID  lpReserved 
)

Definition at line 85 of file dllmain.c.

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 }
UNICODE_STRING BaseDefaultPath
Definition: path.c:21
#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
UNICODE_STRING WindowsSystemDirectory
Definition: base.h:114
uint32_t ULONG_PTR
Definition: typedefs.h:63
DWORD dwReason
Definition: misc.cpp:154
VOID NTAPI BaseDllInitializeMemoryManager(VOID)
Definition: heapmem.c:26
NTSYSAPI PVOID WINAPI RtlEncodePointer(PVOID)
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define DLL_PROCESS_DETACH
Definition: compat.h:119
#define NtCurrentProcess()
Definition: nt_native.h:1657
HMODULE hCurrentModule
Definition: dllmain.c:25
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
NTSTRSAFEVAPI 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:1173
#define swprintf(buf, format,...)
Definition: sprintf.c:56
NTSTATUS NTAPI BaseExitThreadPoolThread(IN NTSTATUS ExitStatus)
Definition: dllmain.c:76
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
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:1896
static const WCHAR L[]
Definition: oid.c:1250
WCHAR BaseDefaultPathBuffer[6140]
Definition: dllmain.c:22
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
BOOLEAN WINAPI ConDllInitialize(IN ULONG Reason, IN PWSTR SessionDir)
Definition: init.c:338
#define NtCurrentPeb()
Definition: FLS.c:20
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
PRTL_CONVERT_STRING Basep8BitStringToUnicodeString
Definition: utils.c:26
#define DPRINT1
Definition: precomp.h:8
LIST_ENTRY BasepAppCertDllsList
Definition: proc.c:28
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:842
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)
NTSTATUS NTAPI CsrClientConnectToServer(IN PWSTR ObjectDirectory, IN ULONG ServerId, IN PVOID ConnectionInfo, IN OUT PULONG ConnectionInfoSize, OUT PBOOLEAN ServerToServerCall)
Definition: connect.c:197
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
UNICODE_STRING BaseWindowsSystemDirectory
Definition: path.c:20

◆ NlsInit()

BOOL FASTCALL NlsInit ( VOID  )

Definition at line 69 of file nls.c.

70 {
73  HANDLE Handle;
74 
77 
78  /*
79  * FIXME: Eventually this should be done only for the NLS Server
80  * process, but since we don't have anything like that (yet?) we
81  * always try to create the "\Nls" directory here.
82  */
83  RtlInitUnicodeString(&DirName, L"\\Nls");
84 
86  &DirName,
88  NULL,
89  NULL);
90 
92  {
93  NtClose(Handle);
94  }
95 
96  /* Setup ANSI code page. */
98  AnsiCodePage.SectionMapping = NtCurrentTeb()->ProcessEnvironmentBlock->AnsiCodePageData;
99 
103 
105 
106  /* Setup OEM code page. */
108  OemCodePage.SectionMapping = NtCurrentTeb()->ProcessEnvironmentBlock->OemCodePageData;
109 
114 
115  return TRUE;
116 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
static CODEPAGE_ENTRY AnsiCodePage
Definition: nls.c:46
#define TRUE
Definition: types.h:120
static LIST_ENTRY CodePageListHead
Definition: nls.c:45
#define OBJ_PERMANENT
Definition: winternl.h:226
#define InsertTailList(ListHead, Entry)
UINT CodePage
Definition: kernel32.h:65
smooth NULL
Definition: ftsmooth.c:416
_In_ HANDLE Handle
Definition: extypes.h:390
LIST_ENTRY Entry
Definition: kernel32.h:64
NTSYSAPI NTSTATUS NTAPI RtlInitializeCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
USHORT CodePage
Definition: precomp.h:32
static CODEPAGE_ENTRY OemCodePage
Definition: nls.c:47
static RTL_CRITICAL_SECTION CodePageListLock
Definition: nls.c:48
static const WCHAR L[]
Definition: oid.c:1250
VOID NTAPI RtlInitCodePageTable(IN PUSHORT TableBase, OUT PCPTABLEINFO CodePageTable)
Definition: nls.c:155
NTSTATUS NTAPI NtCreateDirectoryObject(OUT PHANDLE DirectoryHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
Definition: obdir.c:773
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define DIRECTORY_ALL_ACCESS
Definition: nt_native.h:1259
PBYTE SectionMapping
Definition: kernel32.h:67
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
_In_ PFCB _In_ PCD_NAME DirName
Definition: cdprocs.h:741
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
HANDLE SectionHandle
Definition: kernel32.h:66
unsigned short * PUSHORT
Definition: retypes.h:2
CPTABLEINFO CodePageTable
Definition: kernel32.h:68

Referenced by DllMain().

◆ NlsUninit()

VOID FASTCALL NlsUninit ( VOID  )

Definition at line 126 of file nls.c.

127 {
128  PCODEPAGE_ENTRY Current;
129 
130  /* Delete the code page list. */
131  while (!IsListEmpty(&CodePageListHead))
132  {
134  if (Current->SectionHandle != NULL)
135  {
137  NtClose(Current->SectionHandle);
138  }
140  }
142 }
static LIST_ENTRY CodePageListHead
Definition: nls.c:45
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
smooth NULL
Definition: ftsmooth.c:416
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
static RTL_CRITICAL_SECTION CodePageListLock
Definition: nls.c:48
Definition: kernel32.h:62
PBYTE SectionMapping
Definition: kernel32.h:67
HANDLE SectionHandle
Definition: kernel32.h:66
NTSYSAPI NTSTATUS NTAPI RtlDeleteCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
#define UnmapViewOfFile
Definition: compat.h:411
base of all file and directory entries
Definition: entries.h:82

Referenced by DllMain().

Variable Documentation

◆ BaseDefaultPathBuffer

WCHAR BaseDefaultPathBuffer[6140]

Definition at line 22 of file dllmain.c.

Referenced by DllMain().

◆ BaseDllDirectoryLock

◆ BaseNamedObjectDirectory

HANDLE BaseNamedObjectDirectory

Definition at line 24 of file dllmain.c.

Referenced by BaseGetNamedObjectDirectory().

◆ BaseRunningInServerProcess

BOOLEAN BaseRunningInServerProcess = FALSE

◆ BaseStaticServerData

◆ DllInitialized

BOOL DllInitialized = FALSE
static

Definition at line 29 of file dllmain.c.

Referenced by DllMain().

◆ hCurrentModule

HMODULE hCurrentModule = NULL

Definition at line 25 of file dllmain.c.

Referenced by DllMain(), and GetLocalisedText().

◆ kernel32_handle

◆ Peb

◆ SessionId