Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

Not getting all window messages from IMessageFilter?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
johnf - 16 May 2008 19:21 GMT
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());
       }
   }
Peter Duniho - 16 May 2008 19:42 GMT
> Trying to get a see windows messages using IMessageFilter interface  
> however
[quoted text clipped - 4 lines]
> 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
johnf - 16 May 2008 20:00 GMT
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/library/ms704147(VS.85).aspx
It suggests for console / service apps it is possible to get these msgs.
-John

> > Trying to get a see windows messages using IMessageFilter interface  
> > however
[quoted text clipped - 24 lines]
>
> Pete
Peter Duniho - 16 May 2008 20:18 GMT
> 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/library/ms704147(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
johnf - 16 May 2008 20:38 GMT
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...

> > Hi Pete,
> > Thanks for the response, however ultimately I am incorporating this into  
[quoted text clipped - 19 lines]
>
> Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.