Skip to content

Commit 1f08b01

Browse files
committed
Replace many extern declarations w/ CsWin32
1 parent 083e912 commit 1f08b01

36 files changed

+434
-597
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<PackageVersion Include="Microsoft.Data.Sqlite.Core" Version="8.0.4" />
1919
<PackageVersion Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" />
2020
<PackageVersion Include="Microsoft.Win32.Registry" Version="5.0.0" />
21+
<PackageVersion Include="Microsoft.Windows.CsWin32" Version="0.3.183" />
2122
<PackageVersion Include="MSTest" Version="3.8.3" />
2223
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
2324
<PackageVersion Include="Nullable" Version="1.3.1" />

src/BizHawk.Bizware.Input/KeyMouseInput/RawKeyMouseInput.cs

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,26 @@
77
using BizHawk.Common;
88
using BizHawk.Common.CollectionExtensions;
99

10+
using Windows.Win32;
11+
using Windows.Win32.Foundation;
12+
using Windows.Win32.UI.WindowsAndMessaging;
13+
1014
using static BizHawk.Common.RawInputImports;
11-
using static BizHawk.Common.WmImports;
15+
using static BizHawk.Common.WmImports1;
16+
using static Windows.Win32.Win32Imports;
1217

1318
namespace BizHawk.Bizware.Input
1419
{
1520
/// <summary>
1621
/// Note: Only 1 window per device class (e.g. keyboards) is actually allowed to use RAWINPUT (last one to call RegisterRawInputDevices)
1722
/// So only one instance can actually be used at the same time
1823
/// </summary>
19-
internal sealed class RawKeyMouseInput : IKeyMouseInput
24+
internal sealed unsafe class RawKeyMouseInput : IKeyMouseInput
2025
{
2126
private const int WM_CLOSE = 0x0010;
2227
private const int WM_INPUT = 0x00FF;
2328

24-
private IntPtr RawInputWindow;
29+
private HWND RawInputWindow;
2530
private bool _handleAltKbLayouts;
2631
private List<KeyEvent> _keyEvents = [ ];
2732
private (int X, int Y) _mouseDelta;
@@ -33,27 +38,29 @@ internal sealed class RawKeyMouseInput : IKeyMouseInput
3338
private int RawInputBufferSize;
3439
private readonly int RawInputBufferDataOffset;
3540

36-
private static readonly WNDPROC _wndProc = WndProc;
37-
38-
private static readonly Lazy<IntPtr> _rawInputWindowAtom = new(() =>
41+
private static unsafe readonly Lazy<PCWSTR> _rawInputWindowAtom = new(() =>
3942
{
40-
var wc = default(WNDCLASSW);
41-
wc.lpfnWndProc = _wndProc;
42-
wc.hInstance = LoaderApiImports.GetModuleHandleW(null);
43-
wc.lpszClassName = "RawKeyMouseInputClass";
44-
45-
var atom = RegisterClassW(ref wc);
46-
if (atom == IntPtr.Zero)
43+
WNDCLASSW wc = default;
44+
wc.lpfnWndProc = WndProc;
45+
wc.hInstance = GetModuleHandleW(default(PCWSTR));
46+
var lpszClassNameStr = "RawKeyMouseInputClass";
47+
PCWSTR atom;
48+
fixed (char* lpszClassName = lpszClassNameStr)
49+
{
50+
wc.lpszClassName = lpszClassName;
51+
atom = MAKEINTATOM(RegisterClassW(in wc));
52+
}
53+
if (atom.Value is null)
4754
{
4855
throw new InvalidOperationException("Failed to register RAWINPUT window class");
4956
}
5057

5158
return atom;
5259
});
5360

54-
private static unsafe IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
61+
private static LRESULT WndProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
5562
{
56-
var ud = GetWindowLongPtrW(hWnd, GWLP_USERDATA);
63+
var ud = GetWindowLongPtrW(hWnd, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA);
5764
if (ud == IntPtr.Zero)
5865
{
5966
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
@@ -65,7 +72,7 @@ private static unsafe IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntP
6572
{
6673
if (uMsg == WM_CLOSE)
6774
{
68-
SetWindowLongPtrW(hWnd, GWLP_USERDATA, IntPtr.Zero);
75+
SetWindowLongPtrW(hWnd, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA, IntPtr.Zero);
6976
handle = GCHandle.FromIntPtr(ud);
7077
rawKeyMouseInput = (RawKeyMouseInput)handle.Target;
7178
Marshal.FreeCoTaskMem(rawKeyMouseInput.RawInputBuffer);
@@ -157,7 +164,7 @@ private static unsafe IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntP
157164
}
158165
}
159166

160-
return IntPtr.Zero;
167+
return default;
161168
}
162169

163170
private unsafe void AddKeyInput(RAWKEYBOARD* keyboard)
@@ -203,24 +210,27 @@ private unsafe void AddMouseInput(RAWMOUSE* mouse)
203210
}
204211
}
205212

206-
private static IntPtr CreateRawInputWindow()
213+
private static unsafe HWND CreateRawInputWindow()
207214
{
208-
const int WS_CHILD = 0x40000000;
209-
var window = CreateWindowExW(
215+
var lpWindowNameStr = "RawKeyInput";
216+
HWND window;
217+
fixed (char* lpWindowName = lpWindowNameStr)
218+
{
219+
window = CreateWindowExW(
210220
dwExStyle: 0,
211221
lpClassName: _rawInputWindowAtom.Value,
212-
lpWindowName: "RawKeyInput",
213-
dwStyle: WS_CHILD,
222+
lpWindowName: lpWindowName,
223+
dwStyle: WINDOW_STYLE.WS_CHILD,
214224
X: 0,
215225
Y: 0,
216226
nWidth: 1,
217227
nHeight: 1,
218-
hWndParent: HWND_MESSAGE,
219-
hMenu: IntPtr.Zero,
220-
hInstance: LoaderApiImports.GetModuleHandleW(null),
221-
lpParam: IntPtr.Zero);
222-
223-
if (window == IntPtr.Zero)
228+
hWndParent: HWND.HWND_MESSAGE,
229+
hMenu: default,
230+
hInstance: GetModuleHandleW(default(PCWSTR)),
231+
lpParam: default);
232+
}
233+
if (window.IsNull)
224234
{
225235
throw new InvalidOperationException("Failed to create RAWINPUT window");
226236
}
@@ -280,11 +290,11 @@ public void Dispose()
280290
{
281291
lock (_lockObj)
282292
{
283-
if (RawInputWindow != IntPtr.Zero)
293+
if (!RawInputWindow.IsNull)
284294
{
285295
// Can't use DestroyWindow, that's only allowed in the thread that created the window!
286-
PostMessageW(RawInputWindow, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
287-
RawInputWindow = IntPtr.Zero;
296+
PostMessageW(RawInputWindow, WM_CLOSE, default, default);
297+
RawInputWindow = HWND.Null;
288298
}
289299
else
290300
{
@@ -310,15 +320,20 @@ public IEnumerable<KeyEvent> UpdateKeyInputs(bool handleAltKbLayouts)
310320
{
311321
RawInputWindow = CreateRawInputWindow();
312322
var handle = GCHandle.Alloc(this, GCHandleType.Normal);
313-
SetWindowLongPtrW(RawInputWindow, GWLP_USERDATA, GCHandle.ToIntPtr(handle));
323+
SetWindowLongPtrW(RawInputWindow, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA, GCHandle.ToIntPtr(handle));
314324
}
315325

316326
_handleAltKbLayouts = handleAltKbLayouts;
317327

318-
while (PeekMessageW(out var msg, RawInputWindow, 0, 0, PM_REMOVE))
328+
while (PeekMessageW(
329+
out var msg,
330+
RawInputWindow,
331+
wMsgFilterMin: 0,
332+
wMsgFilterMax: 0,
333+
PEEK_MESSAGE_REMOVE_TYPE.PM_REMOVE))
319334
{
320-
TranslateMessage(ref msg);
321-
DispatchMessageW(ref msg);
335+
TranslateMessage(in msg);
336+
DispatchMessageW(in msg);
322337
}
323338

324339
var ret = _keyEvents;
@@ -340,13 +355,13 @@ public IEnumerable<KeyEvent> UpdateKeyInputs(bool handleAltKbLayouts)
340355
{
341356
RawInputWindow = CreateRawInputWindow();
342357
var handle = GCHandle.Alloc(this, GCHandleType.Normal);
343-
SetWindowLongPtrW(RawInputWindow, GWLP_USERDATA, GCHandle.ToIntPtr(handle));
358+
SetWindowLongPtrW(RawInputWindow, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA, GCHandle.ToIntPtr(handle));
344359
}
345360

346-
while (PeekMessageW(out var msg, RawInputWindow, 0, 0, PM_REMOVE))
361+
while (PeekMessageW(out var msg, RawInputWindow, 0, 0, PEEK_MESSAGE_REMOVE_TYPE.PM_REMOVE))
347362
{
348-
TranslateMessage(ref msg);
349-
DispatchMessageW(ref msg);
363+
TranslateMessage(in msg);
364+
DispatchMessageW(in msg);
350365
}
351366

352367
var ret = _mouseDelta;

src/BizHawk.Bizware.Input/SDL2/SDL2InputAdapter.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using BizHawk.Common.NumberExtensions;
1010
#endif
1111

12+
using Windows.Win32.UI.WindowsAndMessaging;
13+
1214
using static SDL2.SDL;
1315

1416
#pragma warning disable BHI1007 // target-typed Exception TODO don't
@@ -49,10 +51,15 @@ private static void DoSDLEventLoop()
4951
// similar code shouldn't be needed on other platforms (which have global message queues and not thread local message queues)
5052
if (!OSTailoredCode.IsUnixHost)
5153
{
52-
while (WmImports.PeekMessageW(out var msg, IntPtr.Zero, 0, 0, WmImports.PM_REMOVE))
54+
while (WmImports.PeekMessageW(
55+
out var msg,
56+
hWnd: default,
57+
wMsgFilterMin: 0,
58+
wMsgFilterMax: 0,
59+
PEEK_MESSAGE_REMOVE_TYPE.PM_REMOVE))
5360
{
54-
WmImports.TranslateMessage(ref msg);
55-
WmImports.DispatchMessageW(ref msg);
61+
WmImports.TranslateMessage(in msg);
62+
WmImports.DispatchMessageW(in msg);
5663
}
5764
}
5865

src/BizHawk.Client.DiscoHawk/Program.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using BizHawk.Common.StringExtensions;
99
using BizHawk.Emulation.DiscSystem;
1010

11+
using Windows.Win32;
12+
using Windows.Win32.UI.WindowsAndMessaging;
13+
1114
namespace BizHawk.Client.DiscoHawk
1215
{
1316
internal static class Program
@@ -67,9 +70,9 @@ private static void Main(string[] args)
6770
const uint WM_DROPFILES = 0x0233;
6871
const uint WM_COPYDATA = 0x004A;
6972
const uint WM_COPYGLOBALDATA = 0x0049;
70-
WmImports.ChangeWindowMessageFilter(WM_DROPFILES, WmImports.ChangeWindowMessageFilterFlags.Add);
71-
WmImports.ChangeWindowMessageFilter(WM_COPYDATA, WmImports.ChangeWindowMessageFilterFlags.Add);
72-
WmImports.ChangeWindowMessageFilter(WM_COPYGLOBALDATA, WmImports.ChangeWindowMessageFilterFlags.Add);
73+
WmImports.ChangeWindowMessageFilter(WM_DROPFILES, CHANGE_WINDOW_MESSAGE_FILTER_FLAGS.MSGFLT_ADD);
74+
WmImports.ChangeWindowMessageFilter(WM_COPYDATA, CHANGE_WINDOW_MESSAGE_FILTER_FLAGS.MSGFLT_ADD);
75+
WmImports.ChangeWindowMessageFilter(WM_COPYGLOBALDATA, CHANGE_WINDOW_MESSAGE_FILTER_FLAGS.MSGFLT_ADD);
7376

7477
// this will look in subdirectory "dll" to load pinvoked stuff
7578
var dllDir = Path.Combine(AppContext.BaseDirectory, "dll");
@@ -83,22 +86,22 @@ private static void Main(string[] args)
8386

8487
if (dllDir.ContainsOrdinal(';'))
8588
{
86-
var dllShortPathLen = Win32Imports.GetShortPathNameW(dllDir, null, 0);
89+
var dllShortPathLen = Win32Imports.GetShortPathNameW(dllDir);
8790
if (dllShortPathLen == 0)
8891
{
8992
MessageBox.Show(SEMICOLON_IN_DIR_MSG);
9093
return;
9194
}
9295

9396
var dllShortPathBuffer = new char[dllShortPathLen];
94-
dllShortPathLen = Win32Imports.GetShortPathNameW(dllDir, dllShortPathBuffer, dllShortPathLen);
97+
dllShortPathLen = Win32Imports.GetShortPathNameW(dllDir, dllShortPathBuffer);
9598
if (dllShortPathLen == 0)
9699
{
97100
MessageBox.Show(SEMICOLON_IN_DIR_MSG);
98101
return;
99102
}
100103

101-
dllDir = new string(dllShortPathBuffer, 0, dllShortPathLen);
104+
dllDir = dllShortPathBuffer.AsSpan(start: 0, length: (int) dllShortPathLen).ToString();
102105
if (dllDir.ContainsOrdinal(';'))
103106
{
104107
MessageBox.Show(SEMICOLON_IN_DIR_MSG);

src/BizHawk.Client.EmuHawk/AVOut/AviWriter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using BizHawk.Common.PathExtensions;
1212
using BizHawk.Emulation.Common;
1313

14-
using static BizHawk.Common.HeapApiImports;
14+
using Windows.Win32;
1515

1616
// some helpful p/invoke from http://www.codeproject.com/KB/audio-video/Motion_Detection.aspx?msg=1142967
1717
namespace BizHawk.Client.EmuHawk
@@ -456,12 +456,12 @@ public static void DeallocateAVICOMPRESSOPTIONS(ref AVIWriterImports.AVICOMPRESS
456456
#if false // test: increase stability by never freeing anything, ever
457457
if (opts.lpParms != IntPtr.Zero)
458458
{
459-
HeapFree(GetProcessHeap(), 0, opts.lpParms);
459+
HeapFree(GetProcessHeap_SafeHandle(), 0, opts.lpParms);
460460
}
461461

462462
if (opts.lpFormat != IntPtr.Zero)
463463
{
464-
HeapFree(GetProcessHeap(), 0, opts.lpFormat);
464+
HeapFree(GetProcessHeap_SafeHandle(), 0, opts.lpFormat);
465465
}
466466
#endif
467467
#if AVI_SUPPORT
@@ -473,13 +473,13 @@ public void AllocateToAVICOMPRESSOPTIONS(out AVIWriterImports.AVICOMPRESSOPTIONS
473473
{
474474
if (_comprOptions.cbParms != 0)
475475
{
476-
_comprOptions.lpParms = HeapAlloc(GetProcessHeap(), 0, _comprOptions.cbParms);
476+
_comprOptions.lpParms = Win32Imports.HeapAlloc(_comprOptions.cbParms);
477477
Marshal.Copy(Parms, 0, _comprOptions.lpParms, _comprOptions.cbParms);
478478
}
479479

480480
if (_comprOptions.cbFormat != 0)
481481
{
482-
_comprOptions.lpFormat = HeapAlloc(GetProcessHeap(), 0, _comprOptions.cbFormat);
482+
_comprOptions.lpFormat = Win32Imports.HeapAlloc(_comprOptions.cbFormat);
483483
Marshal.Copy(Format, 0, _comprOptions.lpFormat, _comprOptions.cbFormat);
484484
}
485485

src/BizHawk.Client.EmuHawk/CustomControls/FolderBrowserDialogEx.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Threading;
44
using System.Windows.Forms;
55

6-
using BizHawk.Common;
6+
using Windows.Win32;
77

88
using static BizHawk.Common.Shell32Imports;
99

@@ -39,7 +39,7 @@ int Callback(IntPtr hwnd, uint uMsg, IntPtr lParam, IntPtr lpData)
3939
var str = Marshal.StringToHGlobalUni(SelectedPath);
4040
try
4141
{
42-
WmImports.SendMessageW(hwnd, BFFM_SETSELECTIONW, new(1), str);
42+
WmImports.SendMessageW(new(hwnd), BFFM_SETSELECTIONW, new(1), str);
4343
}
4444
finally
4545
{

src/BizHawk.Client.EmuHawk/CustomControls/InputWidget.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected override void OnMouseClick(MouseEventArgs e)
6464
{
6565
if (!OSTailoredCode.IsUnixHost)
6666
{
67-
WmImports.HideCaret(Handle);
67+
WmImports.HideCaret(new(Handle));
6868
}
6969

7070
base.OnMouseClick(e);
@@ -260,7 +260,7 @@ protected override void OnGotFocus(EventArgs e)
260260
{
261261
if (!OSTailoredCode.IsUnixHost)
262262
{
263-
WmImports.HideCaret(Handle);
263+
WmImports.HideCaret(new(Handle));
264264
}
265265
}
266266

@@ -269,4 +269,4 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
269269
return !(keyData.ToString() == "F4" || keyData.ToString().Contains("Alt"));
270270
}
271271
}
272-
}
272+
}

src/BizHawk.Client.EmuHawk/EmuHawkUtil.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
using BizHawk.Common;
55
using BizHawk.Common.StringExtensions;
66

7+
using Windows.Win32;
8+
using Windows.Win32.System.Com;
9+
using Windows.Win32.UI.Shell;
10+
711
namespace BizHawk.Client.EmuHawk
812
{
913
public static class EmuHawkUtil
@@ -21,21 +25,13 @@ public static string ResolveShortcut(string filename)
2125
return filename; // archive internal files are never shortcuts (and choke when analyzing any further)
2226
}
2327

24-
using var link = new ShellLinkImports.ShellLink();
25-
26-
unsafe
27-
{
28-
const uint STGM_READ = 0;
29-
((ShellLinkImports.IPersistFile*)link)->Load(filename, STGM_READ);
30-
28+
ShellLink link = new();
29+
((IPersistFile) link).Load(filename, unchecked((uint) STGM.STGM_READ));
3130
#if false
32-
// TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.
33-
((ShellLinkImports.IShellLinkW*)link)->Resolve(hwnd, 0);
31+
// TODO: if I can get hold of the hwnd call resolve first. This handles moved and renamed files.
32+
((IShellLinkW) link).Resolve(hwnd, 0);
3433
#endif
35-
36-
((ShellLinkImports.IShellLinkW*) link)->GetPath(out var path, (int) Win32Imports.MAX_PATH + 1, 0);
37-
return path;
38-
}
34+
return ((IShellLinkW) link).GetPath();
3935
}
4036
}
4137
}

0 commit comments

Comments
 (0)