M
Mythran
I can draw onto the console window where I want using a mixture of API calls
and the System.Drawing namespace (.Net 1.1). I am trying to install hooks
for the window to catch a resize or another message so that when the console
window gets redrawn, I can repaint what I need to on this window. So far,
no luck.
First I install the hook by calling the SetWindowsHookEx passing the type
WH_CALLWNDPROC, a delegate for the HOOKPROC param, null for the third param,
and the current thread id. For the thread Id, I have also tried using
GetWindowThreadProcessId result as the thread id parameter, still nothing.
The SetWindowsHookEx api returns success, so I know that there were no
errors from calling the API, but when the console application runs, the
delegate does not get called. The following is part of what I have...and it
doesn't work
// --------------------
using System;
using System.Runtime.InteropServices;
namespace Tests.Applications.ConsoleHook.UI
{
/// <summary>
/// Sample of trying to hook into the Console's window.
/// </summary>
public class InternalMain
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(
HookType idHook,
HOOKPROC lpfn,
IntPtr hMod,
int dwThreadId
);
[DllImport("user32.dll", SetLastError = true)]
public static extern int CallNextHookEx(
IntPtr hhk,
int nCode,
IntPtr wParam,
IntPtr lParam
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnhookWindowsHookEx(
IntPtr hhk
);
public delegate int HOOKPROC(
int nCode,
IntPtr wParam,
IntPtr lParam
);
public enum HookType : int
{
WH_MSGFILTER = -1,
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
private HOOKPROC mHookProcFunc = null;
private IntPtr mHHook = IntPtr.Zero;
public static void Main()
{
InternalMain main = new InternalMain();
main.Start();
}
public void Start()
{
Console.WriteLine("Installing hook.");
Install();
Console.WriteLine("Accepting input: ");
Console.ReadLine();
Console.WriteLine("Looping for 5 seconds.");
DateTime endTime = DateTime.Now.AddSeconds(5);
while (DateTime.Now < endTime) {
// Sleep.
System.Threading.Thread.Sleep(100);
}
Console.WriteLine("Uninstalling hook.");
UnhookWindowsHookEx(mHHook);
}
private void Install()
{
mHookProcFunc = new HOOKPROC(HookProc);
mHHook = SetWindowsHookEx(
HookType.WH_CALLWNDPROC,
mHookProcFunc,
IntPtr.Zero,
AppDomain.GetCurrentThreadId()
);
if (mHHook == IntPtr.Zero) {
Marshal.ThrowExceptionForHR(
Marshal.GetHRForLastWin32Error()
);
}
}
public int HookProc(int Code, IntPtr wParam, IntPtr lParam)
{
if (Code >= 0) {
// Hook-specific code.
Console.WriteLine("HookProc(): ");
}
return CallNextHookEx(
mHHook,
Code,
wParam,
lParam
);
}
}
}
// ---------------------
Thanks
Mythran
and the System.Drawing namespace (.Net 1.1). I am trying to install hooks
for the window to catch a resize or another message so that when the console
window gets redrawn, I can repaint what I need to on this window. So far,
no luck.
First I install the hook by calling the SetWindowsHookEx passing the type
WH_CALLWNDPROC, a delegate for the HOOKPROC param, null for the third param,
and the current thread id. For the thread Id, I have also tried using
GetWindowThreadProcessId result as the thread id parameter, still nothing.
The SetWindowsHookEx api returns success, so I know that there were no
errors from calling the API, but when the console application runs, the
delegate does not get called. The following is part of what I have...and it
doesn't work
// --------------------
using System;
using System.Runtime.InteropServices;
namespace Tests.Applications.ConsoleHook.UI
{
/// <summary>
/// Sample of trying to hook into the Console's window.
/// </summary>
public class InternalMain
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(
HookType idHook,
HOOKPROC lpfn,
IntPtr hMod,
int dwThreadId
);
[DllImport("user32.dll", SetLastError = true)]
public static extern int CallNextHookEx(
IntPtr hhk,
int nCode,
IntPtr wParam,
IntPtr lParam
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnhookWindowsHookEx(
IntPtr hhk
);
public delegate int HOOKPROC(
int nCode,
IntPtr wParam,
IntPtr lParam
);
public enum HookType : int
{
WH_MSGFILTER = -1,
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
private HOOKPROC mHookProcFunc = null;
private IntPtr mHHook = IntPtr.Zero;
public static void Main()
{
InternalMain main = new InternalMain();
main.Start();
}
public void Start()
{
Console.WriteLine("Installing hook.");
Install();
Console.WriteLine("Accepting input: ");
Console.ReadLine();
Console.WriteLine("Looping for 5 seconds.");
DateTime endTime = DateTime.Now.AddSeconds(5);
while (DateTime.Now < endTime) {
// Sleep.
System.Threading.Thread.Sleep(100);
}
Console.WriteLine("Uninstalling hook.");
UnhookWindowsHookEx(mHHook);
}
private void Install()
{
mHookProcFunc = new HOOKPROC(HookProc);
mHHook = SetWindowsHookEx(
HookType.WH_CALLWNDPROC,
mHookProcFunc,
IntPtr.Zero,
AppDomain.GetCurrentThreadId()
);
if (mHHook == IntPtr.Zero) {
Marshal.ThrowExceptionForHR(
Marshal.GetHRForLastWin32Error()
);
}
}
public int HookProc(int Code, IntPtr wParam, IntPtr lParam)
{
if (Code >= 0) {
// Hook-specific code.
Console.WriteLine("HookProc(): ");
}
return CallNextHookEx(
mHHook,
Code,
wParam,
lParam
);
}
}
}
// ---------------------
Thanks
Mythran