2011-07-22 08:59:27 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
//
|
2011-07-22 10:10:15 +00:00
|
|
|
// Note that Basep8BitStringToStaticUnicodeString looks deceptively similar.
|
|
|
|
// However, that function was designed for File APIs, which can be switched into
|
|
|
|
// a special "OEM" mode, that uses different NLS files/encoding, and thus calls
|
|
|
|
// RtlOemStringToAnsiString (see SetFileApisToOEM). Thererefore, this macro and
|
|
|
|
// that function are not interchangeable. As a separate note, that function uses
|
|
|
|
// the *Ex version of the Rtl conversion APIs, which does stricter checking that
|
|
|
|
// is not done when this macro is used.
|
|
|
|
//
|
2011-07-22 08:59:27 +00:00
|
|
|
#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 \
|
2011-07-23 18:54:29 +00:00
|
|
|
BaseSetLastNTError(Status); \
|
2011-07-22 08:59:27 +00:00
|
|
|
return FALSE; \
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// This macro uses the ConvertAnsiToUnicode macros above to convert a CreateXxxA
|
|
|
|
// Win32 API into its equivalent CreateXxxW API.
|
|
|
|
//
|
2011-07-23 02:17:26 +00:00
|
|
|
#define ConvertWin32AnsiObjectApiToUnicodeApi(obj, name, ...) \
|
2011-07-22 08:59:27 +00:00
|
|
|
ConvertAnsiToUnicodePrologue \
|
2011-07-23 02:17:26 +00:00
|
|
|
if (!name) return Create##obj##W(__VA_ARGS__, NULL); \
|
2011-07-22 08:59:27 +00:00
|
|
|
ConvertAnsiToUnicodeBody(name) \
|
2011-07-23 02:17:26 +00:00
|
|
|
if (NT_SUCCESS(Status)) return Create##obj##W(__VA_ARGS__, UnicodeCache->Buffer); \
|
2011-07-22 08:59:27 +00:00
|
|
|
ConvertAnsiToUnicodeEpilogue
|
2012-02-21 19:19:44 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// This macro uses the ConvertAnsiToUnicode macros above to convert a CreateXxxA
|
|
|
|
// Win32 API into its equivalent CreateXxxW API.
|
|
|
|
//
|
|
|
|
#define ConvertWin32AnsiObjectApiToUnicodeApi2(obj, name, ...) \
|
|
|
|
ConvertAnsiToUnicodePrologue \
|
|
|
|
if (!name) return Create##obj##W(NULL, __VA_ARGS__); \
|
|
|
|
ConvertAnsiToUnicodeBody(name) \
|
|
|
|
if (NT_SUCCESS(Status)) return Create##obj##W(UnicodeCache->Buffer, __VA_ARGS__); \
|
|
|
|
ConvertAnsiToUnicodeEpilogue
|
2011-07-22 08:59:27 +00:00
|
|
|
|
2011-07-22 09:09:05 +00:00
|
|
|
//
|
|
|
|
// This macro uses the ConvertAnsiToUnicode macros above to convert a FindFirst*A
|
|
|
|
// Win32 API into its equivalent FindFirst*W API.
|
|
|
|
//
|
2011-07-23 02:17:26 +00:00
|
|
|
#define ConvertWin32AnsiChangeApiToUnicodeApi(obj, name, ...) \
|
2011-07-22 09:09:05 +00:00
|
|
|
ConvertAnsiToUnicodePrologue \
|
|
|
|
ConvertAnsiToUnicodeBody(name) \
|
2011-07-23 02:17:26 +00:00
|
|
|
if (NT_SUCCESS(Status)) return obj##W(UnicodeCache->Buffer, ##__VA_ARGS__); \
|
2011-07-22 09:09:05 +00:00
|
|
|
ConvertAnsiToUnicodeEpilogue
|
|
|
|
|
2011-07-23 09:28:15 +00:00
|
|
|
//
|
|
|
|
// This macro uses the ConvertAnsiToUnicode macros above to convert a OpenXxxA
|
|
|
|
// Win32 API into its equivalent OpenXxxW API.
|
|
|
|
//
|
|
|
|
#define ConvertOpenWin32AnsiObjectApiToUnicodeApi(obj, acc, inh, name) \
|
|
|
|
ConvertAnsiToUnicodePrologue \
|
2011-07-23 14:07:17 +00:00
|
|
|
if (!name) \
|
|
|
|
{ \
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER); \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
2011-07-23 09:28:15 +00:00
|
|
|
ConvertAnsiToUnicodeBody(name) \
|
|
|
|
if (NT_SUCCESS(Status)) return Open##obj##W(acc, inh, UnicodeCache->Buffer);\
|
|
|
|
ConvertAnsiToUnicodeEpilogue
|
|
|
|
|
2011-07-23 00:28:33 +00:00
|
|
|
//
|
|
|
|
// This macro (split it up in 3 pieces to allow for intermediary code in between)
|
|
|
|
// wraps the usual code path required to create an NT object based on a Unicode
|
|
|
|
// (Wide) Win32 object creation API.
|
|
|
|
//
|
2011-11-03 00:30:44 +00:00
|
|
|
// It makes use of BaseFormatObjectAttributes and allows for a custom access
|
2011-07-23 00:28:33 +00:00
|
|
|
// mode to be used, and also sets the correct error codes in case of a collision
|
|
|
|
//
|
2011-07-23 09:40:34 +00:00
|
|
|
#define CreateNtObjectFromWin32ApiPrologue \
|
2011-07-23 00:28:33 +00:00
|
|
|
{ \
|
|
|
|
NTSTATUS Status; \
|
|
|
|
HANDLE Handle; \
|
|
|
|
UNICODE_STRING ObjectName; \
|
2011-07-23 09:58:33 +00:00
|
|
|
OBJECT_ATTRIBUTES LocalAttributes; \
|
|
|
|
POBJECT_ATTRIBUTES ObjectAttributes = &LocalAttributes;
|
2011-07-23 09:40:34 +00:00
|
|
|
#define CreateNtObjectFromWin32ApiBody(ntobj, sec, name, access, ...) \
|
2011-07-23 00:28:33 +00:00
|
|
|
if (name) RtlInitUnicodeString(&ObjectName, name); \
|
2011-11-03 07:00:48 +00:00
|
|
|
ObjectAttributes = BaseFormatObjectAttributes(&LocalAttributes, \
|
2011-07-23 00:28:33 +00:00
|
|
|
sec, \
|
2011-07-23 09:40:34 +00:00
|
|
|
name ? &ObjectName : NULL); \
|
2011-07-23 02:17:26 +00:00
|
|
|
Status = NtCreate##ntobj(&Handle, access, ObjectAttributes, ##__VA_ARGS__);
|
2011-07-23 00:28:33 +00:00
|
|
|
#define CreateNtObjectFromWin32ApiEpilogue \
|
|
|
|
if (NT_SUCCESS(Status)) \
|
|
|
|
{ \
|
|
|
|
if (Status == STATUS_OBJECT_NAME_EXISTS) \
|
|
|
|
SetLastError(ERROR_ALREADY_EXISTS); \
|
|
|
|
else \
|
|
|
|
SetLastError(ERROR_SUCCESS); \
|
|
|
|
return Handle; \
|
|
|
|
} \
|
2011-11-03 07:00:48 +00:00
|
|
|
BaseSetLastNTError(Status); \
|
2011-07-23 00:28:33 +00:00
|
|
|
return NULL; \
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// This macro uses the CreateNtObjectFromWin32Api macros from above to create an
|
|
|
|
// NT object based on the Win32 API settings.
|
|
|
|
//
|
|
|
|
// Note that it is hardcoded to always use XXX_ALL_ACCESS permissions, which is
|
|
|
|
// the behavior up until Vista. When/if the target moves to Vista, the macro can
|
|
|
|
// be improved to support caller-specified access masks, as the underlying macro
|
|
|
|
// above does support this.
|
|
|
|
//
|
2011-07-23 02:17:26 +00:00
|
|
|
#define CreateNtObjectFromWin32Api(obj, ntobj, capsobj, sec, name, ...) \
|
2011-07-23 09:40:34 +00:00
|
|
|
CreateNtObjectFromWin32ApiPrologue \
|
|
|
|
CreateNtObjectFromWin32ApiBody(ntobj, sec, name, capsobj##_ALL_ACCESS, ##__VA_ARGS__); \
|
2011-07-23 00:28:33 +00:00
|
|
|
CreateNtObjectFromWin32ApiEpilogue
|
|
|
|
|
2011-07-23 09:40:34 +00:00
|
|
|
//
|
|
|
|
// This macro opens an NT object based on the Win32 API settings.
|
|
|
|
//
|
|
|
|
#define OpenNtObjectFromWin32Api(ntobj, acc, inh, name) \
|
|
|
|
CreateNtObjectFromWin32ApiPrologue \
|
2011-07-23 14:07:17 +00:00
|
|
|
if (!name) \
|
|
|
|
{ \
|
2011-11-03 07:00:48 +00:00
|
|
|
BaseSetLastNTError(STATUS_INVALID_PARAMETER); \
|
2011-07-23 14:07:17 +00:00
|
|
|
return NULL; \
|
|
|
|
} \
|
2011-07-23 09:40:34 +00:00
|
|
|
RtlInitUnicodeString(&ObjectName, name); \
|
2011-07-23 09:58:33 +00:00
|
|
|
InitializeObjectAttributes(ObjectAttributes, \
|
2011-07-23 09:40:34 +00:00
|
|
|
&ObjectName, \
|
|
|
|
inh ? OBJ_INHERIT : 0, \
|
2011-11-03 07:00:48 +00:00
|
|
|
BaseGetNamedObjectDirectory(), \
|
2011-07-23 09:40:34 +00:00
|
|
|
NULL); \
|
2011-07-23 09:58:33 +00:00
|
|
|
Status = NtOpen##ntobj(&Handle, acc, ObjectAttributes); \
|
2011-07-23 14:07:17 +00:00
|
|
|
if (!NT_SUCCESS(Status)) \
|
|
|
|
{ \
|
2011-11-03 07:00:48 +00:00
|
|
|
BaseSetLastNTError(Status); \
|
2011-07-23 14:07:17 +00:00
|
|
|
return NULL; \
|
|
|
|
} \
|
2011-07-23 09:40:34 +00:00
|
|
|
return Handle; \
|
|
|
|
}
|
|
|
|
|