473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Console Window Hooks

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

Apr 3 '06 #1
0 4624

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
5
by: Mullin Yu | last post by:
i want to build an application of both gui and batch interface by using windows application project. i check either passing any args or not. if no, then open the gui application. if yes, use the...
8
by: Mészáros Tamás | last post by:
Hi all, how can I set an app's main window to visible from an other application? My problem is, that I know only the handle of the other app's main process, because the application's main window...
6
by: Mythran | last post by:
Is it possible to attach Windows WndProc hooks into a Console application window? Thanks, Mythran
2
by: Lunchtimemama | last post by:
I am trying to modify another application's window (Google Talk) to insert some UI of my own. I think the best method would be to install a remote local hook in gtalk's thread that intercepts the...
1
by: andyblum | last post by:
>From my reading it is not possible to create Global Hooks for WIndow Creation, Destroyed and Moved events in C#. I am just unclear about the difference between a global and locals hook. Is...
10
by: Stephany Young | last post by:
When one uses the System.Diagnostics.Process.Start method to launch a common or garden Console application, one can set the WindowStyle property of the StartInfo object to ProcessWindowStyle.Hidden...
3
by: TC | last post by:
I'm trying to debug a console application, but I can't see the console output. I've seen many references which say that console output is supposed to appear on the Output window when the...
5
by: =?Utf-8?B?SmFtZXMgV29uZw==?= | last post by:
Dear all, I'd like to know if there is any method to minimize command mode window when a console program is running. In my case, there are several console programs which run periodically in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.