Skip to content

Commit 79f8f05

Browse files
committed
Replace DllImport with CSWin32
1 parent ccfc39e commit 79f8f05

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

Flow.Launcher.Plugin/Flow.Launcher.Plugin.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@
5757
</PropertyGroup>
5858

5959
<ItemGroup>
60-
<None Include="Readme.md" Pack="true" PackagePath="\"/>
60+
<AdditionalFiles Include="NativeMethods.txt" />
61+
</ItemGroup>
62+
63+
<ItemGroup>
64+
<None Include="Readme.md" Pack="true" PackagePath="\" />
6165
<None Include="FodyWeavers.xml" />
6266
</ItemGroup>
6367

@@ -68,6 +72,10 @@
6872
</PackageReference>
6973
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
7074
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
75+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
76+
<PrivateAssets>all</PrivateAssets>
77+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
78+
</PackageReference>
7179
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
7280
</ItemGroup>
7381

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
EnumThreadWindows
2+
GetWindowText
3+
GetWindowTextLength

Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
using System.ComponentModel;
33
using System.Diagnostics;
44
using System.IO;
5-
using System.Runtime.InteropServices;
6-
using System.Text;
75
using System.Threading;
6+
using Windows.Win32;
7+
using Windows.Win32.Foundation;
88

99
namespace Flow.Launcher.Plugin.SharedCommands
1010
{
1111
public static class ShellCommand
1212
{
1313
public delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);
14-
[DllImport("user32.dll")] static extern bool EnumThreadWindows(uint threadId, EnumThreadDelegate lpfn, IntPtr lParam);
15-
[DllImport("user32.dll")] static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
16-
[DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr hwnd);
1714

1815
private static bool containsSecurityWindow;
1916

@@ -42,21 +39,33 @@ private static void CheckSecurityWindow()
4239
{
4340
ProcessThreadCollection ptc = Process.GetCurrentProcess().Threads;
4441
for (int i = 0; i < ptc.Count; i++)
45-
EnumThreadWindows((uint)ptc[i].Id, CheckSecurityThread, IntPtr.Zero);
42+
PInvoke.EnumThreadWindows((uint)ptc[i].Id, CheckSecurityThread, IntPtr.Zero);
4643
}
4744

48-
private static bool CheckSecurityThread(IntPtr hwnd, IntPtr lParam)
45+
private static BOOL CheckSecurityThread(HWND hwnd, LPARAM lParam)
4946
{
5047
if (GetWindowTitle(hwnd) == "Windows Security")
5148
containsSecurityWindow = true;
5249
return true;
5350
}
5451

55-
private static string GetWindowTitle(IntPtr hwnd)
52+
private static unsafe string GetWindowTitle(HWND hwnd)
5653
{
57-
StringBuilder sb = new StringBuilder(GetWindowTextLength(hwnd) + 1);
58-
GetWindowText(hwnd, sb, sb.Capacity);
59-
return sb.ToString();
54+
var capacity = PInvoke.GetWindowTextLength(hwnd) + 1;
55+
char[] buffer = new char[capacity];
56+
fixed (char* pBuffer = buffer)
57+
{
58+
// If the window has no title bar or text, if the title bar is empty,
59+
// or if the window or control handle is invalid, the return value is zero.
60+
if (PInvoke.GetWindowText(hwnd, (PWSTR)pBuffer, capacity) == 0)
61+
{
62+
return string.Empty;
63+
}
64+
65+
int validLength = Array.IndexOf(buffer, '\0');
66+
if (validLength < 0) validLength = capacity;
67+
return new string(buffer, 0, validLength);
68+
}
6069
}
6170

6271
public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "", bool createNoWindow = false)

0 commit comments

Comments
 (0)