Skip to content

Commit 5304190

Browse files
committed
Use LibraryImport for native APIs
1 parent f2d929f commit 5304190

3 files changed

Lines changed: 12 additions & 9 deletions

File tree

v2rayN/v2rayN/Common/WindowsUtils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace v2rayN.Common;
77

8-
internal static class WindowsUtils
8+
internal static partial class WindowsUtils
99
{
1010
private static readonly string _tag = "WindowsUtils";
1111

@@ -39,8 +39,8 @@ public static void SetClipboardData(string strData)
3939
}
4040
}
4141

42-
[DllImport("dwmapi.dll")]
43-
public static extern int DwmSetWindowAttribute(nint hwnd, DWMWINDOWATTRIBUTE attribute, ref int attributeValue, uint attributeSize);
42+
[LibraryImport("dwmapi.dll")]
43+
public static partial int DwmSetWindowAttribute(nint hwnd, DWMWINDOWATTRIBUTE attribute, ref int attributeValue, uint attributeSize);
4444

4545
public static ImageSource IconToImageSource(Icon icon)
4646
{

v2rayN/v2rayN/Manager/HotkeyManager.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace v2rayN.Manager;
22

3-
public sealed class HotkeyManager
3+
public sealed partial class HotkeyManager
44
{
55
private static readonly Lazy<HotkeyManager> _instance = new(() => new());
66
public static HotkeyManager Instance = _instance.Value;
@@ -165,11 +165,13 @@ private void OnThreadPreProcessMessage(ref MSG msg, ref bool handled)
165165
}
166166
}
167167

168-
[DllImport("user32.dll", SetLastError = true)]
169-
private static extern bool RegisterHotKey(nint hWnd, int id, int fsModifiers, int vlc);
168+
[LibraryImport("user32.dll", SetLastError = true)]
169+
[return: MarshalAs(UnmanagedType.Bool)]
170+
private static partial bool RegisterHotKey(nint hWnd, int id, int fsModifiers, int vlc);
170171

171-
[DllImport("user32.dll", SetLastError = true)]
172-
private static extern bool UnregisterHotKey(nint hWnd, int id);
172+
[LibraryImport("user32.dll", SetLastError = true)]
173+
[return: MarshalAs(UnmanagedType.Bool)]
174+
private static partial bool UnregisterHotKey(nint hWnd, int id);
173175

174176
[Flags]
175177
private enum KeyModifiers

v2rayN/v2rayN/v2rayN.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ApplicationIcon>Resources\v2rayN.ico</ApplicationIcon>
99
<ApplicationManifest>app.manifest</ApplicationManifest>
1010
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
11+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1112
</PropertyGroup>
1213

1314
<ItemGroup>
@@ -35,4 +36,4 @@
3536
<ProjectReference Include="..\ServiceLib\ServiceLib.csproj" />
3637
</ItemGroup>
3738

38-
</Project>
39+
</Project>

0 commit comments

Comments
 (0)