You are here: Forum Home → ANT Developers Forums → ANT General Questions → Thread
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetDllDirectory(string lpPathName);
public static void checkUnmanagedLibrary()
{
// Check the unmanaged wrapper is present
// if (!System.IO.File.Exists(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ANT_UNMANAGED_WRAPPER)))
// OMB
if (!ExistsInSearchPath(ANT_UNMANAGED_WRAPPER))
throw new ANT_Exception(ANT_UNMANAGED_WRAPPER + " not found in working directory");
}
// see http://msdn.microsoft.com/en-us/library/ms682586.aspx
// If SafeDllSearchMode is enabled, the search order is as follows:
//
// 1. The directory from which the application loaded.
// 2. The system directory. Use the GetSystemDirectory function to get the path of this directory.
// 3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
// 4. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
// 5. The current directory.
// 6. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
//
// If SafeDllSearchMode is disabled, the search order is as follows:
//
// 1. The directory from which the application loaded.
// 2. The current directory.
// 3. The system directory. Use the GetSystemDirectory function to get the path of this directory.
// 4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
// 5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
// 6. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
//
[DllImport("Kernel32.dll")]
public static extern uint GetDllDirectory(uint length, StringBuilder buffer);
private static IList<String> MyDLLSearchPath = null;
private static IList<String> DLLSearchPath
{
get
{
if (MyDLLSearchPath == null)
{
MyDLLSearchPath = new List<String>();
MyDLLSearchPath.Add(AppDomain.CurrentDomain.BaseDirectory);
MyDLLSearchPath.Add(Environment.GetFolderPath(Environment.SpecialFolder.System));
MyDLLSearchPath.Add(Directory.GetCurrentDirectory());
const int sbCapacity = 512;
StringBuilder sb = new StringBuilder(sbCapacity);
GetDllDirectory(sbCapacity, sb);
if (sb.Length > 0)
MyDLLSearchPath.Add(sb.ToString());
}
return MyDLLSearchPath;
}
}
private static bool ExistsInSearchPath(string fileName)
{
foreach (String folderName in DLLSearchPath)
if (File.Exists(Path.Combine(folderName, fileName)))
return true;
return false;
}