Skip to content

Commit 91d2525

Browse files
committed
2nd Command-line "useinterceptor" loads interceptor driver
App cleanup frees windows console in debug mode
1 parent 431a85c commit 91d2525

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

Tube/Tube.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,18 @@ static void Main(string[] args)
7777
TrayApplicationContext<ViewMain> trayApplicationContext = new TrayApplicationContext<ViewMain>();
7878
MainForm = (ViewMain) trayApplicationContext.MainForm;
7979

80-
// Careful. Does bad things if the event handlers block the driver.
81-
// TODO: Make Interceptor driver use toggle-able in the GUI
82-
LoadInterceptorDriver();
80+
// TODO: Make Interceptor driver use a GUI toggle
81+
bool useInterceptor = (args.Length >= 2 && args[1].ToLower().Equals("useinterceptor"));
82+
InterceptorDriverInput = new Input();
83+
if (useInterceptor)
84+
{
85+
LOGGER.Info("Loading interceptor driver..." );
86+
LoadInterceptorDriver();
87+
if (Tube.InterceptorDriverInput.IsLoaded)
88+
{
89+
LOGGER.Info("Interceptor driver loaded and will be used instead of SendInput() and hooks");
90+
}
91+
}
8392

8493
// Native keyboard and mouse hook initialization
8594
KeyInterceptor.Initialize(KeyboardHandler.HookCallback);
@@ -99,17 +108,26 @@ static void Main(string[] args)
99108

100109
ProcessFileArg(fileName);
101110

102-
LOGGER.Debug("Entering Application.Run()...");
111+
LOGGER.Debug("Entering Application.Run()");
103112
Application.Run(trayApplicationContext);
104-
LOGGER.Debug("...returned from Application.Run().");
113+
LOGGER.Debug("Returned from Application.Run().");
105114
}
106115
finally
107116
{
117+
LOGGER.Info("Starting application exit cleanup...");
118+
108119
// Native class de-initialization
109120
MouseInterceptor.Unhook();
110121
KeyInterceptor.Cleanup();
111122
CmdFileReader.Dispose();
112123
UnloadInterceptorDriver();
124+
125+
LOGGER.Info("... end application exit cleanup");
126+
127+
if (LOGGER.IsDebugEnabled)
128+
{
129+
WinConsole.Free();
130+
}
113131
}
114132

115133
if (WriteOnExit)
@@ -122,36 +140,29 @@ static void Main(string[] args)
122140

123141
private static void LoadInterceptorDriver()
124142
{
125-
InterceptorDriverInput = new Input
143+
try
126144
{
127-
KeyboardFilterMode = KeyboardFilterMode.All,
128-
129145
// For mouse lock
130-
MouseFilterMode = MouseFilterMode.MouseMove
131-
};
146+
InterceptorDriverInput.MouseFilterMode = MouseFilterMode.MouseMove;
132147

133-
// Probably not needed unless we need the filter driver for both input and output
134-
// IntercepterDriverWrapper.OnKeyPressed += IntercepterDriverWrapper_OnKeyPressed;
148+
// Clicking in debug console with this enabled may lock all inputs and require reboot!
149+
InterceptorDriverInput.OnMousePressed += IntercepterDriverWrapper_OnMousePressed;
135150

136-
// Clicking in console with this enabled is very bad.
137-
InterceptorDriverInput.OnMousePressed += IntercepterDriverWrapper_OnMousePressed;
138-
139-
try
140-
{
141151
InterceptorDriverInput.Load();
142152
}
143153
catch (DllNotFoundException notFoundException)
144154
{
145-
LOGGER.Info("Unable to load keyboard filter driver DLL. Make sure DLL is in application directory: " + notFoundException);
155+
LOGGER.Warn("Unable to load keyboard filter driver DLL. Make sure DLL is in application directory: " + notFoundException);
146156
return;
147157
}
148158
catch (Exception e)
149159
{
160+
// Code that simulates input should always check filter driver state
161+
// if Tube.InterceptorDriverInput.IsLoaded use it, otherwise use
162+
// SendInput
150163
LOGGER.Warn("Failed to load keyboard filter driver. SendInput will be used: " + e);
151164
return;
152165
}
153-
154-
LOGGER.Info("Keyboard filter driver loaded and will be used instead of SendInput()!");
155166
}
156167

157168
private static void UnloadInterceptorDriver()

Tube/native/WinConsole.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public static void Initialize(bool alwaysCreateNewConsole = true)
2424
}
2525
}
2626

27+
public static void Free()
28+
{
29+
Console.Out?.Close();
30+
Console.In?.Close();
31+
FreeConsole();
32+
}
33+
2734
private static void InitializeOutStream()
2835
{
2936
var fs = CreateFileStream("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, FileAccess.Write);
@@ -44,8 +51,11 @@ private static void InitializeInStream()
4451
}
4552
}
4653

47-
private static FileStream CreateFileStream(string name, uint win32DesiredAccess, uint win32ShareMode,
48-
FileAccess dotNetFileAccess)
54+
private static FileStream CreateFileStream(
55+
string name,
56+
uint win32DesiredAccess,
57+
uint win32ShareMode,
58+
FileAccess dotNetFileAccess)
4959
{
5060
var file = new SafeFileHandle(CreateFileW(name, win32DesiredAccess, win32ShareMode, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero), true);
5161
if (!file.IsInvalid)
@@ -64,6 +74,13 @@ private static FileStream CreateFileStream(string name, uint win32DesiredAccess,
6474
CallingConvention = CallingConvention.StdCall)]
6575
private static extern int AllocConsole();
6676

77+
[DllImport("kernel32.dll",
78+
EntryPoint = "FreeConsole",
79+
SetLastError = true,
80+
CharSet = CharSet.Auto,
81+
CallingConvention = CallingConvention.StdCall)]
82+
private static extern bool FreeConsole();
83+
6784
[DllImport("kernel32.dll",
6885
EntryPoint = "AttachConsole",
6986
SetLastError = true,
@@ -76,6 +93,7 @@ private static FileStream CreateFileStream(string name, uint win32DesiredAccess,
7693
SetLastError = true,
7794
CharSet = CharSet.Auto,
7895
CallingConvention = CallingConvention.StdCall)]
96+
7997
private static extern IntPtr CreateFileW(
8098
string lpFileName,
8199
UInt32 dwDesiredAccess,

0 commit comments

Comments
 (0)