Skip to content

Commit a395936

Browse files
committed
Replace FormApplication & DllImport with CSWin32
1 parent 1f81ea0 commit a395936

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

Plugins/Flow.Launcher.Plugin.Sys/Flow.Launcher.Plugin.Sys.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,11 @@
5757
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5858
</None>
5959
</ItemGroup>
60+
61+
<ItemGroup>
62+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
63+
<PrivateAssets>all</PrivateAssets>
64+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
65+
</PackageReference>
66+
</ItemGroup>
6067
</Project>

Plugins/Flow.Launcher.Plugin.Sys/Main.cs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5-
using System.Runtime.InteropServices;
65
using System.Windows;
7-
using System.Windows.Forms;
8-
using System.Windows.Interop;
96
using Flow.Launcher.Infrastructure;
107
using Flow.Launcher.Infrastructure.Logger;
118
using Flow.Launcher.Infrastructure.UserSettings;
129
using Flow.Launcher.Plugin.SharedCommands;
10+
using Windows.Win32;
11+
using Windows.Win32.Foundation;
12+
using Windows.Win32.System.Shutdown;
1313
using Application = System.Windows.Application;
1414
using Control = System.Windows.Controls.Control;
15-
using FormsApplication = System.Windows.Forms.Application;
1615

1716
namespace Flow.Launcher.Plugin.Sys
1817
{
@@ -21,33 +20,6 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n
2120
private PluginInitContext context;
2221
private Dictionary<string, string> KeywordTitleMappings = new Dictionary<string, string>();
2322

24-
#region DllImport
25-
26-
internal const int EWX_LOGOFF = 0x00000000;
27-
internal const int EWX_SHUTDOWN = 0x00000001;
28-
internal const int EWX_REBOOT = 0x00000002;
29-
internal const int EWX_FORCE = 0x00000004;
30-
internal const int EWX_POWEROFF = 0x00000008;
31-
internal const int EWX_FORCEIFHUNG = 0x00000010;
32-
33-
[DllImport("user32")]
34-
private static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
35-
36-
[DllImport("user32")]
37-
private static extern void LockWorkStation();
38-
39-
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
40-
private static extern uint SHEmptyRecycleBin(IntPtr hWnd, uint dwFlags);
41-
42-
// http://www.pinvoke.net/default.aspx/Enums/HRESULT.html
43-
private enum HRESULT : uint
44-
{
45-
S_FALSE = 0x0001,
46-
S_OK = 0x0000
47-
}
48-
49-
#endregion
50-
5123
public Control CreateSettingPanel()
5224
{
5325
var results = Commands();
@@ -206,7 +178,7 @@ private List<Result> Commands()
206178
MessageBoxButton.YesNo, MessageBoxImage.Warning);
207179

208180
if (result == MessageBoxResult.Yes)
209-
ExitWindowsEx(EWX_LOGOFF, 0);
181+
PInvoke.ExitWindowsEx(EXIT_WINDOWS_FLAGS.EWX_LOGOFF, 0);
210182

211183
return true;
212184
}
@@ -219,7 +191,7 @@ private List<Result> Commands()
219191
IcoPath = "Images\\lock.png",
220192
Action = c =>
221193
{
222-
LockWorkStation();
194+
PInvoke.LockWorkStation();
223195
return true;
224196
}
225197
},
@@ -229,7 +201,7 @@ private List<Result> Commands()
229201
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_sleep"),
230202
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xec46"),
231203
IcoPath = "Images\\sleep.png",
232-
Action = c => FormsApplication.SetSuspendState(PowerState.Suspend, false, false)
204+
Action = c => PInvoke.SetSuspendState(false, false, false)
233205
},
234206
new Result
235207
{
@@ -274,8 +246,8 @@ private List<Result> Commands()
274246
// http://www.pinvoke.net/default.aspx/shell32/SHEmptyRecycleBin.html
275247
// FYI, couldn't find documentation for this but if the recycle bin is already empty, it will return -2147418113 (0x8000FFFF (E_UNEXPECTED))
276248
// 0 for nothing
277-
var result = SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0);
278-
if (result != (uint) HRESULT.S_OK && result != (uint) 0x8000FFFF)
249+
var result = PInvoke.SHEmptyRecycleBin(new(), string.Empty, 0);
250+
if (result != HRESULT.S_OK && result != HRESULT.E_UNEXPECTED)
279251
{
280252
context.API.ShowMsgBox($"Error emptying recycle bin, error code: {result}\n" +
281253
"please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ExitWindowsEx
2+
LockWorkStation
3+
SHEmptyRecycleBin
4+
S_OK
5+
E_UNEXPECTED
6+
SetSuspendState

0 commit comments

Comments
 (0)