Merge pull request #2485 from Haselnussbomber/update-cswin32

[API14] Update Microsoft.Windows.CsWin32
This commit is contained in:
goat 2025-12-06 18:35:41 +01:00 committed by GitHub
commit 61123ce573
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 42 additions and 21 deletions

View file

@ -263,7 +263,7 @@ public sealed class EntryPoint
var symbolPath = Path.Combine(info.AssetDirectory, "UIRes", "pdb"); var symbolPath = Path.Combine(info.AssetDirectory, "UIRes", "pdb");
var searchPath = $".;{symbolPath}"; var searchPath = $".;{symbolPath}";
var currentProcess = Windows.Win32.PInvoke.GetCurrentProcess_SafeHandle(); var currentProcess = Windows.Win32.PInvoke.GetCurrentProcess();
// Remove any existing Symbol Handler and Init a new one with our search path added // Remove any existing Symbol Handler and Init a new one with our search path added
Windows.Win32.PInvoke.SymCleanup(currentProcess); Windows.Win32.PInvoke.SymCleanup(currentProcess);

View file

@ -201,19 +201,19 @@ public abstract class Hook<T> : IDalamudHook where T : Delegate
if (EnvironmentConfiguration.DalamudForceMinHook) if (EnvironmentConfiguration.DalamudForceMinHook)
useMinHook = true; useMinHook = true;
using var moduleHandle = Windows.Win32.PInvoke.GetModuleHandle(moduleName); var moduleHandle = Windows.Win32.PInvoke.GetModuleHandle(moduleName);
if (moduleHandle.IsInvalid) if (moduleHandle.IsNull)
throw new Exception($"Could not get a handle to module {moduleName}"); throw new Exception($"Could not get a handle to module {moduleName}");
var procAddress = (nint)Windows.Win32.PInvoke.GetProcAddress(moduleHandle, exportName); var procAddress = Windows.Win32.PInvoke.GetProcAddress(moduleHandle, exportName);
if (procAddress == IntPtr.Zero) if (procAddress.IsNull)
throw new Exception($"Could not get the address of {moduleName}::{exportName}"); throw new Exception($"Could not get the address of {moduleName}::{exportName}");
procAddress = HookManager.FollowJmp(procAddress); var address = HookManager.FollowJmp(procAddress.Value);
if (useMinHook) if (useMinHook)
return new MinHookHook<T>(procAddress, detour, Assembly.GetCallingAssembly()); return new MinHookHook<T>(address, detour, Assembly.GetCallingAssembly());
else else
return new ReloadedHook<T>(procAddress, detour, Assembly.GetCallingAssembly()); return new ReloadedHook<T>(address, detour, Assembly.GetCallingAssembly());
} }
/// <summary> /// <summary>

View file

@ -1,4 +1,5 @@
{ {
"$schema": "https://aka.ms/CsWin32.schema.json", "$schema": "https://aka.ms/CsWin32.schema.json",
"useSafeHandles": false,
"allowMarshaling": false "allowMarshaling": false
} }

View file

@ -1,6 +1,8 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using Windows.Win32.Foundation;
namespace Dalamud; namespace Dalamud;
/// <summary> /// <summary>
@ -12,11 +14,11 @@ namespace Dalamud;
/// </remarks> /// </remarks>
public static class SafeMemory public static class SafeMemory
{ {
private static readonly SafeHandle Handle; private static readonly HANDLE Handle;
static SafeMemory() static SafeMemory()
{ {
Handle = Windows.Win32.PInvoke.GetCurrentProcess_SafeHandle(); Handle = Windows.Win32.PInvoke.GetCurrentProcess();
} }
/// <summary> /// <summary>
@ -28,6 +30,12 @@ public static class SafeMemory
/// <returns>Whether the read succeeded.</returns> /// <returns>Whether the read succeeded.</returns>
public static unsafe bool ReadBytes(IntPtr address, int count, out byte[] buffer) public static unsafe bool ReadBytes(IntPtr address, int count, out byte[] buffer)
{ {
if (Handle.IsNull)
{
buffer = [];
return false;
}
buffer = new byte[count <= 0 ? 0 : count]; buffer = new byte[count <= 0 ? 0 : count];
fixed (byte* p = buffer) fixed (byte* p = buffer)
{ {
@ -54,6 +62,9 @@ public static class SafeMemory
/// <returns>Whether the write succeeded.</returns> /// <returns>Whether the write succeeded.</returns>
public static unsafe bool WriteBytes(IntPtr address, byte[] buffer) public static unsafe bool WriteBytes(IntPtr address, byte[] buffer)
{ {
if (Handle.IsNull)
return false;
if (buffer.Length == 0) if (buffer.Length == 0)
return true; return true;

View file

@ -1,7 +1,8 @@
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Windows.Win32.Foundation;
using Windows.Win32.Storage.FileSystem; using Windows.Win32.Storage.FileSystem;
namespace Dalamud.Utility; namespace Dalamud.Utility;
@ -47,30 +48,39 @@ public static class FilesystemUtil
// Open the temp file // Open the temp file
var tempPath = path + ".tmp"; var tempPath = path + ".tmp";
using var tempFile = Windows.Win32.PInvoke.CreateFile( var tempFile = Windows.Win32.PInvoke.CreateFile(
tempPath, tempPath,
(uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE), (uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE),
FILE_SHARE_MODE.FILE_SHARE_NONE, FILE_SHARE_MODE.FILE_SHARE_NONE,
null, null,
FILE_CREATION_DISPOSITION.CREATE_ALWAYS, FILE_CREATION_DISPOSITION.CREATE_ALWAYS,
FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL,
null); HANDLE.Null);
if (tempFile.IsInvalid) if (tempFile.IsNull)
throw new Win32Exception(); throw new Win32Exception();
// Write the data // Write the data
uint bytesWritten = 0; uint bytesWritten = 0;
if (!Windows.Win32.PInvoke.WriteFile(tempFile, new ReadOnlySpan<byte>(bytes), &bytesWritten, null)) fixed (byte* ptr = bytes)
throw new Win32Exception(); {
if (!Windows.Win32.PInvoke.WriteFile(tempFile, ptr, (uint)bytes.Length, &bytesWritten, null))
throw new Win32Exception();
}
if (bytesWritten != bytes.Length) if (bytesWritten != bytes.Length)
{
Windows.Win32.PInvoke.CloseHandle(tempFile);
throw new Exception($"Could not write all bytes to temp file ({bytesWritten} of {bytes.Length})"); throw new Exception($"Could not write all bytes to temp file ({bytesWritten} of {bytes.Length})");
}
if (!Windows.Win32.PInvoke.FlushFileBuffers(tempFile)) if (!Windows.Win32.PInvoke.FlushFileBuffers(tempFile))
{
Windows.Win32.PInvoke.CloseHandle(tempFile);
throw new Win32Exception(); throw new Win32Exception();
}
tempFile.Close(); Windows.Win32.PInvoke.CloseHandle(tempFile);
if (!Windows.Win32.PInvoke.MoveFileEx(tempPath, path, MOVE_FILE_FLAGS.MOVEFILE_REPLACE_EXISTING | MOVE_FILE_FLAGS.MOVEFILE_WRITE_THROUGH)) if (!Windows.Win32.PInvoke.MoveFileEx(tempPath, path, MOVE_FILE_FLAGS.MOVEFILE_REPLACE_EXISTING | MOVE_FILE_FLAGS.MOVEFILE_WRITE_THROUGH))
throw new Win32Exception(); throw new Win32Exception();

View file

@ -858,7 +858,7 @@ public static partial class Util
var sizeWithTerminators = pathBytesSize + (pathBytes.Length * 2); var sizeWithTerminators = pathBytesSize + (pathBytes.Length * 2);
var dropFilesSize = sizeof(DROPFILES); var dropFilesSize = sizeof(DROPFILES);
var hGlobal = Win32_PInvoke.GlobalAlloc_SafeHandle( var hGlobal = Win32_PInvoke.GlobalAlloc(
GLOBAL_ALLOC_FLAGS.GHND, GLOBAL_ALLOC_FLAGS.GHND,
// struct size + size of encoded strings + null terminator for each // struct size + size of encoded strings + null terminator for each
// string + two null terminators for end of list // string + two null terminators for end of list
@ -896,12 +896,11 @@ public static partial class Util
{ {
Win32_PInvoke.SetClipboardData( Win32_PInvoke.SetClipboardData(
(uint)CLIPBOARD_FORMAT.CF_HDROP, (uint)CLIPBOARD_FORMAT.CF_HDROP,
hGlobal); (Windows.Win32.Foundation.HANDLE)hGlobal.Value);
Win32_PInvoke.CloseClipboard(); Win32_PInvoke.CloseClipboard();
return true; return true;
} }
hGlobal.Dispose();
return false; return false;
} }

View file

@ -27,7 +27,7 @@
<!-- DirectX / Win32 --> <!-- DirectX / Win32 -->
<PackageVersion Include="TerraFX.Interop.Windows" Version="10.0.26100.5" /> <PackageVersion Include="TerraFX.Interop.Windows" Version="10.0.26100.5" />
<PackageVersion Include="Microsoft.Windows.CsWin32" Version="0.3.183" /> <PackageVersion Include="Microsoft.Windows.CsWin32" Version="0.3.259" />
<!-- Logging --> <!-- Logging -->
<PackageVersion Include="Serilog" Version="4.0.2" /> <PackageVersion Include="Serilog" Version="4.0.2" />