471,348 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Not getting all window messages from IMessageFilter?

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
4 3586
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
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
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
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.

Similar topics

3 posts views Thread by NeverLift | last post: by
18 posts views Thread by lawrence | last post: by
4 posts views Thread by KlassifiedBBS | last post: by
1 post views Thread by news.microsoft.com | last post: by
1 post views Thread by Ronak mishra | last post: by

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.