Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
FindExecutable.cs50 linesDownload Raw Back to Windows
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#nullable enable5 6#if !UNIX7using System;8using System.Runtime.InteropServices;9 10internal static partial class Interop11{12    internal static unsafe partial class Windows13    {14        // The FindExecutable API is defined in shellapi.h as15        // SHSTDAPI_(HINSTANCE) FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, __out_ecount(MAX_PATH) LPWSTR lpResult);16        // HINSTANCE is void* so we need to use IntPtr (nint) as API return value.17        [LibraryImport("shell32.dll", EntryPoint = "FindExecutableW", StringMarshalling = StringMarshalling.Utf16)]18        internal static partial nint FindExecutableW(string fileName, string directoryPath, char* pathFound);19 20        internal static string? FindExecutable(string filename)21        {22            string? result = null;23 24            // HINSTANCE == PVOID == nint25            nint resultCode = 0;26 27            Span<char> buffer = stackalloc char[MAX_PATH];28            unsafe29            {30                fixed (char* lpBuffer = buffer)31                {32                    resultCode = FindExecutableW(filename, string.Empty, lpBuffer);33 34                    // If FindExecutable returns a result > 32, then it succeeded35                    // and we return the string that was found, otherwise we36                    // return null.37                    if (resultCode > 32)38                    {39                        result = Marshal.PtrToStringUni((IntPtr)lpBuffer);40                        return result;41                    }42                }43            }44 45            return null;46        }47    }48}49#endif50