Replace DllImport with CSWin32

This commit is contained in:
Jack251970 2024-12-10 15:24:19 +08:00
parent ccfc39e54a
commit 79f8f053d9
3 changed files with 32 additions and 12 deletions

View file

@ -57,7 +57,11 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Include="Readme.md" Pack="true" PackagePath="\"/> <AdditionalFiles Include="NativeMethods.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Readme.md" Pack="true" PackagePath="\" />
<None Include="FodyWeavers.xml" /> <None Include="FodyWeavers.xml" />
</ItemGroup> </ItemGroup>
@ -68,6 +72,10 @@
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" /> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" /> <PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" /> <PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
</ItemGroup> </ItemGroup>

View file

@ -0,0 +1,3 @@
EnumThreadWindows
GetWindowText
GetWindowTextLength

View file

@ -2,18 +2,15 @@
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading; using System.Threading;
using Windows.Win32;
using Windows.Win32.Foundation;
namespace Flow.Launcher.Plugin.SharedCommands namespace Flow.Launcher.Plugin.SharedCommands
{ {
public static class ShellCommand public static class ShellCommand
{ {
public delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam); public delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll")] static extern bool EnumThreadWindows(uint threadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll")] static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr hwnd);
private static bool containsSecurityWindow; private static bool containsSecurityWindow;
@ -42,21 +39,33 @@ namespace Flow.Launcher.Plugin.SharedCommands
{ {
ProcessThreadCollection ptc = Process.GetCurrentProcess().Threads; ProcessThreadCollection ptc = Process.GetCurrentProcess().Threads;
for (int i = 0; i < ptc.Count; i++) for (int i = 0; i < ptc.Count; i++)
EnumThreadWindows((uint)ptc[i].Id, CheckSecurityThread, IntPtr.Zero); PInvoke.EnumThreadWindows((uint)ptc[i].Id, CheckSecurityThread, IntPtr.Zero);
} }
private static bool CheckSecurityThread(IntPtr hwnd, IntPtr lParam) private static BOOL CheckSecurityThread(HWND hwnd, LPARAM lParam)
{ {
if (GetWindowTitle(hwnd) == "Windows Security") if (GetWindowTitle(hwnd) == "Windows Security")
containsSecurityWindow = true; containsSecurityWindow = true;
return true; return true;
} }
private static string GetWindowTitle(IntPtr hwnd) private static unsafe string GetWindowTitle(HWND hwnd)
{ {
StringBuilder sb = new StringBuilder(GetWindowTextLength(hwnd) + 1); var capacity = PInvoke.GetWindowTextLength(hwnd) + 1;
GetWindowText(hwnd, sb, sb.Capacity); char[] buffer = new char[capacity];
return sb.ToString(); fixed (char* pBuffer = buffer)
{
// If the window has no title bar or text, if the title bar is empty,
// or if the window or control handle is invalid, the return value is zero.
if (PInvoke.GetWindowText(hwnd, (PWSTR)pBuffer, capacity) == 0)
{
return string.Empty;
}
int validLength = Array.IndexOf(buffer, '\0');
if (validLength < 0) validLength = capacity;
return new string(buffer, 0, validLength);
}
} }
public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "", bool createNoWindow = false) public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "", bool createNoWindow = false)