reactos/reactos/lib/kernel32/misc/dllmain.c

189 lines
4.3 KiB
C
Raw Normal View History

/* $Id$
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: lib/kernel32/misc/dllmain.c
* PURPOSE: Initialization
* PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
* UPDATE HISTORY:
* Created 01/11/98
*/
/* INCLUDES ******************************************************************/
2003-01-15 Casper S. Hornstrup <chorns@users.sourceforge.net> * lib/kernel32/k32.h: New file. * lib/kernel32/makefile (TARGET_CFLAGS): Add -I./. (TARGET_PCH): Set to k32.h. * lib/kernel32/except/except.c: Use <k32.h>. * lib/kernel32/file/backup.c: Ditto. * lib/kernel32/file/cnotify.c: Ditto. * lib/kernel32/file/copy.c: Ditto. * lib/kernel32/file/create.c: Ditto. * lib/kernel32/file/curdir.c: Ditto. * lib/kernel32/file/delete.c: Ditto. * lib/kernel32/file/deviceio.c: Ditto. * lib/kernel32/file/dir.c: Ditto. * lib/kernel32/file/dosdev.c: Ditto. * lib/kernel32/file/file.c: Ditto. * lib/kernel32/file/find.c: Ditto. * lib/kernel32/file/iocompl.c: Ditto. * lib/kernel32/file/lfile.c: Ditto. * lib/kernel32/file/lock.c: Ditto. * lib/kernel32/file/mailslot.c: Ditto. * lib/kernel32/file/move.c: Ditto. * lib/kernel32/file/npipe.c: Ditto. * lib/kernel32/file/pipe.c: Ditto. * lib/kernel32/file/rw.c: Ditto. * lib/kernel32/file/tape.c: Ditto. * lib/kernel32/file/volume.c: Ditto. * lib/kernel32/mem/global.c: Ditto. * lib/kernel32/mem/heap.c: Ditto. * lib/kernel32/mem/isbad.c: Ditto. * lib/kernel32/mem/local.c: Ditto. * lib/kernel32/mem/procmem.c: Ditto. * lib/kernel32/mem/section.c: Ditto. * lib/kernel32/mem/virtual.c: Ditto. * lib/kernel32/misc/atom.c: Ditto. * lib/kernel32/misc/comm.c: Ditto. * lib/kernel32/misc/console.c: Ditto. * lib/kernel32/misc/debug.c: Ditto. * lib/kernel32/misc/dllmain.c: Ditto. * lib/kernel32/misc/env.c: Ditto. * lib/kernel32/misc/error.c: Ditto. * lib/kernel32/misc/handle.c: Ditto. * lib/kernel32/misc/ldr.c: Ditto. * lib/kernel32/misc/profile.c: Ditto. * lib/kernel32/misc/res.c: Ditto. * lib/kernel32/misc/stubs.c: Ditto. * lib/kernel32/misc/sysinfo.c: Ditto. * lib/kernel32/misc/time.c: Ditto. * lib/kernel32/process/cmdline.c: Ditto. * lib/kernel32/process/create.c: Ditto. * lib/kernel32/process/proc.c: Ditto. * lib/kernel32/process/session.c: Ditto. * lib/kernel32/string/lstring.c: Ditto. * lib/kernel32/synch/critical.c: Ditto. * lib/kernel32/synch/event.c: Ditto. * lib/kernel32/synch/intrlck.c: Ditto. * lib/kernel32/synch/mutex.c: Ditto. * lib/kernel32/synch/sem.c: Ditto. * lib/kernel32/synch/timer.c: Ditto. * lib/kernel32/synch/wait.c: Ditto. * lib/kernel32/thread/fiber.c: Ditto. * lib/kernel32/thread/thread.c: Ditto. * lib/kernel32/thread/tls.c: Ditto. svn path=/trunk/; revision=4009
2003-01-15 21:24:36 +00:00
#include <k32.h>
#define NDEBUG
#include "../include/debug.h"
/* GLOBALS *******************************************************************/
extern UNICODE_STRING SystemDirectory;
extern UNICODE_STRING WindowsDirectory;
HANDLE hProcessHeap = NULL;
HMODULE hCurrentModule = NULL;
HANDLE hBaseDir = NULL;
static BOOL DllInitialized = FALSE;
BOOL STDCALL
DllMain(HANDLE hInst,
DWORD dwReason,
LPVOID lpReserved);
/* Critical section for various kernel32 data structures */
RTL_CRITICAL_SECTION DllLock;
RTL_CRITICAL_SECTION ConsoleLock;
extern BOOL WINAPI DefaultConsoleCtrlHandler(DWORD Event);
extern BOOL FASTCALL NlsInit();
extern VOID FASTCALL NlsUninit();
/* FUNCTIONS *****************************************************************/
static NTSTATUS
OpenBaseDirectory(PHANDLE DirHandle)
{
OBJECT_ATTRIBUTES ObjectAttributes;
UNICODE_STRING Name = RTL_CONSTANT_STRING(L"\\BaseNamedObjects");
NTSTATUS Status;
InitializeObjectAttributes(&ObjectAttributes,
&Name,
OBJ_PERMANENT,
NULL,
NULL);
Status = NtOpenDirectoryObject(DirHandle,
DIRECTORY_ALL_ACCESS,
&ObjectAttributes);
if (!NT_SUCCESS(Status))
{
Status = NtCreateDirectoryObject(DirHandle,
DIRECTORY_ALL_ACCESS,
&ObjectAttributes);
if (!NT_SUCCESS(Status))
{
DbgPrint("NtCreateDirectoryObject() failed\n");
}
return Status;
}
return STATUS_SUCCESS;
}
BOOL STDCALL
DllMain(HANDLE hDll,
DWORD dwReason,
LPVOID lpReserved)
{
NTSTATUS Status;
(void)lpReserved;
DPRINT("DllMain(hInst %lx, dwReason %lu)\n",
hDll, dwReason);
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
DPRINT("DLL_PROCESS_ATTACH\n");
LdrDisableThreadCalloutsForDll ((PVOID)hDll);
/*
* Connect to the csrss server
*/
Status = CsrClientConnectToServer(NULL,
0,
NULL,
NULL,
0,
NULL);
if (!NT_SUCCESS(Status))
{
DbgPrint("Failed to connect to csrss.exe (Status %lx)\n",
Status);
ZwTerminateProcess(NtCurrentProcess(), Status);
return FALSE;
}
hProcessHeap = RtlGetProcessHeap();
hCurrentModule = hDll;
/*
* Initialize WindowsDirectory and SystemDirectory
*/
DPRINT("NtSystemRoot: %S\n",
SharedUserData->NtSystemRoot);
RtlCreateUnicodeString (&WindowsDirectory,
SharedUserData->NtSystemRoot);
SystemDirectory.MaximumLength = WindowsDirectory.MaximumLength + 18;
SystemDirectory.Length = WindowsDirectory.Length + 18;
SystemDirectory.Buffer = RtlAllocateHeap (hProcessHeap,
0,
SystemDirectory.MaximumLength);
wcscpy (SystemDirectory.Buffer, WindowsDirectory.Buffer);
wcscat (SystemDirectory.Buffer, L"\\System32");
/* Open object base directory */
Status = OpenBaseDirectory(&hBaseDir);
if (!NT_SUCCESS(Status))
{
DbgPrint("Failed to open object base directory (Status %lx)\n",
Status);
return FALSE;
}
/* Initialize the DLL critical section */
RtlInitializeCriticalSection(&DllLock);
/* Initialize the National Language Support routines */
if (! NlsInit())
{
return FALSE;
}
/* Initialize console ctrl handler */
RtlInitializeCriticalSection(&ConsoleLock);
SetConsoleCtrlHandler(DefaultConsoleCtrlHandler, TRUE);
/* Insert more dll attach stuff here! */
DllInitialized = TRUE;
break;
case DLL_PROCESS_DETACH:
DPRINT("DLL_PROCESS_DETACH\n");
if (DllInitialized == TRUE)
{
/* Insert more dll detach stuff here! */
NlsUninit();
/* Delete DLL critical section */
RtlDeleteCriticalSection (&ConsoleLock);
RtlDeleteCriticalSection (&DllLock);
/* Close object base directory */
NtClose(hBaseDir);
RtlFreeUnicodeString (&SystemDirectory);
RtlFreeUnicodeString (&WindowsDirectory);
}
break;
default:
break;
}
return TRUE;
}
/* EOF */