From 2124442b3d84d99a95f151333b2cc81623c87784 Mon Sep 17 00:00:00 2001 From: Art Yerkes Date: Sun, 15 Feb 2004 00:04:08 +0000 Subject: [PATCH] Added new files for common functions. svn path=/trunk/; revision=8179 --- reactos/lib/rosrtl/makefile | 11 +++- reactos/lib/rosrtl/registry/registry.c | 76 ++++++++++++++++++++++++++ reactos/lib/rosrtl/string/append.c | 38 +++++++++++++ 3 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 reactos/lib/rosrtl/registry/registry.c create mode 100644 reactos/lib/rosrtl/string/append.c diff --git a/reactos/lib/rosrtl/makefile b/reactos/lib/rosrtl/makefile index 64208a4f7c5..730eaeef134 100644 --- a/reactos/lib/rosrtl/makefile +++ b/reactos/lib/rosrtl/makefile @@ -1,4 +1,4 @@ -# $Id: makefile,v 1.11 2004/01/11 20:46:06 navaraf Exp $ +# $Id: makefile,v 1.12 2004/02/15 00:04:07 arty Exp $ PATH_TO_TOP = ../.. @@ -15,6 +15,12 @@ THREAD_OBJECTS = \ thread/exit.o \ thread/stack.o +STRING_OBJECTS = \ + string/append.o + +REGISTRY_OBJECTS = \ + registry/registry.o + MISC_OBJECTS = \ misc/devmode.o \ misc/logfont.o \ @@ -30,7 +36,8 @@ TARGET_NAME = rosrtl TARGET_CFLAGS = -D__USE_W32API -Wall -Werror -TARGET_OBJECTS = $(THREAD_OBJECTS) $(MISC_OBJECTS) +TARGET_OBJECTS = $(THREAD_OBJECTS) $(MISC_OBJECTS) $(STRING_OBJECTS) \ + $(REGISTRY_OBJECTS) DEP_OBJECTS = $(TARGET_OBJECTS) diff --git a/reactos/lib/rosrtl/registry/registry.c b/reactos/lib/rosrtl/registry/registry.c new file mode 100644 index 00000000000..f574dad9733 --- /dev/null +++ b/reactos/lib/rosrtl/registry/registry.c @@ -0,0 +1,76 @@ +#define NTOS_MODE_USER +#include +#include + +/* + * Utility function to read a value from the registry more easily. + * + * IN PUNICODE_STRING KeyName -> Name of key to open + * IN PUNICODE_STRING ValueName -> Name of value to open + * OUT PUNICODE_STRING ReturnedValue -> String contained in registry + * + * Returns NTSTATUS + */ + +NTSTATUS NTAPI RosReadRegistryValue( PUNICODE_STRING KeyName, + PUNICODE_STRING ValueName, + PUNICODE_STRING ReturnedValue ) { + NTSTATUS Status; + HANDLE KeyHandle; + OBJECT_ATTRIBUTES KeyAttributes; + PKEY_VALUE_PARTIAL_INFORMATION KeyValuePartialInfo; + ULONG Length = 0; + ULONG ResLength = 0; + UNICODE_STRING Temp; + + InitializeObjectAttributes(&KeyAttributes, KeyName, OBJ_CASE_INSENSITIVE, + NULL, NULL); + Status = ZwOpenKey(&KeyHandle, KEY_ALL_ACCESS, &KeyAttributes); + if( !NT_SUCCESS(Status) ) { + return Status; + } + + Status = ZwQueryValueKey(KeyHandle, ValueName, KeyValuePartialInformation, + 0, + 0, + &ResLength); + + if( Status != STATUS_BUFFER_TOO_SMALL ) { + NtClose(KeyHandle); + return Status; + } + + ResLength += sizeof( *KeyValuePartialInfo ); + KeyValuePartialInfo = + RtlAllocateHeap(GetProcessHeap(), 0, ResLength); + Length = ResLength; + + if( !KeyValuePartialInfo ) { + NtClose(KeyHandle); + return STATUS_NO_MEMORY; + } + + Status = ZwQueryValueKey(KeyHandle, ValueName, KeyValuePartialInformation, + (PVOID)KeyValuePartialInfo, + Length, + &ResLength); + + if( !NT_SUCCESS(Status) ) { + NtClose(KeyHandle); + RtlFreeHeap(GetProcessHeap(),0,KeyValuePartialInfo); + return Status; + } + + Temp.Length = Temp.MaximumLength = KeyValuePartialInfo->DataLength; + Temp.Buffer = (PWCHAR)KeyValuePartialInfo->Data; + + /* At this point, KeyValuePartialInfo->Data contains the key data */ + RtlInitUnicodeString(ReturnedValue,L""); + RosAppendUnicodeString(ReturnedValue,&Temp,FALSE); + + RtlFreeHeap(GetProcessHeap(),0,KeyValuePartialInfo); + NtClose(KeyHandle); + + return Status; +} + diff --git a/reactos/lib/rosrtl/string/append.c b/reactos/lib/rosrtl/string/append.c new file mode 100644 index 00000000000..eb615bde745 --- /dev/null +++ b/reactos/lib/rosrtl/string/append.c @@ -0,0 +1,38 @@ +#define NTOS_MODE_USER +#include + +/* + * Utility to copy and append two unicode strings. + * + * IN OUT PUNICODE_STRING ResultFirst -> First string and result + * IN PUNICODE_STRING Second -> Second string to append + * IN BOOL Deallocate -> TRUE: Deallocate First string before + * overwriting. + * + * Returns NTSTATUS. + */ + +NTSTATUS NTAPI RosAppendUnicodeString(PUNICODE_STRING ResultFirst, + PUNICODE_STRING Second, + BOOL Deallocate) { + NTSTATUS Status; + PWSTR new_string = + RtlAllocateHeap(GetProcessHeap(),0, + (ResultFirst->Length + Second->Length + sizeof(WCHAR))); + if( !new_string ) { + return STATUS_NO_MEMORY; + } + memcpy( new_string, ResultFirst->Buffer, + ResultFirst->Length ); + memcpy( new_string + ResultFirst->Length / sizeof(WCHAR), + Second->Buffer, + Second->Length ); + if( Deallocate ) RtlFreeUnicodeString(ResultFirst); + ResultFirst->Length += Second->Length; + ResultFirst->MaximumLength = ResultFirst->Length; + new_string[ResultFirst->Length / sizeof(WCHAR)] = 0; + Status = RtlCreateUnicodeString(ResultFirst,new_string) ? + STATUS_SUCCESS : STATUS_NO_MEMORY; + RtlFreeHeap(GetProcessHeap(),0,new_string); + return Status; +}