using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Vanara.PInvoke
{
public static partial class Kernel32
{
/// Retrieves the application-specific portion of the search path used to locate DLLs for the application.
/// The size of the output buffer, in characters.
/// A pointer to a buffer that receives the application-specific portion of the search path.
///
///
/// If the function succeeds, the return value is the length of the string copied to lpBuffer, in characters, not including the terminating null
/// character. If the return value is greater than nBufferLength, it specifies the size of the buffer required for the path.
///
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// DWORD WINAPI GetDllDirectory( _In_ DWORD nBufferLength, _Out_ LPTSTR lpBuffer); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683186(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("WinBase.h", MSDNShortId = "ms683186")]
public static extern uint GetDllDirectory(uint nBufferLength, StringBuilder lpBuffer);
/// Retrieves the application-specific portion of the search path used to locate DLLs for the application.
/// The application-specific portion of the search path.
public static string GetDllDirectory() => FunctionHelper.CallMethodWithStrBuf((StringBuilder sb, ref uint sz) => GetDllDirectory(sz, sb), out var str, (sz, r) => r <= sz) > 0 ? str : throw Win32Error.GetLastError().GetException();
/// Loads and executes an application or creates a new instance of an existing application.
///
/// The file name of the application to be run. When specifying a path, be sure to use backslashes (\), not forward slashes (/). If the lpModuleName
/// parameter does not contain a directory path, the system searches for the executable file in this order:
///
///
/// A pointer to an application-defined LOADPARMS32 structure that defines the new application's parameter block. Set all unused members to NULL,
/// except for lpCmdLine, which must point to a null-terminated string if it is not used. For more information, see Remarks.
///
///
/// If the function succeeds, the return value is greater than 31.
/// If the function fails, the return value is an error value, which may be one of the following values.
///
///
///
/// Return code/value
/// Description
///
/// -
/// 0
/// The system is out of memory or resources.
///
/// -
/// ERROR_BAD_FORMAT11L
/// The .exe file is invalid.
///
/// -
/// ERROR_FILE_NOT_FOUND2L
/// The specified file was not found.
///
/// -
/// ERROR_PATH_NOT_FOUND3L
/// The specified path was not found.
///
///
///
///
// DWORD WINAPI LoadModule( _In_ LPCSTR lpModuleName, _In_ LPVOID lpParameterBlock); https://msdn.microsoft.com/en-us/library/windows/desktop/ms684183(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Ansi)]
[PInvokeData("Winbase.h", MSDNShortId = "ms684183")]
public static extern Win32Error LoadModule(string lpModuleName, [In] IntPtr lpParameterBlock);
/// Adds a directory to the search path used to locate DLLs for the application.
///
/// The directory to be added to the search path. If this parameter is an empty string (""), the call removes the current directory from the default DLL
/// search order. If this parameter is NULL, the function restores the default search order.
///
///
/// 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 SetDllDirectory( _In_opt_ LPCTSTR lpPathName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686203(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms686203")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDllDirectory(string lpPathName);
}
}