1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public static class DllLocator
{
[DllImport("kernel32.dll",
SetLastError = true,
CharSet = CharSet.Auto)]
private static extern uint SearchPath(string lpPath,
string lpFileName,
string lpExtension,
int nBufferLength,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder lpBuffer,
out IntPtr lpFilePart);
static StringBuilder commonBuffer = new StringBuilder(16);
/// <summary>
///
/// </summary>
/// <param name="dllName"></param>
/// <returns>Le chemin complet si elle a été trouvée, null ou "" sinon</returns>
public static string LocateDll(string dllName)
{
if (String.IsNullOrEmpty(dllName))
return null;
lock (commonBuffer)
{
IntPtr dummy;
uint dataSize = 0;
begin:
dataSize = SearchPath(null, dllName, ".dll", commonBuffer.Capacity, commonBuffer, out dummy);
if (dataSize > 0)
{
if (dataSize > commonBuffer.Capacity)
{
/* pas assez de buffer */
commonBuffer.Capacity = (int)(dataSize + 1);
goto begin;
}
return commonBuffer.ToString();
}
else
return null;
}
}
} |
Partager