//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
//
// P/Invoke constants for Win32 functions.
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop.Win32
{
using System;
using System.ComponentModel;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
#if !MANAGEDESENT_ON_WSA // None of this can be called in windows store apps.
///
/// Allocation type options for .
///
[Flags]
internal enum AllocationType : uint
{
///
/// Commit the memory.
///
MEM_COMMIT = 0x1000,
///
/// Reserve the memory.
///
MEM_RESERVE = 0x2000,
}
///
/// Memory protection options for .
///
internal enum MemoryProtection : uint
{
///
/// Read/write access to the pages.
///
PAGE_READWRITE = 0x04,
}
///
/// Options for .
///
internal enum FreeType : uint
{
///
/// Release the memory. The pages will be in the free state.
///
MEM_RELEASE = 0x8000,
}
#endif
///
/// P/Invoke methods for Win32 functions.
///
internal static class NativeMethods
{
#if MANAGEDESENT_ON_CORECLR || MANAGEDESENT_ON_WSA
///
/// The name of the DLL that holds the Core Memory API set.
///
private const string WinCoreMemoryDll = "api-ms-win-core-memory-l1-1-1.dll";
///
/// The name of the DLL that holds the Obsolete Heap API set.
/// (Might be api-ms-win-core-heap-obsolete-l1-1-0.dll.)
///
private const string HeapObsolete = "kernelbase";
///
/// The name of the DLL that holds the Core process/threads API set.
///
private const string WinCoreProcessThreads = "api-ms-win-core-processthreads-l1-1-1.dll";
#else
///
/// The name of the DLL that holds the Core Memory API set.
///
private const string WinCoreMemoryDll = "kernel32.dll";
///
/// The name of the DLL that holds the Obsolete Heap API set.
///
private const string HeapObsolete = "kernel32.dll";
///
/// The name of the DLL that holds the Core process/threads API set.
///
private const string WinCoreProcessThreads = "kernel32.dll";
#endif // MANAGEDESENT_ON_CORECLR || MANAGEDESENT_ON_WSA
#if !MANAGEDESENT_ON_WSA // None of this can be called in windows store apps.
///
/// Throw an exception if the given pointer is null (IntPtr.Zero).
///
/// The pointer to check.
/// The message for the exception.
public static void ThrowExceptionOnNull(IntPtr ptr, string message)
{
if (IntPtr.Zero == ptr)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), message);
}
}
///
/// Throw an exception if the success code is not true.
///
/// The success code.
/// The message for the exception.
public static void ThrowExceptionOnFailure(bool success, string message)
{
if (!success)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), message);
}
}
[DllImport(WinCoreMemoryDll, SetLastError = true)]
public static extern IntPtr VirtualAlloc(IntPtr plAddress, UIntPtr dwSize, uint flAllocationType, uint flProtect);
[DllImport(WinCoreMemoryDll, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool VirtualFree(IntPtr lpAddress, UIntPtr dwSize, uint dwFreeType);
[DllImport(HeapObsolete)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public static extern IntPtr LocalAlloc(int uFlags, UIntPtr sizetdwBytes);
[DllImport(HeapObsolete)]
public static extern IntPtr LocalFree(IntPtr hglobal);
#endif // !MANAGEDESENT_ON_WSA
// Win32 APIs that are white-listed for Windows Store Apps can be safely referenced here.
[DllImport(WinCoreProcessThreads)]
public static extern int GetCurrentProcessId();
}
}