add required win32 apis

This commit is contained in:
Lightczx
2024-01-25 15:50:46 +08:00
parent 948ec9a822
commit a1c604e68a
24 changed files with 255 additions and 32 deletions

View File

@@ -27,18 +27,4 @@ internal static class StringExtension
{
return source.AsSpan().TrimEnd(value).ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EqualsAny(this string source, in ReadOnlySpan<string> values, StringComparison comparisonType)
{
foreach (ref readonly string value in values)
{
if (source.Equals(value, comparisonType))
{
return true;
}
}
return false;
}
}

View File

@@ -49,7 +49,7 @@ internal static class CookieExtension
Must.Range(keys.Length > 0, "Empty keys is not supported");
SortedDictionary<string, string> cookieMap = [];
foreach (string key in keys)
foreach (ref readonly string key in keys)
{
if (source.TryGetValue(key, out string? value))
{

View File

@@ -214,7 +214,7 @@ internal sealed partial class GameRecordClient : IGameRecordClient
.ConfigureAwait(false);
// We have a verification procedure to handle
if (resp?.ReturnCode == (int)KnownReturnCode.CODE1034)
if (resp?.ReturnCode is (int)KnownReturnCode.CODE1034)
{
// Replace message
resp.Message = SH.WebIndexOrSpiralAbyssVerificationFailed;

View File

@@ -3,6 +3,7 @@
using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.Registry;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@@ -22,8 +23,9 @@ internal static class AdvApi32
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
[SupportedOSPlatform("windows5.0")]
public static unsafe extern WIN32_ERROR RegOpenKeyExW(HKEY hKey, [AllowNull] PCWSTR lpSubKey, [AllowNull] uint ulOptions, REG_SAM_FLAGS samDesired, [Out] HKEY* phkResult);
public static unsafe extern WIN32_ERROR RegOpenKeyExW(HKEY hKey, [AllowNull] PCWSTR lpSubKey, [AllowNull] uint ulOptions, REG_SAM_FLAGS samDesired, HKEY* phkResult);
[DebuggerStepThrough]
public static unsafe WIN32_ERROR RegOpenKeyExW(HKEY hKey, ReadOnlySpan<char> subKey, uint ulOptions, REG_SAM_FLAGS samDesired, out HKEY hkResult)
{
fixed (char* lpSubKey = subKey)

View File

@@ -0,0 +1,75 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.NetworkManagement.WindowsFirewall;
using Snap.Hutao.Win32.Security;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace Snap.Hutao.Win32;
[SuppressMessage("", "SH002")]
[SuppressMessage("", "SA1313")]
[SuppressMessage("", "SYSLIB1054")]
internal static class ApiMsWinNetIsolation
{
[DllImport("api-ms-win-net-isolation-l1-1-0.dll", ExactSpelling = true)]
[SupportedOSPlatform("windows8.0")]
public static unsafe extern uint NetworkIsolationEnumAppContainers(uint Flags, uint* pdwNumPublicAppCs, INET_FIREWALL_APP_CONTAINER** ppPublicAppCs);
[DebuggerStepThrough]
public static unsafe uint NetworkIsolationEnumAppContainers(NETISO_FLAG Flags, out uint dwNumPublicAppCs, out INET_FIREWALL_APP_CONTAINER* pPublicAppCs)
{
fixed (uint* pdwNumPublicAppCs = &dwNumPublicAppCs)
{
fixed (INET_FIREWALL_APP_CONTAINER** ppPublicAppCs = &pPublicAppCs)
{
return NetworkIsolationEnumAppContainers((uint)Flags, pdwNumPublicAppCs, ppPublicAppCs);
}
}
}
[DllImport("api-ms-win-net-isolation-l1-1-0.dll", ExactSpelling = true)]
[SupportedOSPlatform("windows8.0")]
public static unsafe extern uint NetworkIsolationFreeAppContainers(INET_FIREWALL_APP_CONTAINER* pPublicAppCs);
[DebuggerStepThrough]
public static unsafe uint NetworkIsolationFreeAppContainers(ref readonly INET_FIREWALL_APP_CONTAINER publicAppCs)
{
fixed (INET_FIREWALL_APP_CONTAINER* pPublicAppCs = &publicAppCs)
{
return NetworkIsolationFreeAppContainers(pPublicAppCs);
}
}
[DllImport("api-ms-win-net-isolation-l1-1-0.dll", ExactSpelling = true)]
[SupportedOSPlatform("windows8.0")]
public static unsafe extern uint NetworkIsolationGetAppContainerConfig(uint* pdwNumPublicAppCs, SID_AND_ATTRIBUTES** appContainerSids);
[DebuggerStepThrough]
public static unsafe uint NetworkIsolationGetAppContainerConfig(out uint dwNumPublicAppCs, out SID_AND_ATTRIBUTES* appContainerSids)
{
fixed (uint* pdwNumPublicAppCs = &dwNumPublicAppCs)
{
fixed (SID_AND_ATTRIBUTES** pAppContainerSids = &appContainerSids)
{
return NetworkIsolationGetAppContainerConfig(pdwNumPublicAppCs, pAppContainerSids);
}
}
}
[DllImport("api-ms-win-net-isolation-l1-1-0.dll", ExactSpelling = true)]
[SupportedOSPlatform("windows8.0")]
public static unsafe extern uint NetworkIsolationSetAppContainerConfig(uint dwNumPublicAppCs, SID_AND_ATTRIBUTES* appContainerSids);
[DebuggerStepThrough]
public static unsafe uint NetworkIsolationSetAppContainerConfig(ReadOnlySpan<SID_AND_ATTRIBUTES> appContainerSids)
{
fixed (SID_AND_ATTRIBUTES* pAppContainerSids = appContainerSids)
{
return NetworkIsolationSetAppContainerConfig((uint)appContainerSids.Length, pAppContainerSids);
}
}
}

View File

@@ -3,6 +3,7 @@
using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.Graphics.Dwm;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@@ -16,6 +17,7 @@ internal static class DwmApi
[SupportedOSPlatform("windows6.0.6000")]
public static unsafe extern HRESULT DwmSetWindowAttribute(HWND hwnd, uint dwAttribute, void* pvAttribute, uint cbAttribute);
[DebuggerStepThrough]
public static unsafe HRESULT DwmSetWindowAttribute<T>(HWND hwnd, DWMWINDOWATTRIBUTE dwAttribute, ref readonly T attribute)
where T : unmanaged
{

View File

@@ -0,0 +1,10 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Win32.Foundation;
internal unsafe struct FlexibleArray<T>
where T : unmanaged
{
public T* Reference;
}

View File

@@ -0,0 +1,9 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Win32.Foundation;
internal struct PSID
{
public unsafe void* Value;
}

View File

@@ -2,6 +2,7 @@
// Licensed under the MIT license.
using Snap.Hutao.Win32.Graphics.Gdi;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@@ -15,6 +16,7 @@ internal static class Gdi32
[SupportedOSPlatform("windows5.0")]
public static extern int GetDeviceCaps([AllowNull] HDC hdc, int index);
[DebuggerStepThrough]
public static int GetDeviceCaps([AllowNull] HDC hdc, GET_DEVICE_CAPS_INDEX index)
{
return GetDeviceCaps(hdc, (int)index);

View File

@@ -5,6 +5,7 @@ using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.Security;
using Snap.Hutao.Win32.System.Console;
using Snap.Hutao.Win32.System.ProcessStatus;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@@ -25,6 +26,18 @@ internal static class Kernel32
[SupportedOSPlatform("windows5.1.2600")]
public static unsafe extern HANDLE CreateEventW([AllowNull] SECURITY_ATTRIBUTES* lpEventAttributes, BOOL bManualReset, BOOL bInitialState, [AllowNull] PCWSTR lpName);
[DebuggerStepThrough]
public static unsafe HANDLE CreateEventW(ref readonly SECURITY_ATTRIBUTES eventAttributes, BOOL bManualReset, BOOL bInitialState, [AllowNull] ReadOnlySpan<char> name)
{
fixed (SECURITY_ATTRIBUTES* lpEventAttributes = &eventAttributes)
{
fixed (char* lpName = name)
{
return CreateEventW(lpEventAttributes, bManualReset, bInitialState, lpName);
}
}
}
[DllImport("KERNEL32.dll", ExactSpelling = true, SetLastError = true)]
public static extern BOOL FreeConsole();
@@ -32,6 +45,7 @@ internal static class Kernel32
public static unsafe extern BOOL GetConsoleMode(HANDLE hConsoleHandle, CONSOLE_MODE* lpMode);
[SuppressMessage("", "SH002")]
[DebuggerStepThrough]
public static unsafe BOOL GetConsoleMode(HANDLE hConsoleHandle, out CONSOLE_MODE mode)
{
fixed (CONSOLE_MODE* lpMode = &mode)
@@ -46,6 +60,7 @@ internal static class Kernel32
[DllImport("KERNEL32.dll", ExactSpelling = true)]
public static unsafe extern BOOL K32EnumProcessModules(HANDLE hProcess, HMODULE* lphModule, uint cb, uint* lpcbNeeded);
[DebuggerStepThrough]
public static unsafe BOOL K32EnumProcessModules(HANDLE hProcess, Span<HMODULE> hModules, out uint cbNeeded)
{
fixed (HMODULE* lphModule = hModules)
@@ -60,6 +75,7 @@ internal static class Kernel32
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
public static extern uint K32GetModuleBaseNameW(HANDLE hProcess, [AllowNull] HMODULE hModule, PWSTR lpBaseName, uint nSize);
[DebuggerStepThrough]
public static unsafe uint K32GetModuleBaseNameW(HANDLE hProcess, [AllowNull] HMODULE hModule, Span<char> baseName)
{
fixed (char* lpBaseName = baseName)
@@ -69,8 +85,9 @@ internal static class Kernel32
}
[DllImport("KERNEL32.dll", ExactSpelling = true)]
public static unsafe extern BOOL K32GetModuleInformation(HANDLE hProcess, HMODULE hModule, [Out] MODULEINFO* lpmodinfo, [In] uint cb);
public static unsafe extern BOOL K32GetModuleInformation(HANDLE hProcess, HMODULE hModule, MODULEINFO* lpmodinfo, uint cb);
[DebuggerStepThrough]
public static unsafe BOOL K32GetModuleInformation(HANDLE hProcess, HMODULE hModule, out MODULEINFO modinfo)
{
fixed (MODULEINFO* lpmodinfo = &modinfo)
@@ -83,6 +100,7 @@ internal static class Kernel32
[SupportedOSPlatform("windows5.1.2600")]
public static unsafe extern BOOL ReadProcessMemory(HANDLE hProcess, void* lpBaseAddress, void* lpBuffer, nuint nSize, [MaybeNull] nuint* lpNumberOfBytesRead);
[DebuggerStepThrough]
public static unsafe BOOL ReadProcessMemory(HANDLE hProcess, void* lpBaseAddress, Span<byte> buffer, [MaybeNull] out nuint numberOfBytesRead)
{
fixed (byte* lpBuffer = buffer)
@@ -94,6 +112,7 @@ internal static class Kernel32
}
}
[DebuggerStepThrough]
public static unsafe BOOL ReadProcessMemory<T>(HANDLE hProcess, void* lpBaseAddress, ref T buffer, [MaybeNull] out nuint numberOfBytesRead)
where T : unmanaged
{
@@ -112,7 +131,8 @@ internal static class Kernel32
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
public static extern BOOL SetConsoleTitleW(PCWSTR lpConsoleTitle);
public static unsafe BOOL SetConsoleTitleW(string consoleTitle)
[DebuggerStepThrough]
public static unsafe BOOL SetConsoleTitleW(ReadOnlySpan<char> consoleTitle)
{
fixed (char* lpConsoleTitle = consoleTitle)
{
@@ -128,6 +148,19 @@ internal static class Kernel32
[SupportedOSPlatform("windows5.1.2600")]
public static unsafe extern BOOL WriteProcessMemory(HANDLE hProcess, void* lpBaseAddress, void* lpBuffer, nuint nSize, nuint* lpNumberOfBytesWritten);
[DebuggerStepThrough]
public static unsafe BOOL WriteProcessMemory(HANDLE hProcess, void* lpBaseAddress, ReadOnlySpan<byte> buffer, out nuint numberOfBytesWritten)
{
fixed (byte* lpBuffer = buffer)
{
fixed (nuint* lpNumberOfBytesWritten = &numberOfBytesWritten)
{
return WriteProcessMemory(hProcess, lpBaseAddress, lpBuffer, (uint)buffer.Length, lpNumberOfBytesWritten);
}
}
}
[DebuggerStepThrough]
public static unsafe BOOL WriteProcessMemory<T>(HANDLE hProcess, void* lpBaseAddress, ref readonly T buffer, out nuint numberOfBytesWritten)
where T : unmanaged
{

View File

@@ -19,6 +19,7 @@ internal static class Macros
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static HRESULT HRESULT_FROM_WIN32(WIN32_ERROR x)
{
// 0x80000000 or 0x80070000 | LOWBYTE(x)
return x <= 0 ? (int)x : (int)(((uint)x & 0x0000FFFFU) | ((uint)FACILITY_CODE.FACILITY_WIN32 << 16) | 0x80000000U);
}
}

View File

@@ -8,13 +8,13 @@ namespace Snap.Hutao.Win32.Memory;
/// </summary>
internal unsafe interface IUnmanagedMemory : IDisposable
{
/// <summary>
/// Gets a pointer to the allocated unmanaged memory.
/// </summary>
void* Pointer { get; }
/// <summary>
/// Gets size of referenced unmanaged memory, in bytes.
/// </summary>
uint Size { get; }
/// <summary>
/// Gets a pointer to the allocated unmanaged memory.
/// </summary>
void* Pointer { get; }
}

View File

@@ -10,16 +10,16 @@ namespace Snap.Hutao.Win32.Memory;
/// </summary>
internal readonly unsafe struct VirtualMemory : IUnmanagedMemory
{
/// <summary>
/// 缓冲区地址
/// </summary>
private readonly void* pointer;
/// <summary>
/// 长度
/// </summary>
private readonly uint size;
/// <summary>
/// 缓冲区地址
/// </summary>
private readonly void* pointer;
/// <summary>
/// 构造一个新的本地内存
/// </summary>
@@ -31,10 +31,10 @@ internal readonly unsafe struct VirtualMemory : IUnmanagedMemory
}
/// <inheritdoc/>
public void* Pointer { get => pointer; }
public uint Size { get => size; }
/// <inheritdoc/>
public uint Size { get => size; }
public void* Pointer { get => pointer; }
/// <inheritdoc/>
public void Dispose()

View File

@@ -0,0 +1,13 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Win32.Foundation;
namespace Snap.Hutao.Win32.NetworkManagement.WindowsFirewall;
[SuppressMessage("", "SA1307")]
internal struct INET_FIREWALL_AC_BINARIES
{
public uint count;
public unsafe PWSTR* binaries;
}

View File

@@ -0,0 +1,13 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Win32.Security;
namespace Snap.Hutao.Win32.NetworkManagement.WindowsFirewall;
[SuppressMessage("", "SA1307")]
internal struct INET_FIREWALL_AC_CAPABILITIES
{
public uint count;
public unsafe SID_AND_ATTRIBUTES* capabilities;
}

View File

@@ -0,0 +1,21 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.Security;
namespace Snap.Hutao.Win32.NetworkManagement.WindowsFirewall;
[SuppressMessage("", "SA1307")]
internal struct INET_FIREWALL_APP_CONTAINER
{
public unsafe SID* appContainerSid;
public unsafe SID* userSid;
public PWSTR appContainerName;
public PWSTR displayName;
public PWSTR description;
public INET_FIREWALL_AC_CAPABILITIES capabilities;
public INET_FIREWALL_AC_BINARIES binaries;
public PWSTR workingDirectory;
public PWSTR packageFullName;
}

View File

@@ -0,0 +1,10 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Win32.NetworkManagement.WindowsFirewall;
internal enum NETISO_FLAG
{
NETISO_FLAG_FORCE_COMPUTE_BINARIES = 1,
NETISO_FLAG_MAX = 2
}

View File

@@ -3,6 +3,7 @@
using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.System.Com;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@@ -16,6 +17,7 @@ internal static class Ole32
[SupportedOSPlatform("windows5.0")]
public static unsafe extern HRESULT CoCreateInstance(Guid* rclsid, [AllowNull] IUnknown pUnkOuter, CLSCTX dwClsContext, Guid* riid, void** ppv);
[DebuggerStepThrough]
public static unsafe HRESULT CoCreateInstance<T>(ref readonly Guid clsid, [AllowNull] IUnknown pUnkOuter, CLSCTX dwClsContext, ref readonly Guid iid, out T* pv)
where T : unmanaged
{
@@ -39,6 +41,7 @@ internal static class Ole32
public static unsafe extern HRESULT CoWaitForMultipleObjects(uint dwFlags, uint dwTimeout, uint cHandles, HANDLE* pHandles, uint* lpdwindex);
[SuppressMessage("", "SH002")]
[DebuggerStepThrough]
public static unsafe HRESULT CoWaitForMultipleObjects(uint dwFlags, uint dwTimeout, ReadOnlySpan<HANDLE> handles, out uint dwindex)
{
fixed (HANDLE* pHandles = handles)

View File

@@ -0,0 +1,14 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Win32.Foundation;
namespace Snap.Hutao.Win32.Security;
internal struct SID
{
public byte Revision;
public byte SubAuthorityCount;
public SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
public FlexibleArray<uint> SubAuthority;
}

View File

@@ -0,0 +1,12 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Win32.Foundation;
namespace Snap.Hutao.Win32.Security;
internal struct SID_AND_ATTRIBUTES
{
public unsafe PSID Sid;
public uint Attributes;
}

View File

@@ -0,0 +1,9 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Win32.Security;
internal struct SID_IDENTIFIER_AUTHORITY
{
public unsafe fixed byte Value[6];
}

View File

@@ -3,6 +3,7 @@
using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.System.Com;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@@ -16,6 +17,7 @@ internal static class Shell32
[SupportedOSPlatform("windows6.0.6000")]
public static unsafe extern HRESULT SHCreateItemFromParsingName(PCWSTR pszPath, [AllowNull] IBindCtx pbc, Guid* riid, void** ppv);
[DebuggerStepThrough]
public static unsafe HRESULT SHCreateItemFromParsingName<T>(ReadOnlySpan<char> szPath, [AllowNull] IBindCtx pbc, ref readonly Guid riid, out T* pv)
where T : unmanaged
{

View File

@@ -2,6 +2,7 @@
// Licensed under the MIT license.
using System.Runtime.InteropServices;
using Snap.Hutao.Win32.Foundation;
namespace Snap.Hutao.Win32.UI.Shell.Common;
@@ -10,5 +11,5 @@ namespace Snap.Hutao.Win32.UI.Shell.Common;
internal struct SHITEMID
{
public ushort cb;
public unsafe byte* abID;
public FlexibleArray<byte> abID;
}

View File

@@ -5,6 +5,7 @@ using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.Graphics.Gdi;
using Snap.Hutao.Win32.UI.Input.KeyboardAndMouse;
using Snap.Hutao.Win32.UI.WindowsAndMessaging;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@@ -22,6 +23,7 @@ internal static class User32
[SupportedOSPlatform("windows5.0")]
public static extern HWND FindWindowExW([AllowNull] HWND hWndParent, [AllowNull] HWND hWndChildAfter, [AllowNull] PCWSTR lpszClass, [AllowNull] PCWSTR lpszWindow);
[DebuggerStepThrough]
public static unsafe HWND FindWindowExW([AllowNull] HWND hWndParent, [AllowNull] HWND hWndChildAfter, [AllowNull] string szClass, [AllowNull] string szWindow)
{
fixed (char* lpszClass = szClass)
@@ -53,6 +55,7 @@ internal static class User32
[SupportedOSPlatform("windows5.0")]
public static unsafe extern BOOL GetWindowPlacement(HWND hWnd, WINDOWPLACEMENT* lpwndpl);
[DebuggerStepThrough]
public static unsafe BOOL GetWindowPlacement(HWND hWnd, ref WINDOWPLACEMENT wndpl)
{
fixed (WINDOWPLACEMENT* lpwndpl = &wndpl)
@@ -65,6 +68,7 @@ internal static class User32
[SupportedOSPlatform("windows5.0")]
public static unsafe extern uint GetWindowThreadProcessId(HWND hWnd, [MaybeNull] uint* lpdwProcessId);
[DebuggerStepThrough]
public static unsafe uint GetWindowThreadProcessId(HWND hWnd, [MaybeNull] out uint dwProcessId)
{
fixed (uint* lpdwProcessId = &dwProcessId)
@@ -85,6 +89,7 @@ internal static class User32
[SupportedOSPlatform("windows5.0")]
public static unsafe extern uint SendInput(uint cInputs, INPUT* pInputs, int cbSize);
[DebuggerStepThrough]
public static unsafe uint SendInput(ReadOnlySpan<INPUT> inputs, int cbSize)
{
fixed (INPUT* pInputs = inputs)