File size: 1,656 Bytes
8c763fb | 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 48 49 50 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
#if !UNIX
using System;
using System.Runtime.InteropServices;
internal static partial class Interop
{
internal static unsafe partial class Windows
{
// The FindExecutable API is defined in shellapi.h as
// SHSTDAPI_(HINSTANCE) FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, __out_ecount(MAX_PATH) LPWSTR lpResult);
// HINSTANCE is void* so we need to use IntPtr (nint) as API return value.
[LibraryImport("shell32.dll", EntryPoint = "FindExecutableW", StringMarshalling = StringMarshalling.Utf16)]
internal static partial nint FindExecutableW(string fileName, string directoryPath, char* pathFound);
internal static string? FindExecutable(string filename)
{
string? result = null;
// HINSTANCE == PVOID == nint
nint resultCode = 0;
Span<char> buffer = stackalloc char[MAX_PATH];
unsafe
{
fixed (char* lpBuffer = buffer)
{
resultCode = FindExecutableW(filename, string.Empty, lpBuffer);
// If FindExecutable returns a result > 32, then it succeeded
// and we return the string that was found, otherwise we
// return null.
if (resultCode > 32)
{
result = Marshal.PtrToStringUni((IntPtr)lpBuffer);
return result;
}
}
}
return null;
}
}
}
#endif
|