Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 3f8a252

Browse files
Kaim, Sean MKaim, Sean M
authored andcommitted
Fixed some complier errors
Fixed a number of complier errors Adjusted the code analysis ruleset to minimum recommended (for now)
1 parent 65c7657 commit 3f8a252

9 files changed

Lines changed: 108 additions & 72 deletions

File tree

SharedLibraryNG/KeyboardHook.cs

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44
//
55

66
using System;
7+
using System.Collections.Generic;
78
using System.ComponentModel;
89
using System.Diagnostics;
910
using System.Runtime.InteropServices;
10-
using System.Collections.Generic;
1111

1212
namespace SharedLibraryNG
1313
{
14+
1415
public class KeyboardHook
1516
{
16-
// ReSharper disable InconsistentNaming
17-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
18-
[return: MarshalAs(UnmanagedType.Bool)]
19-
public static extern bool PostMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, HookKeyMsgData lParam);
20-
// ReSharper restore InconsistentNaming
17+
18+
internal static class NativeMethods
19+
{
20+
// ReSharper disable InconsistentNaming
21+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
22+
[return: MarshalAs(UnmanagedType.Bool)]
23+
public static extern bool PostMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, HookKeyMsgData lParam);
24+
// ReSharper restore InconsistentNaming
25+
}
2126

2227
[Flags]
2328
public enum ModifierKeys
@@ -40,18 +45,35 @@ public enum ModifierKeys
4045
protected class KeyNotificationEntry
4146
: IEquatable<KeyNotificationEntry>
4247
{
43-
public IntPtr WindowHandle;
48+
private IntPtr WindowHandle;
4449
public Int32 KeyCode;
4550
public ModifierKeys ModifierKeys;
4651
public Boolean Block;
4752

53+
public KeyNotificationEntry(IntPtr h, Int32 k, ModifierKeys m, Boolean b)
54+
{
55+
WindowHandle = h;
56+
KeyCode = k;
57+
ModifierKeys = m;
58+
Block = b;
59+
}
4860
public bool Equals(KeyNotificationEntry obj)
4961
{
5062
return (WindowHandle == obj.WindowHandle &&
5163
KeyCode == obj.KeyCode &&
5264
ModifierKeys == obj.ModifierKeys &&
5365
Block == obj.Block);
5466
}
67+
68+
public IntPtr getWinHdl()
69+
{
70+
return WindowHandle;
71+
}
72+
73+
public void setWinHdl(IntPtr hdl)
74+
{
75+
WindowHandle = hdl;
76+
}
5577
}
5678

5779
public const string HookKeyMsgName = "HOOKKEYMSG-{56BE0940-34DA-11E1-B308-C6714824019B}";
@@ -62,9 +84,12 @@ public static Int32 HookKeyMsg
6284
{
6385
if (_hookKeyMsg == 0)
6486
{
65-
_hookKeyMsg = Win32.RegisterWindowMessage(HookKeyMsgName).ToInt32();
66-
if (_hookKeyMsg == 0)
67-
throw new Win32Exception(Marshal.GetLastWin32Error());
87+
_hookKeyMsg = Win32.NativeMethods.RegisterWindowMessage(HookKeyMsgName).ToInt32();
88+
if (_hookKeyMsg == 0)
89+
{
90+
logger.Log.WarnFormat("_hookKeyMsg == 0");
91+
throw new System.InvalidOperationException("_hookKeyMsg == 0");
92+
}
6893
}
6994
return _hookKeyMsg;
7095
}
@@ -104,7 +129,7 @@ private static void SetHook()
104129
var curProcess = Process.GetCurrentProcess();
105130
var curModule = curProcess.MainModule;
106131

107-
var hook = Win32.SetWindowsHookEx(Win32.WH_KEYBOARD_LL, LowLevelKeyboardProcStaticDelegate, Win32.GetModuleHandle(curModule.ModuleName), 0);
132+
var hook = Win32.NativeMethods.SetWindowsHookEx(Win32.WH_KEYBOARD_LL, LowLevelKeyboardProcStaticDelegate, Win32.NativeMethods.GetModuleHandle(curModule.ModuleName), 0);
108133
if (hook == IntPtr.Zero)
109134
throw new Win32Exception(Marshal.GetLastWin32Error());
110135

@@ -115,7 +140,7 @@ private static void UnsetHook()
115140
{
116141
if (_hook == IntPtr.Zero) return;
117142

118-
Win32.UnhookWindowsHookEx(_hook);
143+
Win32.NativeMethods.UnhookWindowsHookEx(_hook);
119144
_hook = IntPtr.Zero;
120145
}
121146

@@ -139,7 +164,7 @@ private static IntPtr LowLevelKeyboardProc(Int32 nCode, IntPtr wParam, Win32.KBD
139164

140165
if (result != 0) return new IntPtr(result);
141166

142-
return Win32.CallNextHookEx(_hook, nCode, wParam, lParam);
167+
return Win32.NativeMethods.CallNextHookEx(_hook, nCode, wParam, lParam);
143168
}
144169

145170
private static int OnKey(Int32 msg, Win32.KBDLLHOOKSTRUCT key)
@@ -152,7 +177,7 @@ private static int OnKey(Int32 msg, Win32.KBDLLHOOKSTRUCT key)
152177
// Mainly when the station is unlocked, or after an admin password is asked
153178
try
154179
{
155-
if (GetFocusWindow() == notificationEntry.WindowHandle && notificationEntry.KeyCode == key.vkCode)
180+
if (GetFocusWindow() == notificationEntry.getWinHdl() && notificationEntry.KeyCode == key.vkCode)
156181
{
157182
var modifierKeys = GetModifierKeyState();
158183
if (!ModifierKeysMatch(notificationEntry.ModifierKeys, modifierKeys)) continue;
@@ -165,7 +190,7 @@ private static int OnKey(Int32 msg, Win32.KBDLLHOOKSTRUCT key)
165190
WasBlocked = notificationEntry.Block,
166191
};
167192

168-
if (!PostMessage(notificationEntry.WindowHandle, HookKeyMsg, wParam, lParam))
193+
if (!NativeMethods.PostMessage(notificationEntry.getWinHdl(), HookKeyMsg, wParam, lParam))
169194
throw new Win32Exception(Marshal.GetLastWin32Error());
170195

171196
if (notificationEntry.Block) result = 1;
@@ -185,12 +210,12 @@ private static int OnKey(Int32 msg, Win32.KBDLLHOOKSTRUCT key)
185210
private static IntPtr GetFocusWindow()
186211
{
187212
var guiThreadInfo = new Win32.GUITHREADINFO();
188-
if (!Win32.GetGUIThreadInfo(0, guiThreadInfo))
213+
if (!Win32.NativeMethods.GetGUIThreadInfo(0, guiThreadInfo))
189214
{
190215
var except = Marshal.GetLastWin32Error();
191216
throw new Win32Exception(except);
192217
}
193-
return Win32.GetAncestor(guiThreadInfo.hwndFocus, Win32.GA_ROOT);
218+
return Win32.NativeMethods.GetAncestor(guiThreadInfo.getFocus(), Win32.GA_ROOT);
194219
}
195220

196221
protected static Dictionary<Int32, ModifierKeys> ModifierKeyTable = new Dictionary<Int32, ModifierKeys>
@@ -214,7 +239,7 @@ public static ModifierKeys GetModifierKeyState()
214239

215240
foreach (KeyValuePair<Int32, ModifierKeys> pair in ModifierKeyTable)
216241
{
217-
if ((Win32.GetAsyncKeyState(pair.Key) & Win32.KEYSTATE_PRESSED) != 0) modifierKeyState |= pair.Value;
242+
if ((Win32.NativeMethods.GetAsyncKeyState(pair.Key) & Win32.KEYSTATE_PRESSED) != 0) modifierKeyState |= pair.Value;
218243
}
219244

220245
if ((modifierKeyState & ModifierKeys.LeftWin) != 0) modifierKeyState |= ModifierKeys.Win;
@@ -239,13 +264,7 @@ public static void RequestKeyNotification(IntPtr windowHandle, Int32 keyCode, Bo
239264

240265
public static void RequestKeyNotification(IntPtr windowHandle, Int32 keyCode, ModifierKeys modifierKeys = ModifierKeys.None, Boolean block = false)
241266
{
242-
var newNotificationEntry = new KeyNotificationEntry
243-
{
244-
WindowHandle = windowHandle,
245-
KeyCode = keyCode,
246-
ModifierKeys = modifierKeys,
247-
Block = block,
248-
};
267+
var newNotificationEntry = new KeyNotificationEntry(windowHandle, keyCode, modifierKeys, block);
249268

250269
foreach (var notificationEntry in NotificationEntries)
251270
if (notificationEntry == newNotificationEntry) return;
@@ -260,15 +279,9 @@ public static void CancelKeyNotification(IntPtr windowHandle, Int32 keyCode, Boo
260279

261280
public static void CancelKeyNotification(IntPtr windowHandle, Int32 keyCode, ModifierKeys modifierKeys = ModifierKeys.None, Boolean block = false)
262281
{
263-
var notificationEntry = new KeyNotificationEntry
264-
{
265-
WindowHandle = windowHandle,
266-
KeyCode = keyCode,
267-
ModifierKeys = modifierKeys,
268-
Block = block,
269-
};
270-
271-
NotificationEntries.Remove(notificationEntry);
282+
var notificationEntry = new KeyNotificationEntry(windowHandle, keyCode, modifierKeys, block);
283+
284+
NotificationEntries.Remove(notificationEntry);
272285
}
273286
}
274287
}

SharedLibraryNG/SharedLibraryNG.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
5555
</PropertyGroup>
5656
<ItemGroup>
57+
<Reference Include="log4net">
58+
<HintPath>..\mRemoteV1\References\log4net.dll</HintPath>
59+
</Reference>
5760
<Reference Include="System" />
5861
<Reference Include="System.Data" />
5962
<Reference Include="System.Windows.Forms" />
@@ -64,6 +67,7 @@
6467
<SubType>Component</SubType>
6568
</Compile>
6669
<Compile Include="KeyboardHook.cs" />
70+
<Compile Include="logger.cs" />
6771
<Compile Include="Properties\AssemblyInfo.cs" />
6872
<Compile Include="Win32.cs" />
6973
</ItemGroup>

SharedLibraryNG/Win32.cs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,34 @@ namespace SharedLibraryNG
77
public static class Win32
88
{
99
#region Functions
10+
internal static class NativeMethods
11+
{
12+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
13+
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, Int32 dwThreadId);
1014

11-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
12-
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, Int32 dwThreadId);
13-
14-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
15-
[return: MarshalAs(UnmanagedType.Bool)]
16-
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
17-
18-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
19-
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, KBDLLHOOKSTRUCT lParam);
15+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
16+
[return: MarshalAs(UnmanagedType.Bool)]
17+
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
2018

21-
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
22-
public static extern IntPtr GetModuleHandle(string lpModuleName);
19+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
20+
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, KBDLLHOOKSTRUCT lParam);
2321

24-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
25-
public static extern IntPtr RegisterWindowMessage(string lpString);
22+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
23+
public static extern IntPtr GetModuleHandle(string lpModuleName);
2624

27-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
28-
[return: MarshalAs(UnmanagedType.Bool)]
29-
public static extern bool GetGUIThreadInfo(Int32 idThread, GUITHREADINFO lpgui);
25+
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
26+
public static extern IntPtr RegisterWindowMessage(string lpString);
3027

31-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
32-
public static extern IntPtr GetAncestor(IntPtr hwnd, UInt32 gaFlags);
28+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
29+
[return: MarshalAs(UnmanagedType.Bool)]
30+
public static extern bool GetGUIThreadInfo(Int32 idThread, GUITHREADINFO lpgui);
3331

34-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
35-
public static extern Int16 GetAsyncKeyState(Int32 vKey);
32+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
33+
public static extern IntPtr GetAncestor(IntPtr hwnd, UInt32 gaFlags);
3634

35+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
36+
public static extern Int16 GetAsyncKeyState(Int32 vKey);
37+
}
3738
#endregion
3839

3940
#region Delegates
@@ -51,7 +52,7 @@ public class KBDLLHOOKSTRUCT
5152
public Int32 scanCode;
5253
public Int32 flags;
5354
public Int32 time;
54-
public IntPtr dwExtraInfo;
55+
private IntPtr dwExtraInfo;
5556
};
5657

5758
[StructLayout(LayoutKind.Sequential)]
@@ -73,13 +74,18 @@ public GUITHREADINFO()
7374

7475
public Int32 cbSize;
7576
public Int32 flags;
76-
public IntPtr hwndActive;
77-
public IntPtr hwndFocus;
78-
public IntPtr hwndCapture;
79-
public IntPtr hwndMenuOwner;
80-
public IntPtr hwndMoveSize;
81-
public IntPtr hwndCaret;
77+
private IntPtr hwndActive;
78+
private IntPtr hwndFocus;
79+
private IntPtr hwndCapture;
80+
private IntPtr hwndMenuOwner;
81+
private IntPtr hwndMoveSize;
82+
private IntPtr hwndCaret;
8283
public RECT rcCaret;
84+
85+
public IntPtr getFocus()
86+
{
87+
return hwndFocus;
88+
}
8389
}
8490

8591
#endregion

SharedLibraryNG/logger.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using log4net;
3+
using System.Reflection;
4+
5+
public class logger
6+
{
7+
public static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
8+
9+
public logger()
10+
{
11+
12+
}
13+
}

mRemoteV1/App/App.Native.vb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ Namespace App
77
End Sub
88

99
#Region "Functions"
10-
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
10+
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
1111
Public Shared Function AppendMenu(ByVal hMenu As IntPtr, ByVal uFlags As Int32, ByVal uIDNewItem As IntPtr, ByVal lpNewItem As String) As Boolean
1212
End Function
1313

1414
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
1515
Public Shared Function CreatePopupMenu() As IntPtr
1616
End Function
1717

18-
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
18+
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
1919
Public Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
2020
End Function
2121

@@ -27,7 +27,7 @@ Namespace App
2727
Public Shared Function GetSystemMenu(ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
2828
End Function
2929

30-
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
30+
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
3131
Public Shared Function InsertMenu(ByVal hMenu As IntPtr, ByVal uPosition As Integer, ByVal uFlags As Integer, ByVal uIDNewItem As IntPtr, ByVal lpNewItem As String) As Boolean
3232
End Function
3333

@@ -79,8 +79,8 @@ Namespace App
7979
#Region "Structures"
8080
<StructLayout(LayoutKind.Sequential)>
8181
Public Structure WINDOWPOS
82-
Public hwnd As IntPtr
83-
Public hwndInsertAfter As IntPtr
82+
Private hwnd As IntPtr
83+
Private hwndInsertAfter As IntPtr
8484
Public x As Integer
8585
Public y As Integer
8686
Public cx As Integer

mRemoteV1/Tools/EnvironmentInfo.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ Namespace Tools
2727

2828
Protected Class Win32
2929
' ReSharper disable InconsistentNaming
30-
<DllImport("kernel32", CharSet:=CharSet.Auto, SetLastError:=True)>
30+
<DllImport("kernel32", CharSet:=CharSet.Unicode, SetLastError:=True)>
3131
Public Shared Function LoadLibrary(<[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpFileName As String) As IntPtr
3232
End Function
3333

34-
<DllImport("kernel32", ExactSpelling:=True, CharSet:=CharSet.Ansi, SetLastError:=True)>
34+
<DllImport("kernel32", ExactSpelling:=True, CharSet:=CharSet.Unicode, SetLastError:=True)>
3535
Public Shared Function GetProcAddress(<[In]()> ByVal hModule As IntPtr, <[In](), MarshalAs(UnmanagedType.LPStr)> ByVal lpProcName As String) As IntPtr
3636
End Function
3737

mRemoteV1/Tools/ProcessController.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ Namespace Tools
141141
' ReSharper restore ClassNeverInstantiated.Local
142142
' ReSharper disable InconsistentNaming
143143
' ReSharper disable UnusedMethodReturnValue.Local
144-
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
144+
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
145145
Public Shared Sub GetClassName(ByVal hWnd As IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer)
146146
End Sub
147147

148148
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
149149
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
150150
End Function
151151

152-
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
152+
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
153153
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As System.Text.StringBuilder) As IntPtr
154154
End Function
155155

mRemoteV1/Tools/Tools.Misc.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Namespace Tools
2020
Public szTypeName As String
2121
End Structure
2222

23-
<DllImport("shell32.dll")> _
23+
<DllImport("shell32.dll", CharSet:=CharSet.Unicode)>
2424
Private Shared Function SHGetFileInfo(ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As IntPtr
2525
End Function
2626

mRemoteV1/mRemoteV1.vbproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
<DebugType>full</DebugType>
9696
<PlatformTarget>x86</PlatformTarget>
9797
<DefineConstants>PORTABLE</DefineConstants>
98-
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
98+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
9999
<Prefer32Bit>false</Prefer32Bit>
100100
<UseVSHostingProcess>true</UseVSHostingProcess>
101101
</PropertyGroup>

0 commit comments

Comments
 (0)