Skip to content

Commit 66c272c

Browse files
committed
Fix size of atoms, size_t parameters. Addresses CA1901
1 parent 71f2464 commit 66c272c

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

Rubberduck.Core/Common/Hotkeys/Hotkey.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Hotkey(IntPtr hWndVbe, string key, CommandBase command, Keys secondKey =
3333
public Keys Combo { get; }
3434
public Keys SecondKey { get; }
3535
public bool IsTwoStepHotkey { get; }
36-
public bool IsAttached => HotkeyInfo.HookId != IntPtr.Zero;
36+
public bool IsAttached => HotkeyInfo.HookId != 0;
3737

3838
public event EventHandler<HookEventArgs> MessageReceived;
3939

@@ -69,7 +69,7 @@ public void Detach()
6969
return;
7070
}
7171

72-
if (!User32.UnregisterHotKey(_hWndVbe, HotkeyInfo.HookId))
72+
if (!User32.UnregisterHotKey(_hWndVbe, new IntPtr(HotkeyInfo.HookId)))
7373
{
7474
var error = Marshal.GetLastWin32Error();
7575
Logger.Warn($"Error calling UnregisterHotKey on hokey with id {HotkeyInfo.HookId} for command of type {Command.GetType()}; the error was {error}; going to delete the atom anyway... The memory may leak.");
@@ -82,7 +82,7 @@ public void Detach()
8282
Logger.Warn($"Error calling DeleteGlobalAtom; the error was {lastError}, the id {HotkeyInfo.HookId} and the type of the associated command {Command.GetType()}.");
8383
}
8484

85-
HotkeyInfo = new HotkeyInfo(IntPtr.Zero, Combo);
85+
HotkeyInfo = new HotkeyInfo(0, Combo);
8686
ClearCommandShortcutText();
8787
}
8888

@@ -93,15 +93,15 @@ private void HookKey(Keys key, uint shift)
9393
return;
9494
}
9595

96-
var hookId = (IntPtr)Kernel32.GlobalAddAtom(Guid.NewGuid().ToString());
96+
var hookId = Kernel32.GlobalAddAtom(Guid.NewGuid().ToString());
9797
var error = Marshal.GetLastWin32Error();
98-
if (hookId == IntPtr.Zero)
98+
if (hookId == 0)
9999
{
100100
Logger.Warn($"Error calling GlobalAddAtom; the error was {error}; aborting the HookKey operation...");
101101
return;
102102
}
103103

104-
var success = User32.RegisterHotKey(_hWndVbe, hookId, shift, (uint)key);
104+
var success = User32.RegisterHotKey(_hWndVbe, new IntPtr(hookId), shift, (uint)key);
105105
if (!success)
106106
{
107107
Logger.Debug(RubberduckUI.CommonHotkey_KeyNotRegistered, key);

Rubberduck.Core/Common/Hotkeys/HotkeyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ public struct HotkeyInfo
99
{
1010
private const Keys Modifiers = Keys.Alt | Keys.Control | Keys.Shift;
1111

12-
public HotkeyInfo(IntPtr hookId, Keys keys)
12+
public HotkeyInfo(ushort hookId, Keys keys)
1313
{
1414
HookId = hookId;
1515
Keys = keys;
1616
}
1717

18-
public IntPtr HookId { get; }
18+
public ushort HookId { get; }
1919
public Keys Keys { get; }
2020

2121
public override string ToString()

Rubberduck.Core/Common/WinAPI/Kernel32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static class Kernel32
2828
/// <param name="nAtom">The atom and character string to be deleted.</param>
2929
/// <returns>The function always returns (ATOM) 0.</returns>
3030
[DllImport("kernel32.dll", SetLastError=true, ExactSpelling=true)]
31-
public static extern ushort GlobalDeleteAtom(IntPtr nAtom);
31+
public static extern ushort GlobalDeleteAtom(ushort nAtom);
3232

3333
/// <summary>
3434
/// Sets the last-error code for the calling thread.

Rubberduck.VBEEditor/ComManagement/TypeLibs/TypeLibsSupport.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public static class UnmanagedMemHelper
434434
/// Windows API call used for memory range validation
435435
/// </summary>
436436
[DllImport("kernel32.dll")]
437-
public static extern int VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);
437+
public static extern IntPtr VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, IntPtr dwLength);
438438

439439
/// <summary>
440440
/// Do our best to validate that the input memory address is actually a COM object
@@ -479,10 +479,13 @@ public static bool IsValidMemoryRange(IntPtr memOffset, int size, bool checkIsEx
479479
if (memOffset == IntPtr.Zero) return false;
480480

481481
var memInfo = new MEMORY_BASIC_INFORMATION();
482-
var sizeOfMemInfo = Marshal.SizeOf(memInfo);
482+
var sizeOfMemInfo = new IntPtr(Marshal.SizeOf(memInfo));
483483

484484
// most of the time, a bad pointer will fail here
485-
if (VirtualQuery(memOffset, out memInfo, sizeOfMemInfo) != sizeOfMemInfo) return false;
485+
if (VirtualQuery(memOffset, out memInfo, sizeOfMemInfo) != sizeOfMemInfo)
486+
{
487+
return false;
488+
}
486489

487490
// check the memory area is not a guard page, or otherwise inaccessible
488491
if ((memInfo.Protect.HasFlag(ALLOCATION_PROTECTION.PAGE_NOACCESS)) ||

0 commit comments

Comments
 (0)