From d19dfaa93ce862df784f8ecbffeac671b1365320 Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Fri, 22 Jul 2011 08:59:27 +0000 Subject: [PATCH] [KERNEL32]: Add a macro function that automatically takes care of A->W conversion for Win32->NT Object Create APIs, and does AllTheRightStuff. [KERNEL32]: Fix bug #1: CreateFileMappingA was not returning the right error if the file mapping name was too long. By making it use the new ConvertWin32AnsiObjectApiToUnicodeApi macro, it now does. svn path=/trunk/; revision=52776 --- .../dll/win32/kernel32/client/file/filemap.c | 40 +++----------- reactos/dll/win32/kernel32/include/base_x.h | 52 +++++++++++++++++++ reactos/dll/win32/kernel32/include/kernel32.h | 4 ++ reactos/dll/win32/kernel32/k32.h | 5 +- 4 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 reactos/dll/win32/kernel32/include/base_x.h diff --git a/reactos/dll/win32/kernel32/client/file/filemap.c b/reactos/dll/win32/kernel32/client/file/filemap.c index 705a8c360de..d0648ce7614 100644 --- a/reactos/dll/win32/kernel32/client/file/filemap.c +++ b/reactos/dll/win32/kernel32/client/file/filemap.c @@ -27,38 +27,14 @@ CreateFileMappingA(IN HANDLE hFile, IN DWORD dwMaximumSizeLow, IN LPCSTR lpName) { - NTSTATUS Status; - ANSI_STRING AnsiName; - PUNICODE_STRING UnicodeCache; - LPCWSTR UnicodeName = NULL; - - /* Check for a name */ - if (lpName) - { - /* Use TEB Cache */ - UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; - - /* Convert to unicode */ - RtlInitAnsiString(&AnsiName, lpName); - Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); - if (!NT_SUCCESS(Status)) - { - /* Conversion failed */ - SetLastErrorByStatus(Status); - return NULL; - } - - /* Otherwise, save the buffer */ - UnicodeName = (LPCWSTR)UnicodeCache->Buffer; - } - - /* Call the Unicode version */ - return CreateFileMappingW(hFile, - lpFileMappingAttributes, - flProtect, - dwMaximumSizeHigh, - dwMaximumSizeLow, - UnicodeName); + /* Call the W(ide) function */ + ConvertWin32AnsiObjectApiToUnicodeApi(FileMapping, + lpName, + hFile, + lpFileMappingAttributes, + flProtect, + dwMaximumSizeHigh, + dwMaximumSizeLow); } /* diff --git a/reactos/dll/win32/kernel32/include/base_x.h b/reactos/dll/win32/kernel32/include/base_x.h new file mode 100644 index 00000000000..9b9d9ea17b5 --- /dev/null +++ b/reactos/dll/win32/kernel32/include/base_x.h @@ -0,0 +1,52 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS System Libraries + * FILE: dll/win32/kernel32/include/base_x.h + * PURPOSE: Base API Client Macros + * PROGRAMMER: Alex Ionescu (alex@relsoft.net) + */ + +#pragma once + +/* INCLUDES *******************************************************************/ + +// +// This macro (split it up in 3 pieces to allow for intermediary code in between) +// converts a NULL-terminated ASCII string, usually associated with an object +// name, into its NT-native UNICODE_STRING structure, by using the TEB's Static +// Unicode String. +// +// It should only be used when the name is supposed to be less than MAX_PATH +// (260 characters). +// +// It returns the correct ERROR_FILENAME_EXCED_RANGE Win32 error when the path +// is too long. +// +#define ConvertAnsiToUnicodePrologue \ +{ \ + NTSTATUS Status; \ + PUNICODE_STRING UnicodeCache; \ + ANSI_STRING AnsiName; +#define ConvertAnsiToUnicodeBody(name) \ + UnicodeCache = &NtCurrentTeb()->StaticUnicodeString; \ + RtlInitAnsiString(&AnsiName, name); \ + Status = RtlAnsiStringToUnicodeString(UnicodeCache, &AnsiName, FALSE); +#define ConvertAnsiToUnicodeEpilogue \ + if (Status == STATUS_BUFFER_OVERFLOW) \ + SetLastError(ERROR_FILENAME_EXCED_RANGE); \ + else \ + SetLastErrorByStatus(Status); \ + return FALSE; \ +} + +// +// This macro uses the ConvertAnsiToUnicode macros above to convert a CreateXxxA +// Win32 API into its equivalent CreateXxxW API. +// +#define ConvertWin32AnsiObjectApiToUnicodeApi(obj, name, args...) \ + ConvertAnsiToUnicodePrologue \ + if (!name) return Create##obj##W(args, NULL); \ + ConvertAnsiToUnicodeBody(name) \ + if (NT_SUCCESS(Status)) return Create##obj##W(args, UnicodeCache->Buffer); \ + ConvertAnsiToUnicodeEpilogue + diff --git a/reactos/dll/win32/kernel32/include/kernel32.h b/reactos/dll/win32/kernel32/include/kernel32.h index 5c13847d128..2a50f01ccc3 100644 --- a/reactos/dll/win32/kernel32/include/kernel32.h +++ b/reactos/dll/win32/kernel32/include/kernel32.h @@ -21,6 +21,10 @@ #define FIXME(fmt, ...) WARN__(gDebugChannel, fmt,## __VA_ARGS__) #define ERR(fmt, ...) ERR__(gDebugChannel, fmt, ##__VA_ARGS__) +#define STUB \ + SetLastError(ERROR_CALL_NOT_IMPLEMENTED); \ + DPRINT1("%s() is UNIMPLEMENTED!\n", __FUNCTION__) + #define debugstr_a #define debugstr_w #define wine_dbgstr_w diff --git a/reactos/dll/win32/kernel32/k32.h b/reactos/dll/win32/kernel32/k32.h index c64e33f8f34..553347ce27f 100644 --- a/reactos/dll/win32/kernel32/k32.h +++ b/reactos/dll/win32/kernel32/k32.h @@ -42,8 +42,7 @@ /* PSEH for SEH Support */ #include -#define STUB \ - SetLastError(ERROR_CALL_NOT_IMPLEMENTED); \ - DPRINT1("%s() is UNIMPLEMENTED!\n", __FUNCTION__) +/* Base Macros */ +#include "include/base_x.h" #endif