By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,016 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,016 IT Pros & Developers. It's quick & easy.

Not getting all window messages from IMessageFilter?

P: n/a
Trying to get a see windows messages using IMessageFilter interface however
it does not seem to get all window messages. Specifically I am looking for
the WM_POWERBROADCAST. I override wndproc which shows the msg successfully
but nothing shows up when using IMessageFilter. Am i misunderstanding
something? Here is sample code:
public class MyFilter : IMessageFilter
{

const int WM_POWERBROADCAST = 0x0218;
public bool PreFilterMessage(ref Message m)
{
if m.Msg == WM_POWERBROADCAST)
{
Console.WriteLine("Power Broadcast recieved.");
}
return false;
}
}
class MainForm : Form
{

public MainForm() //MainForm's constructor
{
}
public static void Main()
{

Application.AddMessageFilter(new MyFilter());
Application.Run(new MainForm());
}
}

Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Fri, 16 May 2008 11:21:27 -0700, johnf
<jo***@discussions.microsoft.comwrote:
Trying to get a see windows messages using IMessageFilter interface
however
it does not seem to get all window messages. Specifically I am looking
for
the WM_POWERBROADCAST. I override wndproc which shows the msg
successfully
but nothing shows up when using IMessageFilter. Am i misunderstanding
something?
It's been awhile since I did lots of Win32 stuff, so this is just a
guess. But WM_POWERBROADCAST is probably sent by the system using
HWND_BROADCAST and SendMessage(), bypassing the message queue. At the
same time, message filtering is as I understand it done through the
message dispatch system, which would apply only to messages going through
the thread's message queue.

If that's all correct, that would explain why you aren't seeing that
particular message in the IMessageFilter implementation.

It's probably better to use the WndProc() override anyway. Using the
message filtering is a pretty broad brush, as you wind up looking at
_every_ single message that goes through the queue. The message you're
looking for will never be seen by most of the window instances in your
application (it's only sent to top-level windows) so even if filtering did
work, it's a waste of time. I think it's better to just watch for it in a
top-level window you have in your application, using WndProc().

Pete
Jun 27 '08 #2

P: n/a
Hi Pete,
Thanks for the response, however ultimately I am incorporating this into a
service, so it wont be possible to override wndproc. According to this doc
(about halfway down)
http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx
It suggests for console / service apps it is possible to get these msgs.
-John

"Peter Duniho" wrote:
On Fri, 16 May 2008 11:21:27 -0700, johnf
<jo***@discussions.microsoft.comwrote:
Trying to get a see windows messages using IMessageFilter interface
however
it does not seem to get all window messages. Specifically I am looking
for
the WM_POWERBROADCAST. I override wndproc which shows the msg
successfully
but nothing shows up when using IMessageFilter. Am i misunderstanding
something?

It's been awhile since I did lots of Win32 stuff, so this is just a
guess. But WM_POWERBROADCAST is probably sent by the system using
HWND_BROADCAST and SendMessage(), bypassing the message queue. At the
same time, message filtering is as I understand it done through the
message dispatch system, which would apply only to messages going through
the thread's message queue.

If that's all correct, that would explain why you aren't seeing that
particular message in the IMessageFilter implementation.

It's probably better to use the WndProc() override anyway. Using the
message filtering is a pretty broad brush, as you wind up looking at
_every_ single message that goes through the queue. The message you're
looking for will never be seen by most of the window instances in your
application (it's only sent to top-level windows) so even if filtering did
work, it's a waste of time. I think it's better to just watch for it in a
top-level window you have in your application, using WndProc().

Pete
Jun 27 '08 #3

P: n/a
On Fri, 16 May 2008 12:00:02 -0700, johnf
<jo***@discussions.microsoft.comwrote:
Hi Pete,
Thanks for the response, however ultimately I am incorporating this into
a
service, so it wont be possible to override wndproc.
Win32 services can create a message-only window. At the very least, you
should be able to accomplish the same in your service via p/invoke. I
don't know of any .NET support for that, but it might exist.
According to this doc
(about halfway down)
http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx
It suggests for console / service apps it is possible to get these msgs.
Well, that page is for Windows Mobile (CE). It's possible that the
technique works on Windows Mobile but not regular Windows. What platform
are you testing this on? It's also possible that the documentation is
simply incorrect. That could either be an error in the documentation, or
some bug in .NET.

Sorry I don't have anything more detailed.

Pete
Jun 27 '08 #4

P: n/a
Actually the link is for Mobile PC's (laptops?) not CE. The topic covers
Power Management in Windows XP and Vista. Thanks for the input Pete, if
anyone has anything else, greatly appreciated...

"Peter Duniho" wrote:
On Fri, 16 May 2008 12:00:02 -0700, johnf
<jo***@discussions.microsoft.comwrote:
Hi Pete,
Thanks for the response, however ultimately I am incorporating this into
a
service, so it wont be possible to override wndproc.

Win32 services can create a message-only window. At the very least, you
should be able to accomplish the same in your service via p/invoke. I
don't know of any .NET support for that, but it might exist.
According to this doc
(about halfway down)
http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx
It suggests for console / service apps it is possible to get these msgs.

Well, that page is for Windows Mobile (CE). It's possible that the
technique works on Windows Mobile but not regular Windows. What platform
are you testing this on? It's also possible that the documentation is
simply incorrect. That could either be an error in the documentation, or
some bug in .NET.

Sorry I don't have anything more detailed.

Pete
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.