Skip to content

Commit a8c3de7

Browse files
committed
Refactoring for native methods
1 parent 593706b commit a8c3de7

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

src/MacroRecoderCsScript/NativeMethods.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ internal static class NativeMethods
2222
[DllImport( "user32.dll", SetLastError = true )]
2323
internal static extern uint SendInput( uint inputNum, Input[] inputs, int inputStructSize );
2424

25-
[DllImport( "Kernel32.dll" )]
25+
[DllImport( "Kernel32.dll", SetLastError = true )]
2626
internal static extern bool AttachConsole( int processId );
2727

28-
[DllImport( "Kernel32.dll" )]
28+
[DllImport( "Kernel32.dll", SetLastError = true )]
2929
internal static extern bool FreeConsole();
3030
}
3131
}

src/MacroRecoderCsScript/SendInputWrapper.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public static void SendMouseInput( MouseInput[] mouseInput )
1919
result = NativeMethods.SendInput( ( uint ) input.Length, input, Marshal.SizeOf( input[ 0 ] ) );
2020

2121
if( result == NativeMethods.SEND_INPUT_FAILED ) {
22-
int errorCode = Marshal.GetLastWin32Error();
23-
throw new Win32Exception( errorCode );
22+
throw new Win32Exception();
2423
}
2524
}
2625

@@ -38,8 +37,7 @@ public static void SendKeyInput( KeyInput[] keyInput )
3837
result = NativeMethods.SendInput( ( uint ) input.Length, input, Marshal.SizeOf( input[ 0 ] ) );
3938

4039
if( result == NativeMethods.SEND_INPUT_FAILED ) {
41-
int errorCode = Marshal.GetLastWin32Error();
42-
throw new Win32Exception( errorCode );
40+
throw new Win32Exception();
4341
}
4442
}
4543
}

src/MacroRecoderCsScript/UserInputHook.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public void RegisterKeyHook()
5353
KeyHookHandle = NativeMethods.SetWindowsHookEx( ( int )WindowsHookID.KeyBoardLowLevel, KeyHookCallback, module, 0 );
5454

5555
if( KeyHookHandle == IntPtr.Zero ) {
56-
int errorCode = Marshal.GetLastWin32Error();
57-
throw new Win32Exception( errorCode );
56+
throw new Win32Exception();
5857
}
5958
}
6059

@@ -64,8 +63,7 @@ public void RegisterMouseHook()
6463
MouseHookHandle = NativeMethods.SetWindowsHookEx( ( int )WindowsHookID.MouseLowLevel, MouseHookCallback, module, 0 );
6564

6665
if( MouseHookHandle == IntPtr.Zero ) {
67-
int errorCode = Marshal.GetLastWin32Error();
68-
throw new Win32Exception( errorCode );
66+
throw new Win32Exception();
6967
}
7068
}
7169

0 commit comments

Comments
 (0)