using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
namespace Vanara.PInvoke
{
public static partial class Kernel32
{
///
/// An application-defined callback function used with the EnumResourceLanguages and EnumResourceLanguagesEx functions.
/// It receives the type, name, and language of a resource item. The ENUMRESLANGPROC type defines a pointer to this callback
/// function. EnumResLangProc is a placeholder for the application-defined function name.
///
///
/// Type: HMODULE
///
/// A handle to the module whose executable file contains the resources for which the languages are being enumerated. If this
/// parameter is NULL, the function enumerates the resource languages in the module used to create the current process.
///
///
///
/// Type: LPCTSTR
///
/// The type of resource for which the language is being enumerated. Alternately, rather than a pointer, this parameter can be ,
/// where ID is an integer value representing a predefined resource type. For standard resource types, see Resource Types. For more
/// information, see the Remarks section below.
///
///
///
/// Type: LPCTSTR
///
/// The name of the resource for which the language is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is the integer identifier of the resource. For more information, see the Remarks section below.
///
///
///
/// Type: WORD
///
/// The language identifier for the resource for which the language is being enumerated. The EnumResourceLanguages or
/// EnumResourceLanguagesEx function provides this value. For a list of the primary language identifiers and sublanguage
/// identifiers that constitute a language identifier, see MAKELANGID.
///
///
///
/// Type: LONG_PTR
///
/// The application-defined parameter passed to the EnumResourceLanguages or EnumResourceLanguagesEx function. This
/// parameter can be used in error checking.
///
///
///
/// Type: BOOL
/// Returns TRUE to continue enumeration or FALSE to stop enumeration.
///
// BOOL CALLBACK EnumResLangProc( _In_opt_ HMODULE hModule, _In_ LPCTSTR lpszType, _In_ LPCTSTR lpszName, _In_ WORD wIDLanguage, _In_
// LONG_PTR lParam); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648033(v=vs.85).aspx
[PInvokeData("Winbase.h", MSDNShortId = "ms648033")]
[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public delegate bool EnumResLangProc([In] HINSTANCE hModule, [In] ResourceId lpszType, [In] ResourceId lpszName, ushort wIDLanguage, [In] IntPtr lParam);
///
/// An application-defined callback function used with the EnumResourceNames and EnumResourceNamesEx functions. It
/// receives the type and name of a resource. The ENUMRESNAMEPROC type defines a pointer to this callback function.
/// EnumResNameProc is a placeholder for the application-defined function name.
///
///
/// Type: HMODULE
///
/// A handle to the module whose executable file contains the resources that are being enumerated. If this parameter is NULL,
/// the function enumerates the resource names in the module used to create the current process.
///
///
///
/// Type: LPCTSTR
///
/// The type of resource for which the name is being enumerated. Alternately, rather than a pointer, this parameter can be , where ID
/// is an integer value representing a predefined resource type. For standard resource types, see Resource Types. For more
/// information, see the Remarks section below.
///
///
///
/// Type: LPTSTR
///
/// The name of a resource of the type being enumerated. Alternately, rather than a pointer, this parameter can be , where ID is the
/// integer identifier of the resource. For more information, see the Remarks section below.
///
///
///
/// Type: LONG_PTR
///
/// An application-defined parameter passed to the EnumResourceNames or EnumResourceNamesEx function. This parameter
/// can be used in error checking.
///
///
///
/// Type: BOOL
/// Returns TRUE to continue enumeration or FALSE to stop enumeration.
///
// BOOL CALLBACK EnumResNameProc( _In_opt_ HMODULE hModule, _In_ LPCTSTR lpszType, _In_ LPTSTR lpszName, _In_ LONG_PTR lParam); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648034(v=vs.85).aspx
[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
[SuppressUnmanagedCodeSecurity]
[PInvokeData("Winbase.h", MSDNShortId = "ms648034")]
[return: MarshalAs(UnmanagedType.Bool)]
public delegate bool EnumResNameProc(HINSTANCE hModule, ResourceId lpszType, ResourceId lpszName, IntPtr lParam);
///
/// An application-defined callback function used with the EnumResourceTypes and EnumResourceTypesEx functions. It
/// receives resource types. The ENUMRESTYPEPROC type defines a pointer to this callback function. EnumResTypeProc is a
/// placeholder for the application-defined function name.
///
///
/// Type: HMODULE
///
/// A handle to the module whose executable file contains the resources for which the types are to be enumerated. If this parameter
/// is NULL, the function enumerates the resource types in the module used to create the current process.
///
///
///
/// Type: LPTSTR
///
/// The type of resource for which the type is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is the integer identifier of the given resource type. For standard resource types, see
/// Resource Types. For more information, see the Remarks section below.
///
///
///
/// Type: LONG_PTR
///
/// An application-defined parameter passed to the EnumResourceTypes or EnumResourceTypesEx function. This parameter
/// can be used in error checking.
///
///
///
/// Type: BOOL
/// Returns TRUE to continue enumeration or FALSE to stop enumeration.
///
// BOOL CALLBACK EnumResTypeProc( _In_opt_ HMODULE hModule, _In_ LPTSTR lpszType, _In_ LONG_PTR lParam); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648041(v=vs.85).aspx
[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
[PInvokeData("Winbase.h", MSDNShortId = "ms648041")]
[return: MarshalAs(UnmanagedType.Bool)]
public delegate bool EnumResTypeProc(HINSTANCE hModule, ResourceId lpszType, IntPtr lParam);
/// Flags used by .
[PInvokeData("Winbase.h", MSDNShortId = "ms683200")]
[Flags]
public enum GET_MODULE_HANDLE_EX_FLAG
{
/// The lpModuleName parameter is an address in the module.
GET_MODULE_HANDLE_EX_FLAG_PIN = 0x00000001,
///
/// The module stays loaded until the process is terminated, no matter how many times FreeLibrary is called.
/// This option cannot be used with GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT.
///
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT = 0x00000002,
///
/// The reference count for the module is not incremented. This option is equivalent to the behavior of GetModuleHandle. Do not
/// pass the retrieved module handle to the FreeLibrary function; doing so can cause the DLL to be unmapped prematurely. For more
/// information, see Remarks.
/// This option cannot be used with GET_MODULE_HANDLE_EX_FLAG_PIN.
///
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS = 0x00000004,
}
/// Flags that may be passed to the function.
[PInvokeData("libloaderapi.h")]
[Flags]
public enum LoadLibraryExFlags
{
/// Define no flags, the function will behave as does.
None = 0,
///
/// If this value is used, and the executable module is a DLL, the system does not call DllMain for process and thread
/// initialization and termination. Also, the system does not load additional executable modules that are referenced by the
/// specified module.
///
///
/// Do not use this value; it is provided only for backward compatibility. If you are planning to access only data or resources
/// in the DLL, use or or both.
/// Otherwise, load the library as a DLL or executable module using the LoadLibrary function.
///
DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
///
/// If this value is used, the system does not check AppLocker rules or apply Software Restriction Policies for the DLL. This
/// action applies only to the DLL being loaded and not to its dependencies. This value is recommended for use in setup programs
/// that must run extracted DLLs during installation.
///
///
///
/// Windows Server 2008 R2 and Windows 7: On systems with KB2532445 installed, the caller must be running as "LocalSystem" or
/// "TrustedInstaller"; otherwise the system ignores this flag. For more information, see "You can circumvent AppLocker rules by
/// using an Office macro on a computer that is running Windows 7 or Windows Server 2008 R2" in the Help and Support Knowledge
/// Base at http://support.microsoft.com/kb/2532445.
///
///
/// Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP: AppLocker was introduced in Windows 7 and Windows
/// Server 2008 R2.
///
///
LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
///
/// If this value is used, the system maps the file into the calling process's virtual address space as if it were a data file.
/// Nothing is done to execute or prepare to execute the mapped file. Therefore, you cannot call functions like
/// GetModuleFileName, GetModuleHandle or GetProcAddress with this DLL. Using this value causes writes to read-only memory to
/// raise an access violation. Use this flag when you want to load a DLL only to extract messages or resources from it.
/// This value can be used with .
///
LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
///
/// Similar to , except that the DLL file is opened with exclusive write access for the
/// calling process. Other processes cannot open the DLL file for write access while it is in use. However, the DLL can still be
/// opened by other processes.
/// This value can be used with .
///
/// Windows Server 2003 and Windows XP: This value is not supported until Windows Vista.
LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
///
/// If this value is used, the system maps the file into the process's virtual address space as an image file. However, the
/// loader does not load the static imports or perform the other usual initialization steps. Use this flag when you want to load
/// a DLL only to extract messages or resources from it.
///
/// Unless the application depends on the file having the in-memory layout of an image, this value should be used with either
/// or . For more information, see the
/// Remarks section.
///
///
LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
///
/// If this value is used, the application's installation directory is searched for the DLL and its dependencies. Directories in
/// the standard search path are not searched. This value cannot be combined with .
///
///
/// Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008: This value requires KB2533623 to be installed.
/// Windows Server 2003 and Windows XP: This value is not supported.
///
LOAD_LIBRARY_SEARCH_APPLICATION_DIR = 0x00000200,
///
/// This value is a combination of , ,
/// and . Directories in the standard search path are not searched. This value cannot
/// be combined with .
///
/// This value represents the recommended maximum number of directories an application should include in its DLL search path.
///
///
///
/// Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008: This value requires KB2533623 to be installed.
/// Windows Server 2003 and Windows XP: This value is not supported.
///
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000,
///
/// If this value is used, the directory that contains the DLL is temporarily added to the beginning of the list of directories
/// that are searched for the DLL's dependencies. Directories in the standard search path are not searched.
/// The lpFileName parameter must specify a fully qualified path. This value cannot be combined with .
///
/// For example, if Lib2.dll is a dependency of C:\Dir1\Lib1.dll, loading Lib1.dll with this value causes the system to search
/// for Lib2.dll only in C:\Dir1. To search for Lib2.dll in C:\Dir1 and all of the directories in the DLL search path, combine
/// this value with .
///
///
///
/// Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008: This value requires KB2533623 to be installed.
/// Windows Server 2003 and Windows XP: This value is not supported.
///
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = 0x00000100,
///
/// If this value is used, %windows%\system32 is searched for the DLL and its dependencies. Directories in the standard search
/// path are not searched. This value cannot be combined with .
///
///
/// Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008: This value requires KB2533623 to be installed.
/// Windows Server 2003 and Windows XP: This value is not supported.
///
LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800,
///
/// If this value is used, directories added using the AddDllDirectory or the SetDllDirectory function are searched for the DLL
/// and its dependencies. If more than one directory has been added, the order in which the directories are searched is
/// unspecified. Directories in the standard search path are not searched. This value cannot be combined with .
///
///
/// Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008: This value requires KB2533623 to be installed.
/// Windows Server 2003 and Windows XP: This value is not supported.
///
LOAD_LIBRARY_SEARCH_USER_DIRS = 0x00000400,
///
/// If this value is used and lpFileName specifies an absolute path, the system uses the alternate file search strategy discussed
/// in the Remarks section to find associated executable modules that the specified module causes to be loaded. If this value is
/// used and lpFileName specifies a relative path, the behavior is undefined.
///
/// If this value is not used, or if lpFileName does not specify a path, the system uses the standard search strategy discussed
/// in the Remarks section to find associated executable modules that the specified module causes to be loaded.
///
/// This value cannot be combined with any LOAD_LIBRARY_SEARCH flag.
///
LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
}
/// The type of file to search.
[Flags]
public enum RESOURCE_ENUM_FLAGS
{
///
/// Searches the file specified by hModule, regardless of whether the file is an LN file, another type of LN file, or an .mui file.
///
RESOURCE_ENUM_LN = 0x0001,
///
/// Search for resources in .mui files associated with the LN file specified by hModule and with the current language
/// preferences, following the usual Resource Loader strategy (see User Interface Language Management). Alternately, if LangId is
/// nonzero, then only the specified .mui file will be searched. Typically this flag should be used only if hModule references an
/// LN file. If hModule references an .mui file, then that file is actually covered by the RESOURCE_ENUM_LN flag, despite the
/// name of the flag.
///
RESOURCE_ENUM_MUI = 0x0002,
/// Restricts the .mui files search to system-installed MUI languages.
RESOURCE_ENUM_MUI_SYSTEM = 0x0004,
///
/// Performs extra validation on the resource section and its reference in the PE header while doing the enumeration to ensure
/// that resources are properly formatted. The validation sets a maximum limit of 260 characters for each name that is enumerated.
///
RESOURCE_ENUM_VALIDATE = 0x0008,
/// Undocumented.
RESOURCE_ENUM_MODULE_EXACT = 0x0010,
}
/// Flags specifying details of the find operation.
public enum SEARCH_FLAGS
{
///
/// Test to find out if the value specified by lpStringValue is the first value in the source string indicated by lpStringSource.
///
FIND_STARTSWITH = 0x00100000,
///
/// Test to find out if the value specified by lpStringValue is the last value in the source string indicated by lpStringSource.
///
FIND_ENDSWITH = 0x00200000,
/// Search the string, starting with the first character of the string.
FIND_FROMSTART = 0x00400000,
/// Search the string in the reverse direction, starting with the last character of the string.
FIND_FROMEND = 0x00800000,
}
/// Adds a directory to the process DLL search path.
///
/// An absolute path to the directory to add to the search path. For example, to add the directory Dir2 to the process DLL search
/// path, specify \Dir2. For more information about paths, see Naming Files, Paths, and Namespaces.
///
///
///
/// If the function succeeds, the return value is an opaque pointer that can be passed to RemoveDllDirectory to remove the DLL
/// from the process DLL search path.
///
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// DLL_DIRECTORY_COOKIE WINAPI AddDllDirectory( _In_ PCWSTR NewDirectory); https://msdn.microsoft.com/en-us/library/windows/desktop/hh310513(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("LibLoaderAPI.h", MSDNShortId = "hh310513")]
public static extern IntPtr AddDllDirectory(string NewDirectory);
///
/// Disables the DLL_THREAD_ATTACH and DLL_THREAD_DETACH notifications for the specified dynamic-link library (DLL). This can reduce
/// the size of the working set for some applications.
///
///
/// A handle to the DLL module for which the DLL_THREAD_ATTACH and DLL_THREAD_DETACH notifications are to be disabled. The
/// LoadLibrary, LoadLibraryEx, or GetModuleHandle function returns this handle. Note that you cannot call
/// GetModuleHandle with NULL because this returns the base address of the executable image, not the DLL image.
///
///
/// If the function succeeds, the return value is nonzero.
///
/// If the function fails, the return value is zero. The DisableThreadLibraryCalls function fails if the DLL specified by
/// hModule has active static thread local storage, or if hModule is an invalid module handle. To get extended error information,
/// call GetLastError.
///
///
// BOOL WINAPI DisableThreadLibraryCalls( _In_ HMODULE hModule); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682579(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Winbase.h", MSDNShortId = "ms682579")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DisableThreadLibraryCalls([In] HINSTANCE hModule);
/// Enumerates language-specific resources, of the specified type and name, associated with a binary module.
///
/// Type: HMODULE
///
/// The handle to a module to be searched. Starting with Windows Vista, if this is a language-neutral Portable Executable (LN file),
/// then appropriate .mui files (if any exist) are included in the search. If this is a specific .mui file, only that file is
/// searched for resources.
///
/// If this parameter is NULL, that is equivalent to passing in a handle to the module used to create the current process.
///
///
/// Type: LPCTSTR
///
/// The type of resource for which the language is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is an integer value representing a predefined resource type. For a list of predefined
/// resource types, see Resource Types. For more information, see the Remarks section below.
///
///
///
/// Type: LPCTSTR
///
/// The name of the resource for which the language is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is the integer identifier of the resource. For more information, see the Remarks section below.
///
///
///
/// Type: ENUMRESLANGPROC
/// A pointer to the callback function to be called for each enumerated resource language. For more information, see EnumResLangProc.
///
///
/// Type: LONG_PTR
/// An application-defined value passed to the callback function. This parameter can be used in error checking.
///
///
/// Type: BOOL
/// Returns TRUE if successful or FALSE otherwise. To get extended error information, call GetLastError.
///
// BOOL WINAPI EnumResourceLanguages( _In_ HMODULE hModule, _In_ LPCTSTR lpType, _In_ LPCTSTR lpName, _In_ ENUMRESLANGPROC
// lpEnumFunc, _In_ LONG_PTR lParam); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648035(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "EnumResourceLanguagesW")]
[PInvokeData("Winbase.h", MSDNShortId = "ms648035")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumResourceLanguages([In] HINSTANCE hModule, [In] SafeResourceId lpType, [In] SafeResourceId lpName, EnumResLangProc lpEnumFunc, IntPtr lParam);
///
/// Enumerates language-specific resources, of the specified type and name, associated with a specified binary module. Extends
/// EnumResourceLanguages by allowing more control over the enumeration.
///
///
/// Type: HMODULE
///
/// The handle to a module to search. Typically this is a language-neutral Portable Executable (LN file), and if flag
/// RESOURCE_ENUM_MUI is set, then appropriate .mui files are included in the search. Alternately, this can be a handle to an
/// .mui file or other LN file. If this is a specific .mui file, only that file is searched for resources.
///
/// If this parameter is NULL, it is equivalent to passing in a handle to the module used to create the current process.
///
///
/// Type: LPCTSTR
///
/// The type of the resource for which the language is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is an integer value representing a predefined resource type. For a list of predefined
/// resource types, see Resource Types. For more information, see the Remarks section below.
///
///
///
/// Type: LPCTSTR
///
/// The name of the resource for which the language is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is the integer identifier of the resource. For more information, see the Remarks section below.
///
///
///
/// Type: ENUMRESLANGPROC
/// A pointer to the callback function to be called for each enumerated resource language. For more information, see EnumResLangProc.
///
///
/// Type: LONG_PTR
/// An application-defined value passed to the callback function. This parameter can be used in error checking.
///
///
/// Type: DWORD
///
/// The type of file to be searched. The following values are supported. Note that if dwFlags is zero, then the
/// RESOURCE_ENUM_LN and RESOURCE_ENUM_MUI flags are assumed to be specified.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// RESOURCE_ENUM_MUI0x0002
///
/// Search for language-specific resources in .mui files associated with the LN file specified by hModule. Alternately, if LangId is
/// nonzero, the only .mui file searched will be the one matching the specified LangId. Typically this flag should be used only if
/// hModule references an LN file. If hModule references an .mui file, then that file is actually covered by the RESOURCE_LN flag,
/// despite the name of the flag. See the Remarks section below for sequence of search.
///
///
/// -
/// RESOURCE_ENUM_LN0x0001
///
/// Searches the file specified by hModule, regardless of whether the file is an LN file, another type of LN file, or an .mui file.
///
///
/// -
/// RESOURCE_ENUM_MUI_SYSTEM0x0004
/// Restricts the .mui files search to system-installed MUI languages.
///
/// -
/// RESOURCE_ENUM_VALIDATE0x0008
///
/// Performs extra validation on the resource section and its reference in the PE header while doing the enumeration to ensure that
/// resources are properly formatted.
///
///
///
///
///
///
/// Type: LANGID
///
/// The localization language used to filter the search in the .mui file. This parameter is used only when the
/// RESOURCE_ENUM_MUI flag is set in dwFlags. If zero is specified, then all .mui files are included in the search. If a
/// nonzero LangId is specified, then the only .mui file searched will be the one matching the specified LangId.
///
///
///
/// Type: BOOL
///
/// Returns TRUE if the function succeeds or FALSE if the function does not find a resource of the type specified, or
/// if the function fails for another reason. To get extended error information, call GetLastError.
///
///
// BOOL WINAPI EnumResourceLanguagesEx( _In_ HMODULE hModule, _In_ LPCTSTR lpType, _In_ LPCTSTR lpName, _In_ ENUMRESLANGPROC
// lpEnumFunc, _In_ LONG_PTR lParam, _In_ DWORD dwFlags, _In_ LANGID LangId); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648036(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "EnumResourceLanguagesExW")]
[PInvokeData("Winbase.h", MSDNShortId = "ms648036")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumResourceLanguagesEx([In] HINSTANCE hModule, [In] SafeResourceId lpType, [In] SafeResourceId lpName, EnumResLangProc lpEnumFunc, IntPtr lParam, RESOURCE_ENUM_FLAGS dwFlags, ushort LangId);
/// Enumerates language-specific resources, of the specified type and name, associated with a specified binary module.
///
///
/// The handle to a module to search. Typically this is a language-neutral Portable Executable (LN file), and if flag
/// RESOURCE_ENUM_MUI is set, then appropriate .mui files are included in the search. Alternately, this can be a handle to an
/// .mui file or other LN file. If this is a specific .mui file, only that file is searched for resources.
///
/// If this parameter is NULL, it is equivalent to passing in a handle to the module used to create the current process.
///
///
/// The type of the resource for which the language is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is an integer value representing a predefined resource type. For a list of predefined
/// resource types, see Resource Types. For more information, see the Remarks section below.
///
///
/// The name of the resource for which the language is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is the integer identifier of the resource. For more information, see the Remarks section below.
///
///
/// The type of file to be searched. Note that if is zero, then the RESOURCE_ENUM_LN and
/// RESOURCE_ENUM_MUI flags are assumed to be specified.
///
///
/// The localization language used to filter the search in the .mui file. This parameter is used only when the
/// RESOURCE_ENUM_MUI flag is set in dwFlags. If zero is specified, then all .mui files are included in the search. If a
/// nonzero LangId is specified, then the only .mui file searched will be the one matching the specified LangId.
///
/// A list of the language identifiers (see Language Identifiers) for which a resource was found.
[PInvokeData("Winbase.h", MSDNShortId = "ms648036")]
public static IList EnumResourceLanguagesEx(HINSTANCE hModule, SafeResourceId type, SafeResourceId name, RESOURCE_ENUM_FLAGS flags = 0, ushort langFilter = 0)
{
var list = new List();
if (!EnumResourceLanguagesEx(hModule, type, name, (p, i, n, luid, l) => { list.Add(luid); return true; }, IntPtr.Zero, flags, langFilter))
Win32Error.ThrowLastError();
return list;
}
///
/// Enumerates resources of a specified type within a binary module. For Windows Vista and later, this is typically a
/// language-neutral Portable Executable (LN file), and the enumeration will also include resources from the corresponding
/// language-specific resource files (.mui files) that contain localizable language resources. It is also possible for hModule to
/// specify an .mui file, in which case only that file is searched for resources.
///
///
/// A handle to a module to be searched. Starting with Windows Vista, if this is an LN file, then appropriate .mui files (if any
/// exist) are included in the search.
/// If this parameter is NULL, that is equivalent to passing in a handle to the module used to create the current process.
///
///
/// The type of the resource for which the name is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is an integer value representing a predefined resource type.
///
/// A pointer to the callback function to be called for each enumerated resource name or ID.
/// An application-defined value passed to the callback function. This parameter can be used in error checking.
///
/// The return value is TRUE if the function succeeds or FALSE if the function does not find a resource of the type specified, or if
/// the function fails for another reason. To get extended error information, call GetLastError.
///
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "EnumResourceNamesW")]
[SuppressUnmanagedCodeSecurity]
[PInvokeData("WinBase.h", MSDNShortId = "ms648037")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumResourceNames(HINSTANCE hModule, SafeResourceId lpszType, EnumResNameProc lpEnumFunc, IntPtr lParam);
///
/// Enumerates resources of a specified type within a binary module. For Windows Vista and later, this is typically a
/// language-neutral Portable Executable (LN file), and the enumeration will also include resources from the corresponding
/// language-specific resource files (.mui files) that contain localizable language resources. It is also possible for hModule to
/// specify an .mui file, in which case only that file is searched for resources.
///
///
/// A handle to a module to be searched. Starting with Windows Vista, if this is an LN file, then appropriate .mui files (if any
/// exist) are included in the search.
/// If this parameter is NULL, that is equivalent to passing in a handle to the module used to create the current process.
///
///
/// The type of the resource for which the name is being enumerated. Alternately, rather than a string, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is an integer value representing a predefined resource type.
///
///
///
/// The type of file to search. The following values are supported. Note that if dwFlags is zero, then the RESOURCE_ENUM_LN
/// and RESOURCE_ENUM_MUI flags are assumed to be specified.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// RESOURCE_ENUM_MUI0x0002
///
/// Search for resources in .mui files associated with the LN file specified by hModule and with the current language preferences,
/// following the usual Resource Loader strategy (see User Interface Language Management). Alternately, if LangId is nonzero, then
/// only the specified .mui file will be searched. Typically this flag should be used only if hModule references an LN file. If
/// hModule references an .mui file, then that file is actually covered by the RESOURCE_ENUM_LN flag, despite the name of the flag.
///
///
/// -
/// RESOURCE_ENUM_LN0x0001
///
/// Searches the file specified by hModule, regardless of whether the file is an LN file, another type of LN file, or an .mui file.
///
///
/// -
/// RESOURCE_ENUM_VALIDATE0x0008
///
/// Performs extra validation on the resource section and its reference in the PE header while doing the enumeration to ensure that
/// resources are properly formatted. The validation sets a maximum limit of 260 characters for each name that is enumerated.
///
///
///
///
///
///
///
/// The localization language used to filter the search in the MUI module. This parameter is used only when the
/// RESOURCE_ENUM_MUI flag is set in dwFlags. If zero is specified, then all .mui files that match current language
/// preferences are included in the search, following the usual Resource Loader strategy (see User Interface Language Management). If
/// a nonzero LangId is specified, then the only .mui file searched will be the one matching the specified LangId.
///
///
/// ///
/// A list of strings for each of the resources matching .
[PInvokeData("WinBase.h", MSDNShortId = "ms648037")]
public static IList EnumResourceNamesEx(HINSTANCE hModule, SafeResourceId type, RESOURCE_ENUM_FLAGS flags = 0, ushort langFilter = 0)
{
var list = new List();
if (!EnumResourceNamesEx(hModule, type, (m, t, name, l) => { list.Add(name); return true; }, IntPtr.Zero, flags, langFilter))
Win32Error.ThrowLastError();
return list;
}
///
/// Enumerates resources of a specified type that are associated with a specified binary module. The search can include both an LN
/// file and its associated .mui files, or it can be limited in several ways.
///
///
/// Type: HMODULE
///
/// The handle to a module to search. Typically this is an LN file, and if flag RESOURCE_ENUM_MUI is set, then appropriate
/// .mui files are included in the search. Alternately, this can be a handle to an .mui file or other LN file.
///
/// If this parameter is NULL, it is equivalent to passing in a handle to the module used to create the current process.
///
///
/// Type: LPCTSTR
///
/// The type of the resource for which the name is being enumerated. Alternately, rather than a pointer, this parameter can be
/// MAKEINTRESOURCE(ID), where ID is an integer value representing a predefined resource type. For a list of predefined
/// resource types, see Resource Types. For more information, see the Remarks section below.
///
///
///
/// Type: ENUMRESNAMEPROC
/// A pointer to the callback function to be called for each enumerated resource name. For more information, see EnumResNameProc.
///
///
/// Type: LONG_PTR
/// An application-defined value passed to the callback function. This parameter can be used in error checking.
///
///
/// Type: DWORD
///
/// The type of file to search. The following values are supported. Note that if dwFlags is zero, then the RESOURCE_ENUM_LN
/// and RESOURCE_ENUM_MUI flags are assumed to be specified.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// RESOURCE_ENUM_MUI0x0002
///
/// Search for resources in .mui files associated with the LN file specified by hModule and with the current language preferences,
/// following the usual Resource Loader strategy (see User Interface Language Management). Alternately, if LangId is nonzero, then
/// only the specified .mui file will be searched. Typically this flag should be used only if hModule references an LN file. If
/// hModule references an .mui file, then that file is actually covered by the RESOURCE_ENUM_LN flag, despite the name of the flag.
///
///
/// -
/// RESOURCE_ENUM_LN0x0001
///
/// Searches the file specified by hModule, regardless of whether the file is an LN file, another type of LN file, or an .mui file.
///
///
/// -
/// RESOURCE_ENUM_VALIDATE0x0008
///
/// Performs extra validation on the resource section and its reference in the PE header while doing the enumeration to ensure that
/// resources are properly formatted. The validation sets a maximum limit of 260 characters for each name that is enumerated.
///
///
///
///
///
///
/// Type: LANGID
///
/// The localization language used to filter the search in the MUI module. This parameter is used only when the
/// RESOURCE_ENUM_MUI flag is set in dwFlags. If zero is specified, then all .mui files that match current language
/// preferences are included in the search, following the usual Resource Loader strategy (see User Interface Language Management). If
/// a nonzero LangId is specified, then the only .mui file searched will be the one matching the specified LangId.
///
///
///
/// Type: BOOL
///
/// The function TRUE if successful, or FALSE if the function does not find a resource of the type specified, or if the
/// function fails for another reason. To get extended error information, call GetLastError.
///
///
// BOOL WINAPI EnumResourceNamesEx( _In_opt_ HMODULE hModule, _In_ LPCTSTR lpszType, _In_ ENUMRESNAMEPROC lpEnumFunc, _In_ LONG_PTR
// lParam, _In_ DWORD dwFlags, _In_ LANGID LangId); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648038(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "EnumResourceNamesExW")]
[PInvokeData("Winbase.h", MSDNShortId = "ms648038")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumResourceNamesEx(HINSTANCE hModule, SafeResourceId lpszType, EnumResNameProc lpEnumFunc, IntPtr lParam, RESOURCE_ENUM_FLAGS dwFlags, ushort LangId);
///
///
/// Enumerates resource types within a binary module. Starting with Windows Vista, this is typically a language-neutral Portable
/// Executable (LN file), and the enumeration also includes resources from one of the corresponding language-specific resource files
/// (.mui files)—if one exists—that contain localizable language resources. It is also possible to use hModule to specify a .mui
/// file, in which case only that file is searched for resource types.
///
///
/// Alternately, applications can call EnumResourceTypesEx, which provides more precise control over which resource files to enumerate.
///
///
///
/// Type: HMODULE
///
/// A handle to a module to be searched. This handle must be obtained through LoadLibrary or LoadLibraryEx. See Remarks
/// for more information.
///
/// If this parameter is NULL, that is equivalent to passing in a handle to the module used to create the current process.
///
///
/// Type: ENUMRESTYPEPROC
///
/// A pointer to the callback function to be called for each enumerated resource type. For more information, see the
/// EnumResTypeProc function.
///
///
///
/// Type: LONG_PTR
/// An application-defined value passed to the callback function.
///
///
/// Type: BOOL
/// Returns TRUE if successful; otherwise, FALSE. To get extended error information, call GetLastError.
///
// BOOL WINAPI EnumResourceTypes( _In_opt_ HMODULE hModule, _In_ ENUMRESTYPEPROC lpEnumFunc, _In_ LONG_PTR lParam); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648039(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "EnumResourceTypesW")]
[PInvokeData("Winbase.h", MSDNShortId = "ms648039")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumResourceTypes([In] HINSTANCE hModule, EnumResTypeProc lpEnumFunc, IntPtr lParam);
///
///
/// Enumerates resource types associated with a specified binary module. The search can include both a language-neutral Portable
/// Executable file (LN
/// file) and its associated .mui files. Alternately, it can be limited to a single binary module of any type, or to the .mui files
/// associated with a single LN file. The search can also be limited to a single associated .mui file that contains resources for a
/// specific language.
///
///
/// For each resource type found, EnumResourceTypesEx calls an application-defined callback function lpEnumFunc, passing the
/// resource type it finds, as well as the various other parameters that were passed to EnumResourceTypesEx.
///
///
///
/// Type: HMODULE
///
/// The handle to a module to be searched. Typically this is an LN file, and if flag RESOURCE_ENUM_MUI is set, then
/// appropriate .mui files can be included in the search. Alternately, this can be a handle to an .mui file or other LN file.
///
/// If this parameter is NULL, it is equivalent to passing in a handle to the module used to create the current process.
///
///
/// Type: ENUMRESTYPEPROC
/// A pointer to the callback function to be called for each enumerated resource type. For more information, see EnumResTypeProc.
///
///
/// Type: LONG_PTR
/// An application-defined value passed to the callback function.
///
///
/// Type: DWORD
///
/// The type of file to be searched. The following values are supported. Note that if dwFlags is zero, then the
/// RESOURCE_ENUM_LN and RESOURCE_ENUM_MUI flags are assumed to be specified.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// RESOURCE_ENUM_MUI0x0002
///
/// Search for resource types in one of the .mui files associated with the file specified by hModule and with the current language
/// preferences, following the usual Resource Loader strategy (see User Interface Language Management). Alternately, if LangId is
/// nonzero, then only the .mui file of the language as specified by LangId will be searched. Typically this flag should be used only
/// if hModule references an LN file. If hModule references an .mui file, then that file is actually covered by the RESOURCE_ENUM_LN
/// flag, despite the name of the flag.
///
///
/// -
/// RESOURCE_ENUM_LN0x0001
/// Searches only the file specified by hModule, regardless of whether the file is an LN file or an .mui file.
///
/// -
/// RESOURCE_ENUM_VALIDATE0x0008
///
/// Performs extra validation on the resource section and its reference in the PE header while doing the enumeration to ensure that
/// resources are properly formatted. The validation sets a maximum limit of 260 characters for each type that is enumerated.
///
///
///
///
///
///
/// Type: LANGID
///
/// The language used to filter the search in the MUI module. This parameter is used only when the RESOURCE_ENUM_MUI flag is
/// set in dwFlags. If zero is specified, then all .mui files that match current language preferences are included in the search,
/// following the usual Resource Loader strategy (see User Interface Language Management). If a nonzero LangId is specified, then the
/// only .mui file searched will be the one matching the specified LangId.
///
///
///
/// Type: BOOL
///
/// Returns TRUE if successful or FALSE if the function does not find a resource of the type specified, or if the
/// function fails for another reason. To get extended error information, call GetLastError.
///
///
// BOOL WINAPI EnumResourceTypesEx( _In_opt_ HMODULE hModule, _In_ ENUMRESTYPEPROC lpEnumFunc, _In_ LONG_PTR lParam, _In_ DWORD
// dwFlags, _In_ LANGID LangId); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648040(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms648040")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumResourceTypesEx(HINSTANCE hModule, EnumResTypeProc lpEnumFunc, IntPtr lParam, RESOURCE_ENUM_FLAGS dwFlags, ushort LangId);
///
///
/// Enumerates resource types associated with a specified binary module. The search can include both a language-neutral Portable
/// Executable file (LN
/// file) and its associated .mui files. Alternately, it can be limited to a single binary module of any type, or to the .mui files
/// associated with a single LN file. The search can also be limited to a single associated .mui file that contains resources for a
/// specific language.
///
///
///
/// Type: HMODULE
///
/// The handle to a module to be searched. Typically this is an LN file, and if flag RESOURCE_ENUM_MUI is set, then
/// appropriate .mui files can be included in the search. Alternately, this can be a handle to an .mui file or other LN file.
///
/// If this parameter is NULL, it is equivalent to passing in a handle to the module used to create the current process.
///
///
/// Type: DWORD
///
/// The type of file to be searched. The following values are supported. Note that if dwFlags is zero, then the
/// RESOURCE_ENUM_LN and RESOURCE_ENUM_MUI flags are assumed to be specified.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// RESOURCE_ENUM_MUI0x0002
///
/// Search for resource types in one of the .mui files associated with the file specified by hModule and with the current language
/// preferences, following the usual Resource Loader strategy (see User Interface Language Management). Alternately, if LangId is
/// nonzero, then only the .mui file of the language as specified by LangId will be searched. Typically this flag should be used only
/// if hModule references an LN file. If hModule references an .mui file, then that file is actually covered by the RESOURCE_ENUM_LN
/// flag, despite the name of the flag.
///
///
/// -
/// RESOURCE_ENUM_LN0x0001
/// Searches only the file specified by hModule, regardless of whether the file is an LN file or an .mui file.
///
/// -
/// RESOURCE_ENUM_VALIDATE0x0008
///
/// Performs extra validation on the resource section and its reference in the PE header while doing the enumeration to ensure that
/// resources are properly formatted. The validation sets a maximum limit of 260 characters for each type that is enumerated.
///
///
///
///
///
///
/// Type: LANGID
///
/// The language used to filter the search in the MUI module. This parameter is used only when the RESOURCE_ENUM_MUI flag is
/// set in dwFlags. If zero is specified, then all .mui files that match current language preferences are included in the search,
/// following the usual Resource Loader strategy (see User Interface Language Management). If a nonzero LangId is specified, then the
/// only .mui file searched will be the one matching the specified LangId.
///
///
/// List of resource identifiers.
public static IList EnumResourceTypesEx([In] HINSTANCE hModule, RESOURCE_ENUM_FLAGS flags = 0, ushort langFilter = 0)
{
var list = new List();
if (!EnumResourceTypesEx(hModule, (p, t, l) => { list.Add(t); return true; }, IntPtr.Zero, flags, langFilter))
Win32Error.ThrowLastError();
return list;
}
///
/// Determines the location of a resource with the specified type and name in the specified module.
/// To specify a language, use the FindResourceEx function.
///
///
/// Type: HMODULE
///
/// A handle to the module whose portable executable file or an accompanying MUI file contains the resource. If this parameter is
/// NULL, the function searches the module used to create the current process.
///
///
///
/// Type: LPCTSTR
///
/// The name of the resource. Alternately, rather than a pointer, this parameter can be MAKEINTRESOURCE(ID), where ID is the
/// integer identifier of the resource. For more information, see the Remarks section below.
///
///
///
/// Type: LPCTSTR
///
/// The resource type. Alternately, rather than a pointer, this parameter can be MAKEINTRESOURCE(ID), where ID is the integer
/// identifier of the given resource type. For standard resource types, see Resource Types. For more information, see the Remarks
/// section below.
///
///
///
/// Type: HRSRC
///
/// If the function succeeds, the return value is a handle to the specified resource's information block. To obtain a handle to the
/// resource, pass this handle to the function.
///
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
// HRSRC WINAPI FindResource( _In_opt_ HMODULE hModule, _In_ LPCTSTR lpName, _In_ LPCTSTR lpType); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648042(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindResourceW")]
[PInvokeData("Winbase.h", MSDNShortId = "ms648042")]
public static extern HRSRC FindResource([In] HINSTANCE hModule, [In] SafeResourceId lpName, [In] SafeResourceId lpType);
/// Determines the location of the resource with the specified type, name, and language in the specified module.
///
/// Type: HMODULE
///
/// A handle to the module whose portable executable file or an accompanying MUI file contains the resource. If this parameter is
/// NULL, the function searches the module used to create the current process.
///
///
///
/// Type: LPCTSTR
///
/// The resource type. Alternately, rather than a pointer, this parameter can be MAKEINTRESOURCE(ID), where ID is the integer
/// identifier of the given resource type. For standard resource types, see Resource Types. For more information, see the Remarks
/// section below.
///
///
///
/// Type: LPCTSTR
///
/// The name of the resource. Alternately, rather than a pointer, this parameter can be MAKEINTRESOURCE(ID), where ID is the
/// integer identifier of the resource. For more information, see the Remarks section below.
///
///
///
/// Type: WORD
/// The language of the resource. If this parameter is , the current language associated with the calling thread is used.
///
/// To specify a language other than the current language, use the MAKELANGID macro to create this parameter. For more
/// information, see MAKELANGID.
///
///
///
/// Type: HRSRC
///
/// If the function succeeds, the return value is a handle to the specified resource's information block. To obtain a handle to the
/// resource, pass this handle to the LoadResource function.
///
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
// HRSRC WINAPI FindResourceEx( _In_opt_ HMODULE hModule, _In_ LPCTSTR lpType, _In_ LPCTSTR lpName, _In_ WORD wLanguage); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648043(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindResourceExW")]
[PInvokeData("Winbase.h", MSDNShortId = "ms648043")]
public static extern HRSRC FindResourceEx([In] HINSTANCE hModule, [In] SafeResourceId lpType, [In] SafeResourceId lpName, ushort wLanguage);
/// Locates a Unicode string (wide characters) in another Unicode string for a non-linguistic comparison.
///
///
/// Flags specifying details of the find operation. These flags are mutually exclusive, with FIND_FROMSTART being the default. The
/// application can specify just one of the find flags.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// FIND_FROMSTART
/// Search the string, starting with the first character of the string.
///
/// -
/// FIND_FROMEND
/// Search the string in the reverse direction, starting with the last character of the string.
///
/// -
/// FIND_STARTSWITH
/// Test to find out if the value specified by lpStringValue is the first value in the source string indicated by lpStringSource.
///
/// -
/// FIND_ENDSWITH
/// Test to find out if the value specified by lpStringValue is the last value in the source string indicated by lpStringSource.
///
///
///
///
/// Pointer to the source string, in which the function searches for the string specified by lpStringValue.
///
/// Size, in characters excluding the terminating null character, of the string indicated by lpStringSource. The application must
/// normally specify a positive number, or 0. The application can specify -1 if the source string is null-terminated and the function
/// should calculate the size automatically.
///
/// Pointer to the search string for which the function searches in the source string.
///
/// Size, in characters excluding the terminating null character, of the string indicated by lpStringValue. The application must
/// normally specify a positive number, or 0. The application can specify -1 if the string is null-terminated and the function should
/// calculate the size automatically.
///
///
/// TRUE if the function is to perform a case-insensitive comparison, and FALSE otherwise. The comparison is not a
/// linguistic operation and is not appropriate for all locales and languages. Its behavior is similar to that for English.
///
///
///
/// Returns a 0-based index into the source string indicated by lpStringSource if successful. If the function succeeds, the found
/// string is the same size as the value of lpStringValue. A return value of 0 indicates that the function found a match at the
/// beginning of the source string.
///
///
/// The function returns -1 if it does not succeed or if it does not find the search string. To get extended error information, the
/// application can call GetLastError, which can return one of the following error codes:
///
///
// int FindStringOrdinal( _In_ DWORD dwFindStringOrdinalFlags, _In_ LPCWSTR lpStringSource, _In_ int cchSource, _In_ LPCWSTR
// lpStringValue, _In_ int cchValue, _In_ BOOL bIgnoreCase); https://msdn.microsoft.com/en-us/library/windows/desktop/dd318061(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[PInvokeData("Libloaderapi.h", MSDNShortId = "dd318061")]
public static extern int FindStringOrdinal(SEARCH_FLAGS dwFindStringOrdinalFlags, string lpStringSource, int cchSource, string lpStringValue, int cchValue, [MarshalAs(UnmanagedType.Bool)] bool bIgnoreCase);
///
/// Frees the loaded dynamic-link library (DLL) module and, if necessary, decrements its reference count. When the reference count
/// reaches zero, the module is unloaded from the address space of the calling process and the handle is no longer valid.
///
///
/// A handle to the loaded library module. The LoadLibrary, LoadLibraryEx, GetModuleHandle, or
/// GetModuleHandleEx function returns this handle.
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call the GetLastError function.
///
// BOOL WINAPI FreeLibrary( _In_ HMODULE hModule); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683152(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Winbase.h", MSDNShortId = "ms683152")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FreeLibrary(HINSTANCE hModule);
///
/// Decrements the reference count of a loaded dynamic-link library (DLL) by one, then calls ExitThread to terminate the
/// calling thread. The function does not return.
///
///
///
/// A handle to the DLL module whose reference count the function decrements. The LoadLibrary or GetModuleHandleEx
/// function returns this handle.
///
///
/// Do not call this function with a handle returned by the GetModuleHandle function, since this function does not maintain a
/// reference count for the module.
///
///
/// The exit code for the calling thread.
/// This function does not return a value. Invalid module handles are ignored.
// VOID WINAPI FreeLibraryAndExitThread( _In_ HMODULE hModule, _In_ DWORD dwExitCode); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683153(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("Winbase.h", MSDNShortId = "ms683153")]
public static extern void FreeLibraryAndExitThread([In] HINSTANCE hModule, uint dwExitCode);
///
///
/// [This function is obsolete and is only supported for backward compatibility with 16-bit Windows. For 32-bit Windows applications,
/// it is not necessary to free the resources loaded using LoadResource. If used on 32 or 64-bit Windows systems, this
/// function will return FALSE.]
///
///
/// Decrements (decreases by one) the reference count of a loaded resource. When the reference count reaches zero, the memory
/// occupied by the resource is freed.
///
///
///
/// Type: HGLOBAL
/// A handle of the resource. It is assumed that hglbResource was created by LoadResource.
///
///
/// Type: BOOL
/// If the function succeeds, the return value is zero.
/// If the function fails, the return value is nonzero, which indicates that the resource has not been freed.
///
// BOOL WINAPI FreeResource( _In_ HGLOBAL hglbResource); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648044(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("Winbase.h", MSDNShortId = "ms648044")]
[Obsolete]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FreeResource([In] HRSRCDATA hglbResource);
///
///
/// Retrieves the fully qualified path for the file that contains the specified module. The module must have been loaded by the
/// current process.
///
/// To locate the file for a module that was loaded by another process, use the GetModuleFileNameEx function.
///
///
///
/// A handle to the loaded module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves
/// the path of the executable file of the current process.
///
///
/// The GetModuleFileName function does not retrieve the path for modules that were loaded using the
/// LOAD_LIBRARY_AS_DATAFILE flag. For more information, see LoadLibraryEx.
///
///
///
///
/// A pointer to a buffer that receives the fully qualified path of the module. If the length of the path is less than the size that
/// the nSize parameter specifies, the function succeeds and the path is returned as a null-terminated string.
///
///
/// If the length of the path exceeds the size that the nSize parameter specifies, the function succeeds and the string is truncated
/// to nSize characters including the terminating null character.
///
/// Windows XP: The string is truncated to nSize characters and is not null-terminated.
///
/// The string returned will use the same format that was specified when the module was loaded. Therefore, the path can be a long or
/// short file name, and can use the prefix "\\?\". For more information, see Naming a File.
///
///
/// The size of the lpFilename buffer, in TCHARs.
///
///
/// If the function succeeds, the return value is the length of the string that is copied to the buffer, in characters, not including
/// the terminating null character. If the buffer is too small to hold the module name, the string is truncated to nSize characters
/// including the terminating null character, the function returns nSize, and the function sets the last error to ERROR_INSUFFICIENT_BUFFER.
///
///
/// Windows XP: If the buffer is too small to hold the module name, the function returns nSize. The last error code remains
/// ERROR_SUCCESS. If nSize is zero, the return value is zero and the last error code is ERROR_SUCCESS.
///
/// If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError.
///
// DWORD WINAPI GetModuleFileName( _In_opt_ HMODULE hModule, _Out_ LPTSTR lpFilename, _In_ DWORD nSize); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms683197")]
public static extern uint GetModuleFileName(HINSTANCE hModule, StringBuilder lpFilename, uint nSize);
///
/// Retrieves the fully qualified path for the file that contains the specified module. The module must have been loaded by the
/// current process.
/// To locate the file for a module that was loaded by another process, use the GetModuleFileNameEx function.
///
///
/// A handle to the loaded module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path of
/// the executable file of the current process.
///
/// The GetModuleFileName function does not retrieve the path for modules that were loaded using the LOAD_LIBRARY_AS_DATAFILE flag.
/// For more information, see LoadLibraryEx.
///
///
///
/// The string returned will use the same format that was specified when the module was loaded. Therefore, the path can be a long or
/// short file name, and can use the prefix "\\?\". For more information, see Naming a File.
///
[SecurityCritical]
[PInvokeData("WinBase.h", MSDNShortId = "ms683197")]
public static string GetModuleFileName(HINSTANCE hModule)
{
var buffer = new StringBuilder(MAX_PATH);
Label_000B:
var num1 = GetModuleFileName(hModule, buffer, (uint)buffer.Capacity);
if (num1 == 0)
throw new Win32Exception();
if (num1 == buffer.Capacity && Marshal.GetLastWin32Error() == Win32Error.ERROR_INSUFFICIENT_BUFFER)
{
buffer.EnsureCapacity(buffer.Capacity * 2);
goto Label_000B;
}
return buffer.ToString();
}
///
/// Retrieves a module handle for the specified module. The module must have been loaded by the calling process.
/// To avoid the race conditions described in the Remarks section, use the GetModuleHandleEx function.
///
///
///
/// The name of the loaded module (either a .dll or .exe file). If the file name extension is omitted, the default library extension
/// .dll is appended. The file name string can include a trailing point character (.) to indicate that the module name has no
/// extension. The string does not have to specify a path. When specifying a path, be sure to use backslashes (\), not forward
/// slashes (/). The name is compared (case independently) to the names of modules currently mapped into the address space of the
/// calling process.
///
///
/// If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process (.exe file).
///
///
/// The GetModuleHandle function does not retrieve handles for modules that were loaded using the
/// LOAD_LIBRARY_AS_DATAFILE flag. For more information, see LoadLibraryEx.
///
///
///
/// If the function succeeds, the return value is a handle to the specified module.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
// HMODULE WINAPI GetModuleHandle( _In_opt_ LPCTSTR lpModuleName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683199(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms683199")]
public static extern SafeHINSTANCE GetModuleHandle([Optional] string lpModuleName);
///
/// Retrieves a module handle for the specified module and increments the module's reference count unless
/// GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT is specified. The module must have been loaded by the calling process.
///
///
/// This parameter can be zero or one or more of the following values. If the module's reference count is incremented, the caller
/// must use the FreeLibrary function to decrement the reference count when the module handle is no longer needed.
///
///
/// The name of the loaded module (either a .dll or .exe file), or an address in the module (if dwFlags is GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS).
///
/// For a module name, if the file name extension is omitted, the default library extension .dll is appended. The file name string
/// can include a trailing point character (.) to indicate that the module name has no extension. The string does not have to specify
/// a path. When specifying a path, be sure to use backslashes (\), not forward slashes (/). The name is compared (case
/// independently) to the names of modules currently mapped into the address space of the calling process.
///
/// If this parameter is NULL, the function returns a handle to the file used to create the calling process (.exe file).
///
///
/// A handle to the specified module. If the function fails, this parameter is NULL.
///
/// The GetModuleHandleEx function does not retrieve handles for modules that were loaded using the
/// LOAD_LIBRARY_AS_DATAFILE flag. For more information, see LoadLibraryEx.
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, see GetLastError.
///
// BOOL WINAPI GetModuleHandleEx( _In_ DWORD dwFlags, _In_opt_ LPCTSTR lpModuleName, _Out_ HMODULE *phModule); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683200(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms683200")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG dwFlags, [Optional] string lpModuleName, out SafeHINSTANCE phModule);
///
/// Retrieves a module handle for the specified module and increments the module's reference count unless
/// GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT is specified. The module must have been loaded by the calling process.
///
///
/// This parameter can be zero or one or more of the following values. If the module's reference count is incremented, the caller
/// must use the FreeLibrary function to decrement the reference count when the module handle is no longer needed.
///
///
/// The name of the loaded module (either a .dll or .exe file), or an address in the module (if dwFlags is GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS).
///
/// For a module name, if the file name extension is omitted, the default library extension .dll is appended. The file name string
/// can include a trailing point character (.) to indicate that the module name has no extension. The string does not have to specify
/// a path. When specifying a path, be sure to use backslashes (\), not forward slashes (/). The name is compared (case
/// independently) to the names of modules currently mapped into the address space of the calling process.
///
/// If this parameter is NULL, the function returns a handle to the file used to create the calling process (.exe file).
///
///
/// A handle to the specified module. If the function fails, this parameter is NULL.
///
/// The GetModuleHandleEx function does not retrieve handles for modules that were loaded using the
/// LOAD_LIBRARY_AS_DATAFILE flag. For more information, see LoadLibraryEx.
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, see GetLastError.
///
// BOOL WINAPI GetModuleHandleEx( _In_ DWORD dwFlags, _In_opt_ LPCTSTR lpModuleName, _Out_ HMODULE *phModule); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683200(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms683200")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG dwFlags, [In] IntPtr lpModuleName, out SafeHINSTANCE phModule);
/// Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
///
///
/// A handle to the DLL module that contains the function or variable. The LoadLibrary, LoadLibraryEx,
/// LoadPackagedLibrary, or GetModuleHandle function returns this handle.
///
///
/// The GetProcAddress function does not retrieve addresses from modules that were loaded using the
/// LOAD_LIBRARY_AS_DATAFILE flag. For more information, see LoadLibraryEx.
///
///
///
/// The function or variable name, or the function's ordinal value. If this parameter is an ordinal value, it must be in the
/// low-order word; the high-order word must be zero.
///
///
/// If the function succeeds, the return value is the address of the exported function or variable.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
// FARPROC WINAPI GetProcAddress( _In_ HMODULE hModule, _In_ LPCSTR lpProcName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683212(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Winbase.h", MSDNShortId = "ms683212")]
public static extern IntPtr GetProcAddress(HINSTANCE hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
///
///
/// Loads the specified module into the address space of the calling process. The specified module may cause other modules to be loaded.
///
/// For additional load options, use the LoadLibraryEx function.
///
///
///
/// The name of the module. This can be either a library module (a .dll file) or an executable module (an .exe file). The name
/// specified is the file name of the module and is not related to the name stored in the library module itself, as specified by the
/// LIBRARY keyword in the module-definition (.def) file.
///
/// If the string specifies a full path, the function searches only that path for the module.
///
/// If the string specifies a relative path or a module name without a path, the function uses a standard search strategy to find the
/// module; for more information, see the Remarks.
///
///
/// If the function cannot find the module, the function fails. When specifying a path, be sure to use backslashes (\), not forward
/// slashes (/). For more information about paths, see Naming a File or Directory.
///
///
/// If the string specifies a module name without a path and the file name extension is omitted, the function appends the default
/// library extension .dll to the module name. To prevent the function from appending .dll to the module name, include a trailing
/// point character (.) in the module name string.
///
///
///
/// If the function succeeds, the return value is a handle to the module.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
// HMODULE WINAPI LoadLibrary( _In_ LPCTSTR lpFileName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms684175(v=vs.85).aspx
[PInvokeData("Winbase.h", MSDNShortId = "ms684175")]
[DllImport(Lib.Kernel32, CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
public static extern SafeHINSTANCE LoadLibrary([In, MarshalAs(UnmanagedType.LPTStr)] string lpFileName);
///
/// Loads the specified module into the address space of the calling process. The specified module may cause other modules to be loaded.
///
///
///
/// A string that specifies the file name of the module to load. This name is not related to the name stored in a library module
/// itself, as specified by the LIBRARY keyword in the module-definition (.def) file.
///
///
/// The module can be a library module (a .dll file) or an executable module (an .exe file). If the specified module is an executable
/// module, static imports are not loaded; instead, the module is loaded as if DONT_RESOLVE_DLL_REFERENCES was specified. See
/// the dwFlags parameter for more information.
///
///
/// If the string specifies a module name without a path and the file name extension is omitted, the function appends the default
/// library extension .dll to the module name. To prevent the function from appending .dll to the module name, include a trailing
/// point character (.) in the module name string.
///
///
/// If the string specifies a fully qualified path, the function searches only that path for the module. When specifying a path, be
/// sure to use backslashes (\), not forward slashes (/). For more information about paths, see Naming Files, Paths, and Namespaces.
///
///
/// If the string specifies a module name without a path and more than one loaded module has the same base name and extension, the
/// function returns a handle to the module that was loaded first.
///
///
/// If the string specifies a module name without a path and a module of the same name is not already loaded, or if the string
/// specifies a module name with a relative path, the function searches for the specified module. The function also searches for
/// modules if loading the specified module causes the system to load other associated modules (that is, if the module has
/// dependencies). The directories that are searched and the order in which they are searched depend on the specified path and the
/// dwFlags parameter. For more information, see Remarks.
///
/// If the function cannot find the module or one of its dependencies, the function fails.
///
/// This parameter is reserved for future use. It must be NULL.
///
///
/// The action to be taken when loading the module. If no flags are specified, the behavior of this function is identical to that of
/// the LoadLibrary function. This parameter can be one of the following values.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// DONT_RESOLVE_DLL_REFERENCES0x00000001
///
/// If this value is used, and the executable module is a DLL, the system does not call DllMain for process and thread initialization
/// and termination. Also, the system does not load additional executable modules that are referenced by the specified module.
///
///
/// -
/// LOAD_IGNORE_CODE_AUTHZ_LEVEL0x00000010
///
/// If this value is used, the system does not check AppLocker rules or apply Software Restriction Policies for the DLL. This action
/// applies only to the DLL being loaded and not to its dependencies. This value is recommended for use in setup programs that must
/// run extracted DLLs during installation.Windows Server 2008 R2 and Windows 7: On systems with KB2532445 installed, the caller must
/// be running as "LocalSystem" or "TrustedInstaller"; otherwise the system ignores this flag.
/// For more information, see "You can circumvent AppLocker rules by using an Office macro on a computer that is running
/// Windows 7 or Windows Server 2008 R2" in the Help and Support Knowledge Base at
/// http://support.microsoft.com/kb/2532445.Windows Server 2008, Windows Vista, Windows Server 2003 and Windows XP: AppLocker was
/// introduced in Windows 7 and Windows Server 2008 R2.
///
///
/// -
/// LOAD_LIBRARY_AS_DATAFILE0x00000002
///
/// If this value is used, the system maps the file into the calling process's virtual address space as if it were a data file.
/// Nothing is done to execute or prepare to execute the mapped file. Therefore, you cannot call functions like GetModuleFileName,
/// GetModuleHandle or GetProcAddress with this DLL. Using this value causes writes to read-only memory to raise an access violation.
/// Use this flag when you want to load a DLL only to extract messages or resources from it.This value can be used with
/// LOAD_LIBRARY_AS_IMAGE_RESOURCE. For more information, see Remarks.
///
///
/// -
/// LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE0x00000040
///
/// Similar to LOAD_LIBRARY_AS_DATAFILE, except that the DLL file is opened with exclusive write access for the calling process.
/// Other processes cannot open the DLL file for write access while it is in use. However, the DLL can still be opened by other
/// processes.This value can be used with LOAD_LIBRARY_AS_IMAGE_RESOURCE. For more information, see Remarks.Windows Server 2003 and
/// Windows XP: This value is not supported until Windows Vista.
///
///
/// -
/// LOAD_LIBRARY_AS_IMAGE_RESOURCE0x00000020
///
/// If this value is used, the system maps the file into the process's virtual address space as an image file. However, the loader
/// does not load the static imports or perform the other usual initialization steps. Use this flag when you want to load a DLL only
/// to extract messages or resources from it.Unless the application depends on the file having the in-memory layout of an image, this
/// value should be used with either LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE or LOAD_LIBRARY_AS_DATAFILE. For more information, see the
/// Remarks section.Windows Server 2003 and Windows XP: This value is not supported until Windows Vista.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_APPLICATION_DIR0x00000200
///
/// If this value is used, the application's installation directory is searched for the DLL and its dependencies. Directories in the
/// standard search path are not searched. This value cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH.Windows 7, Windows Server
/// 2008 R2, Windows Vista and Windows Server 2008: This value requires KB2533623 to be installed.Windows Server 2003 and Windows XP:
/// This value is not supported.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_DEFAULT_DIRS0x00001000
///
/// This value is a combination of LOAD_LIBRARY_SEARCH_APPLICATION_DIR, LOAD_LIBRARY_SEARCH_SYSTEM32, and
/// LOAD_LIBRARY_SEARCH_USER_DIRS. Directories in the standard search path are not searched. This value cannot be combined with
/// LOAD_WITH_ALTERED_SEARCH_PATH.This value represents the recommended maximum number of directories an application should include
/// in its DLL search path.Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server 2008: This value requires KB2533623 to
/// be installed.Windows Server 2003 and Windows XP: This value is not supported.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR0x00000100
///
/// If this value is used, the directory that contains the DLL is temporarily added to the beginning of the list of directories that
/// are searched for the DLL's dependencies. Directories in the standard search path are not searched.The lpFileName parameter must
/// specify a fully qualified path. This value cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH.For example, if Lib2.dll is a
/// dependency of C:\Dir1\Lib1.dll, loading Lib1.dll with this value causes the system to search for Lib2.dll only in C:\Dir1. To
/// search for Lib2.dll in C:\Dir1 and all of the directories in the DLL search path, combine this value with
/// LOAD_LIBRARY_DEFAULT_DIRS.Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server 2008: This value requires KB2533623
/// to be installed.Windows Server 2003 and Windows XP: This value is not supported.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_SYSTEM320x00000800
///
/// If this value is used, %windows%\system32 is searched for the DLL and its dependencies. Directories in the standard search path
/// are not searched. This value cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH.Windows 7, Windows Server 2008 R2, Windows
/// Vista and Windows Server 2008: This value requires KB2533623 to be installed.Windows Server 2003 and Windows XP: This value is
/// not supported.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_USER_DIRS0x00000400
///
/// If this value is used, directories added using the AddDllDirectory or the SetDllDirectory function are searched for the DLL and
/// its dependencies. If more than one directory has been added, the order in which the directories are searched is unspecified.
/// Directories in the standard search path are not searched. This value cannot be combined with
/// LOAD_WITH_ALTERED_SEARCH_PATH.Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server
/// 2008: This value requires KB2533623 to be installed.Windows Server 2003 and Windows XP: This value is not supported.
///
///
/// -
/// LOAD_WITH_ALTERED_SEARCH_PATH0x00000008
///
/// If this value is used and lpFileName specifies an absolute path, the system uses the alternate file search strategy discussed in
/// the Remarks section to find associated executable modules that the specified module causes to be loaded. If this value is used
/// and lpFileName specifies a relative path, the behavior is undefined.If this value is not used, or if lpFileName does not specify
/// a path, the system uses the standard search strategy discussed in the Remarks section to find associated executable modules that
/// the specified module causes to be loaded.This value cannot be combined with any LOAD_LIBRARY_SEARCH flag.
///
///
///
///
///
///
/// If the function succeeds, the return value is a handle to the loaded module.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
// HMODULE WINAPI LoadLibraryEx( _In_ LPCTSTR lpFileName, _Reserved_ HANDLE hFile, _In_ DWORD dwFlags); https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx
[DllImport(Lib.Kernel32, CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
[PInvokeData("LibLoaderAPI.h", MSDNShortId = "ms684179")]
[SuppressUnmanagedCodeSecurity]
public static extern SafeHINSTANCE LoadLibraryEx(string lpFileName, [Optional] IntPtr hFile, LoadLibraryExFlags dwFlags);
///
/// Loads the specified module into the address space of the calling process. The specified module may cause other modules to be loaded.
///
///
///
/// A string that specifies the file name of the module to load. This name is not related to the name stored in a library module
/// itself, as specified by the LIBRARY keyword in the module-definition (.def) file.
///
///
/// The module can be a library module (a .dll file) or an executable module (an .exe file). If the specified module is an executable
/// module, static imports are not loaded; instead, the module is loaded as if DONT_RESOLVE_DLL_REFERENCES was specified. See
/// the dwFlags parameter for more information.
///
///
/// If the string specifies a module name without a path and the file name extension is omitted, the function appends the default
/// library extension .dll to the module name. To prevent the function from appending .dll to the module name, include a trailing
/// point character (.) in the module name string.
///
///
/// If the string specifies a fully qualified path, the function searches only that path for the module. When specifying a path, be
/// sure to use backslashes (\), not forward slashes (/). For more information about paths, see Naming Files, Paths, and Namespaces.
///
///
/// If the string specifies a module name without a path and more than one loaded module has the same base name and extension, the
/// function returns a handle to the module that was loaded first.
///
///
/// If the string specifies a module name without a path and a module of the same name is not already loaded, or if the string
/// specifies a module name with a relative path, the function searches for the specified module. The function also searches for
/// modules if loading the specified module causes the system to load other associated modules (that is, if the module has
/// dependencies). The directories that are searched and the order in which they are searched depend on the specified path and the
/// dwFlags parameter. For more information, see Remarks.
///
/// If the function cannot find the module or one of its dependencies, the function fails.
///
///
///
/// The action to be taken when loading the module. If no flags are specified, the behavior of this function is identical to that of
/// the LoadLibrary function. This parameter can be one of the following values.
///
///
///
///
/// Value
/// Meaning
///
/// -
/// DONT_RESOLVE_DLL_REFERENCES0x00000001
///
/// If this value is used, and the executable module is a DLL, the system does not call DllMain for process and thread initialization
/// and termination. Also, the system does not load additional executable modules that are referenced by the specified module.
///
///
/// -
/// LOAD_IGNORE_CODE_AUTHZ_LEVEL0x00000010
///
/// If this value is used, the system does not check AppLocker rules or apply Software Restriction Policies for the DLL. This action
/// applies only to the DLL being loaded and not to its dependencies. This value is recommended for use in setup programs that must
/// run extracted DLLs during installation.Windows Server 2008 R2 and Windows 7: On systems with KB2532445 installed, the caller must
/// be running as "LocalSystem" or "TrustedInstaller"; otherwise the system ignores this flag.
/// For more information, see "You can circumvent AppLocker rules by using an Office macro on a computer that is running
/// Windows 7 or Windows Server 2008 R2" in the Help and Support Knowledge Base at
/// http://support.microsoft.com/kb/2532445.Windows Server 2008, Windows Vista, Windows Server 2003 and Windows XP: AppLocker was
/// introduced in Windows 7 and Windows Server 2008 R2.
///
///
/// -
/// LOAD_LIBRARY_AS_DATAFILE0x00000002
///
/// If this value is used, the system maps the file into the calling process's virtual address space as if it were a data file.
/// Nothing is done to execute or prepare to execute the mapped file. Therefore, you cannot call functions like GetModuleFileName,
/// GetModuleHandle or GetProcAddress with this DLL. Using this value causes writes to read-only memory to raise an access violation.
/// Use this flag when you want to load a DLL only to extract messages or resources from it.This value can be used with
/// LOAD_LIBRARY_AS_IMAGE_RESOURCE. For more information, see Remarks.
///
///
/// -
/// LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE0x00000040
///
/// Similar to LOAD_LIBRARY_AS_DATAFILE, except that the DLL file is opened with exclusive write access for the calling process.
/// Other processes cannot open the DLL file for write access while it is in use. However, the DLL can still be opened by other
/// processes.This value can be used with LOAD_LIBRARY_AS_IMAGE_RESOURCE. For more information, see Remarks.Windows Server 2003 and
/// Windows XP: This value is not supported until Windows Vista.
///
///
/// -
/// LOAD_LIBRARY_AS_IMAGE_RESOURCE0x00000020
///
/// If this value is used, the system maps the file into the process's virtual address space as an image file. However, the loader
/// does not load the static imports or perform the other usual initialization steps. Use this flag when you want to load a DLL only
/// to extract messages or resources from it.Unless the application depends on the file having the in-memory layout of an image, this
/// value should be used with either LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE or LOAD_LIBRARY_AS_DATAFILE. For more information, see the
/// Remarks section.Windows Server 2003 and Windows XP: This value is not supported until Windows Vista.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_APPLICATION_DIR0x00000200
///
/// If this value is used, the application's installation directory is searched for the DLL and its dependencies. Directories in the
/// standard search path are not searched. This value cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH.Windows 7, Windows Server
/// 2008 R2, Windows Vista and Windows Server 2008: This value requires KB2533623 to be installed.Windows Server 2003 and Windows XP:
/// This value is not supported.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_DEFAULT_DIRS0x00001000
///
/// This value is a combination of LOAD_LIBRARY_SEARCH_APPLICATION_DIR, LOAD_LIBRARY_SEARCH_SYSTEM32, and
/// LOAD_LIBRARY_SEARCH_USER_DIRS. Directories in the standard search path are not searched. This value cannot be combined with
/// LOAD_WITH_ALTERED_SEARCH_PATH.This value represents the recommended maximum number of directories an application should include
/// in its DLL search path.Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server 2008: This value requires KB2533623 to
/// be installed.Windows Server 2003 and Windows XP: This value is not supported.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR0x00000100
///
/// If this value is used, the directory that contains the DLL is temporarily added to the beginning of the list of directories that
/// are searched for the DLL's dependencies. Directories in the standard search path are not searched.The lpFileName parameter must
/// specify a fully qualified path. This value cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH.For example, if Lib2.dll is a
/// dependency of C:\Dir1\Lib1.dll, loading Lib1.dll with this value causes the system to search for Lib2.dll only in C:\Dir1. To
/// search for Lib2.dll in C:\Dir1 and all of the directories in the DLL search path, combine this value with
/// LOAD_LIBRARY_DEFAULT_DIRS.Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server 2008: This value requires KB2533623
/// to be installed.Windows Server 2003 and Windows XP: This value is not supported.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_SYSTEM320x00000800
///
/// If this value is used, %windows%\system32 is searched for the DLL and its dependencies. Directories in the standard search path
/// are not searched. This value cannot be combined with LOAD_WITH_ALTERED_SEARCH_PATH.Windows 7, Windows Server 2008 R2, Windows
/// Vista and Windows Server 2008: This value requires KB2533623 to be installed.Windows Server 2003 and Windows XP: This value is
/// not supported.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_USER_DIRS0x00000400
///
/// If this value is used, directories added using the AddDllDirectory or the SetDllDirectory function are searched for the DLL and
/// its dependencies. If more than one directory has been added, the order in which the directories are searched is unspecified.
/// Directories in the standard search path are not searched. This value cannot be combined with
/// LOAD_WITH_ALTERED_SEARCH_PATH.Windows 7, Windows Server 2008 R2, Windows Vista and Windows Server
/// 2008: This value requires KB2533623 to be installed.Windows Server 2003 and Windows XP: This value is not supported.
///
///
/// -
/// LOAD_WITH_ALTERED_SEARCH_PATH0x00000008
///
/// If this value is used and lpFileName specifies an absolute path, the system uses the alternate file search strategy discussed in
/// the Remarks section to find associated executable modules that the specified module causes to be loaded. If this value is used
/// and lpFileName specifies a relative path, the behavior is undefined.If this value is not used, or if lpFileName does not specify
/// a path, the system uses the standard search strategy discussed in the Remarks section to find associated executable modules that
/// the specified module causes to be loaded.This value cannot be combined with any LOAD_LIBRARY_SEARCH flag.
///
///
///
///
///
///
/// If the function succeeds, the return value is a handle to the loaded module.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
// HMODULE WINAPI LoadLibraryEx( _In_ LPCTSTR lpFileName, _Reserved_ HANDLE hFile, _In_ DWORD dwFlags); https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx
[PInvokeData("LibLoaderAPI.h", MSDNShortId = "ms684179")]
[SuppressUnmanagedCodeSecurity]
public static SafeHINSTANCE LoadLibraryEx(string lpFileName, LoadLibraryExFlags dwFlags = 0) => LoadLibraryEx(lpFileName, IntPtr.Zero, dwFlags);
/// Retrieves a handle that can be used to obtain a pointer to the first byte of the specified resource in memory.
///
/// Type: HMODULE
///
/// A handle to the module whose executable file contains the resource. If hModule is NULL, the system loads the resource from
/// the module that was used to create the current process.
///
///
///
/// Type: HRSRC
/// A handle to the resource to be loaded. This handle is returned by the FindResource or FindResourceEx function.
///
///
/// Type: HGLOBAL
/// If the function succeeds, the return value is a handle to the data associated with the resource.
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
// HGLOBAL WINAPI LoadResource( _In_opt_ HMODULE hModule, _In_ HRSRC hResInfo); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648046(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Winbase.h", MSDNShortId = "ms648046")]
[SuppressUnmanagedCodeSecurity]
public static extern HRSRCDATA LoadResource(HINSTANCE hModule, HRSRC hResInfo);
/// Retrieves a pointer to the specified resource in memory.
///
/// Type: HGLOBAL
///
/// A handle to the resource to be accessed. The LoadResource function returns this handle. Note that this parameter is listed
/// as an HGLOBAL variable only for backward compatibility. Do not pass any value as a parameter other than a successful
/// return value from the LoadResource function.
///
///
///
/// Type: LPVOID
///
/// If the loaded resource is available, the return value is a pointer to the first byte of the resource; otherwise, it is NULL.
///
///
// LPVOID WINAPI LockResource( _In_ HGLOBAL hResData); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648047(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("Winbase.h", MSDNShortId = "ms648047")]
[SuppressUnmanagedCodeSecurity]
public static extern IntPtr LockResource(HRSRCDATA hResData);
/// Determines whether the specified function in a delay-loaded DLL is available on the system.
///
/// A handle to the calling module. Desktop applications can use the GetModuleHandle or GetModuleHandleEx function to get this
/// handle. Windows Store apps should set this parameter to static_cast<HMODULE>(&__ImageBase).
///
///
/// The file name of the delay-loaded DLL that exports the specified function. This parameter is case-insensitive.
///
/// Windows Store apps should specify API sets, rather than monolithic DLLs. For example, api-ms-win-core-memory-l1-1-1.dll, rather
/// than kernel32.dll.
///
///
/// The name of the function to query. This parameter is case-sensitive.
/// This parameter is reserved and must be zero (0).
///
/// TRUE if the specified function is available on the system. If the specified function is not available on the system, this
/// function returns FALSE. To get extended error information, call GetLastError.
///
///
///
/// A delay-loaded DLL is statically linked but not actually loaded into memory until the running application references a symbol
/// exported by that DLL. Applications often delay load DLLs that contain functions the application might call only rarely or not at
/// all, because the DLL is only loaded when it is needed instead of being loaded at application startup like other statically linked
/// DLLs. This helps improve application performance, especially during initialization. A delay-load DLL is specified at link time
/// with the /DELAYLOAD (Delay Load Import) linker option.
///
///
/// Applications that target multiple versions of Windows or multiple Windows device families also rely on delay-loaded DLLs to make
/// visible extra features when they are available.
///
///
/// A desktop application can use delayed loading as an alternative to runtime dynamic linking that uses LoadLibrary or LoadLibraryEx
/// to load a DLL and GetProcAddress to get a pointer to a function. A Windows Store app cannot use LoadLibrary or
/// LoadLibraryEx, so to get the benefits to runtime dynamic linking, a Windows Store app must use the delayed loading mechanism.
///
///
/// To check whether a function in a delay-loaded DLL is available on the system, the application calls
/// QueryOptionalDelayLoadedAPI with the specified function. If QueryOptionalDelayLoadedAPI succeeds, the application
/// can safely call the specified function.
///
/// Examples
///
/// The following example shows how to use QueryOptionalDelayLoadedAPI to determine whether the VirtualAllocEx function is
/// available on the system.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/libloaderapi2/nf-libloaderapi2-queryoptionaldelayloadedapi
// BOOL QueryOptionalDelayLoadedAPI( HMODULE hParentModule, LPCSTR lpDllName, LPCSTR lpProcName, DWORD Reserved );
[DllImport(Lib.KernelBase, SetLastError = true, ExactSpelling = true)]
[PInvokeData("libloaderapi2.h", MSDNShortId = "43690689-4372-48ae-ac6d-230250f05f7c")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool QueryOptionalDelayLoadedAPI(HINSTANCE hParentModule, [MarshalAs(UnmanagedType.LPStr)] string lpDllName, [MarshalAs(UnmanagedType.LPStr)] string lpProcName, uint Reserved = 0);
/// Removes a directory that was added to the process DLL search path by using AddDllDirectory.
/// The cookie returned by AddDllDirectory when the directory was added to the search path.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI RemoveDllDirectory( _In_ DLL_DIRECTORY_COOKIE Cookie); https://msdn.microsoft.com/en-us/library/windows/desktop/hh310514(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("LibLoaderAPI.h", MSDNShortId = "hh310514")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RemoveDllDirectory(IntPtr Cookie);
///
/// Specifies a default set of directories to search when the calling process loads a DLL. This search path is used when
/// LoadLibraryEx is called with no LOAD_LIBRARY_SEARCH flags.
///
///
/// The directories to search. This parameter can be any combination of the following values.
///
///
///
/// Value
/// Meaning
///
/// -
/// LOAD_LIBRARY_SEARCH_APPLICATION_DIR0x00000200
/// If this value is used, the application's installation directory is searched.
///
/// -
/// LOAD_LIBRARY_SEARCH_DEFAULT_DIRS0x00001000
///
/// This value is a combination of LOAD_LIBRARY_SEARCH_APPLICATION_DIR, LOAD_LIBRARY_SEARCH_SYSTEM32, and
/// LOAD_LIBRARY_SEARCH_USER_DIRS.This value represents the recommended maximum number of directories an application should include
/// in its DLL search path.
///
///
/// -
/// LOAD_LIBRARY_SEARCH_SYSTEM320x00000800
/// If this value is used, %windows%\system32 is searched.
///
/// -
/// LOAD_LIBRARY_SEARCH_USER_DIRS0x00000400
///
/// If this value is used, any path explicitly added using the AddDllDirectory or SetDllDirectory function is searched. If more than
/// one directory has been added, the order in which those directories are searched is unspecified.
///
///
///
///
///
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI SetDefaultDllDirectories( _In_ DWORD DirectoryFlags); https://msdn.microsoft.com/en-us/library/windows/desktop/hh310515(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("LibLoaderAPI.h", MSDNShortId = "hh310515")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDefaultDllDirectories(LoadLibraryExFlags DirectoryFlags);
/// Retrieves the size, in bytes, of the specified resource.
///
/// Type: HMODULE
/// A handle to the module whose executable file contains the resource.
///
///
/// Type: HRSRC
/// A handle to the resource. This handle must be created by using the FindResource or FindResourceEx function.
///
///
/// Type: DWORD
/// If the function succeeds, the return value is the number of bytes in the resource.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// DWORD WINAPI SizeofResource( _In_opt_ HMODULE hModule, _In_ HRSRC hResInfo); https://msdn.microsoft.com/en-us/library/windows/desktop/ms648048(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("Winbase.h", MSDNShortId = "ms648048")]
public static extern uint SizeofResource(HINSTANCE hModule, HRSRC hResInfo);
/// Provides a handle to a resource.
[StructLayout(LayoutKind.Sequential)]
public struct HRSRC : IHandle
{
private IntPtr handle;
/// Initializes a new instance of the struct.
/// An object that represents the pre-existing handle to use.
public HRSRC(IntPtr preexistingHandle) => handle = preexistingHandle;
/// Returns an invalid handle by instantiating a object with .
public static HRSRC NULL => new HRSRC(IntPtr.Zero);
/// Gets a value indicating whether this instance is a null handle.
public bool IsNull => handle == IntPtr.Zero;
/// Performs an explicit conversion from to .
/// The handle.
/// The result of the conversion.
public static explicit operator IntPtr(HRSRC h) => h.handle;
/// Performs an implicit conversion from to .
/// The pointer to a handle.
/// The result of the conversion.
public static implicit operator HRSRC(IntPtr h) => new HRSRC(h);
/// Implements the operator !=.
/// The first handle.
/// The second handle.
/// The result of the operator.
public static bool operator !=(HRSRC h1, HRSRC h2) => !(h1 == h2);
/// Implements the operator ==.
/// The first handle.
/// The second handle.
/// The result of the operator.
public static bool operator ==(HRSRC h1, HRSRC h2) => h1.Equals(h2);
///
public override bool Equals(object obj) => obj is HRSRC h ? handle == h.handle : false;
///
public override int GetHashCode() => handle.GetHashCode();
///
public IntPtr DangerousGetHandle() => handle;
}
/// Provides a handle to resource data.
[StructLayout(LayoutKind.Sequential)]
public struct HRSRCDATA : IHandle
{
private IntPtr handle;
/// Initializes a new instance of the struct.
/// An object that represents the pre-existing handle to use.
public HRSRCDATA(IntPtr preexistingHandle) => handle = preexistingHandle;
/// Returns an invalid handle by instantiating a object with .
public static HRSRCDATA NULL => new HRSRCDATA(IntPtr.Zero);
/// Gets a value indicating whether this instance is a null handle.
public bool IsNull => handle == IntPtr.Zero;
/// Performs an explicit conversion from to .
/// The handle.
/// The result of the conversion.
public static explicit operator IntPtr(HRSRCDATA h) => h.handle;
/// Performs an implicit conversion from to .
/// The pointer to a handle.
/// The result of the conversion.
public static implicit operator HRSRCDATA(IntPtr h) => new HRSRCDATA(h);
/// Implements the operator !=.
/// The first handle.
/// The second handle.
/// The result of the operator.
public static bool operator !=(HRSRCDATA h1, HRSRCDATA h2) => !(h1 == h2);
/// Implements the operator ==.
/// The first handle.
/// The second handle.
/// The result of the operator.
public static bool operator ==(HRSRCDATA h1, HRSRCDATA h2) => h1.Equals(h2);
///
public override bool Equals(object obj) => obj is HRSRCDATA h ? handle == h.handle : false;
///
public override int GetHashCode() => handle.GetHashCode();
///
public IntPtr DangerousGetHandle() => handle;
}
/// Provides a to a that releases a created HINSTANCE instance at disposal using FreeLibrary.
[PInvokeData("LibLoaderAPI.h")]
public class SafeHINSTANCE : SafeHANDLE
{
/// Initializes a new instance of the class and assigns an existing handle.
/// An object that represents the pre-existing handle to use.
///
/// to reliably release the handle during the finalization phase; otherwise, (not recommended).
///
public SafeHINSTANCE(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { }
private SafeHINSTANCE() : base() { }
///
/// Gets a value indicating whether the module was loaded as a data file (LOAD_LIBRARY_AS_DATAFILE or
/// LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE). Equivalent to LDR_IS_DATAFILE.
///
public bool IsDataFile => (handle.ToInt64() & 1) != 0;
///
/// Gets a value indicating whether the module was loaded as an image file (LOAD_LIBRARY_AS_IMAGE_RESOURCE). Equivalent to LDR_IS_IMAGEMAPPING.
///
public bool IsImageMapping => (handle.ToInt64() & 2) != 0;
///
/// Gets a value indicating whether the module was loaded as either a data file or an image file. Equivalent to LDR_IS_RESOURCE.
///
public bool IsResource => (handle.ToInt64() & 3) != 0;
/// Performs an implicit conversion from to .
/// The safe handle instance.
/// The result of the conversion.
public static implicit operator HINSTANCE(SafeHINSTANCE h) => h.handle;
/// Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
/// The function or variable name.
///
/// If the function succeeds, the return value is the address of the exported function or variable.
/// If the function fails, an exception with the error is thrown.
///
public IntPtr GetProcAddress(string procName) => Win32Error.ThrowLastErrorIfNull(Kernel32.GetProcAddress(handle, procName));
/// Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
/// The function's ordinal value.
///
/// If the function succeeds, the return value is the address of the exported function or variable.
/// If the function fails, an exception with the error is thrown.
///
public IntPtr GetProcAddress(int ordinal) => GetProcAddress($"#{ordinal}");
/// Retrieves a delegate for an exported function or variable from the specified dynamic-link library (DLL).
/// The delegate type to which to convert that function pointer.
/// The function or variable name.
///
/// If the function succeeds, the return value is the exported function or variable's delegate.
/// If the function fails, an exception with the error is thrown.
///
public T GetProcAddress(string procName) where T : Delegate
{
var ptr = Win32Error.ThrowLastErrorIfNull(Kernel32.GetProcAddress(handle, procName));
return (T)Marshal.GetDelegateForFunctionPointer(ptr, typeof(T));
}
/// Retrieves a delegate for an exported function or variable from the specified dynamic-link library (DLL).
/// The delegate type to which to convert that function pointer.
/// The function's ordinal value.
///
/// If the function succeeds, the return value is the exported function or variable's delegate.
/// If the function fails, an exception with the error is thrown.
///
public T GetProcAddress(int ordinal) where T : Delegate => GetProcAddress($"#{ordinal}");
///
protected override bool InternalReleaseHandle() => FreeLibrary(this);
}
}
}