[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Intercepting WM_ACTIVATE message for any window (detecting activation with hooks/subclassing)

Posted on 2004-08-05
8
Medium Priority
?
4,099 Views
Last Modified: 2012-06-27
hello,

i currently need to detect when a certain application's window is activated and deactivated.  this is a 3rd party window, not one that is part of my vb.net project.  i have read a bit about hooks and subclassing, but i have not seen a concrete example of this in vb.net.  i would basically like to do what Spy++ does for message logging.  i can get the window's basic attributes like hWnd just fine.  i am just having problem's hooking into the messages.  i have successfully written code which intercepts messages from my own forms, but when i try to use a 3rd party window, i am unsuccessful.  the APIs i have been using are SetWindowLong and CallWindowProc.  please advise.

thanks,
jim
0
Comment
Question by:vanveghj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 3

Expert Comment

by:imu79
ID: 11755480
take a look at this:

http://www.codeproject.com/csharp/GlobalSystemHook.asp

It's in C# but you should be able to get an idea on how to setup system level hooks.

hope this helps..
Imran.
0
 

Author Comment

by:vanveghj
ID: 11759692
hi,

thanks for the link.  however, from the site, "Some hook types are not suited to this implementation of global hooks."  what i want is not supported.  he only shows keyboard and mouse hooks.  examples of those are all over.

if anyone has a better way to detect when windows with a certain title are created/activated, let me know.  and of course, i do not want to use polling.

thanks,
jim v.
0
 
LVL 3

Expert Comment

by:imu79
ID: 11773407
hmmm..it seems installing a system level hook is a bit more involved than mouse and keyboard hooks (thread level hooks). here is a link I found that explains (not in detail though) how to implement a system level hook:

http://www.macadamian.com/column/windowshooks.html

Also, check out this discussion:

http://tinyurl.com/6owes

(if you can't resolve the above, just search for 'ShellProc' in groups.google.com and look at the first message)

According to that discussion, you cannot implement system level hooks using VB .NET or .NET in general. It makes sense after reading that discussion. Also, although the article says that you need to have your filter function in an unmanaged C++ dll, I had read somewhere that you could also do away with implementing the filter function in vb 6 bas module and compiling it to a dll. Ofcourse, you would want to make the function as fast and concise as possible (and yes, absolutely error-free or it could bring down the whole system) considering that windows is going to inject the dll code into each of the running processes.

here's another general article about hooks written way back in 1993 in case you haven't stumbled upon it already:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_hooks32.asp?frame=true&hidetoc=true

you're right - there isn't much out there about system level hooks. I would really be interested in someone implement this in VB/VB .NET. Maybe I'll venture this if I get enough free time from my day job :(

hope this helps..
Imran.



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vanveghj
ID: 11790590
Imran,

thanks for your continued help.  i will read those sites within the next few days, too busy right now ;-).  i am now realizing i will need to write a dll in c++, but this is no problem.  hopefully i can piece it all together.  i will update you in a few days.  i will also try to post the code if i get it working.

thanks,
jim v.
0
 
LVL 3

Expert Comment

by:imu79
ID: 11792324
Jim,

a couple of other links that I think might be useful:

http://www.planet-source-code.com/vb/scripts/showcode.asp?txtCodeId=51403&lngWId=1
ttp://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=9153

both of these have source code on how to implement system level shell hooks in VB 6. I'm not sure if they work under VB .NET.

Also look at the documentation of the two API functions:
RegisterShellHookWindow
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/registershellhookwindow.asp

DeregisterShellHookWindow
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Hooks/HookReference/HookFunctions/DeregisterShellHookWindow.asp

I think those are the functions you'll basically need to setup the shell hook (yes, without ever using SetWindowsHookEx and UnhookWindowsHookEx)

I'm going to be trying that myself probably this weekend. I'll let you know.

hope this helps..
Imran.
0
 
LVL 3

Accepted Solution

by:
imu79 earned 2000 total points
ID: 11817448
Jim,

I think this works in .NET as well :)
It turned out to be much simpler than I thought. Here's the code:

' Shell Events Constants
Public Enum ShellEvents
        HSHELL_WINDOWCREATED = 1
        HSHELL_WINDOWDESTROYED = 2
        HSHELL_ACTIVATESHELLWINDOW = 3
        HSHELL_WINDOWACTIVATED = 4
        HSHELL_GETMINRECT = 5
        HSHELL_REDRAW = 6
        HSHELL_TASKMAN = 7
        HSHELL_LANGUAGE = 8
        HSHELL_ACCESSIBILITYSTATE = 11
End Enum

' API Declares
Public Declare Function RegisterWindowMessage Lib "user32.dll" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer
Public Declare Function DeregisterShellHookWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
Public Declare Function RegisterShellHookWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer

Imports System.Runtime.InteropServices

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private uMsgNotify As Integer

#Region " Windows Form Designer generated code "

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' This will register the ShellHook event messages between the shell and our application.
        ' The uMsgNotify is then used to communicate between the shell and our application whenever any
        ' of the shell events are fired such as an app starting up, shutting down, activating, minimizing, etc
        uMsgNotify = RegisterWindowMessage("SHELLHOOK")
        ' This basically registers our window to receive the shell events
        Call RegisterShellHookWindow(Me.Handle)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = uMsgNotify Then
            Select Case m.WParam.ToInt32
                Case ShellEvents.HSHELL_WINDOWACTIVATED
                    Debug.WriteLine("window activated")
            End Select
        End If
        MyBase.WndProc(m)
    End Sub

let me know if this helps..
Imran.
0
 

Author Comment

by:vanveghj
ID: 11817868
Imran,

WOW, THANK YOU SO MUCH!  i took 5 minutes and got this working in vb.net.  you have no idea how much i appreciate you taking the time to do this.  you're right, it does seem easier then i expected.  i am guessing others will find this code valuable too.

thanks again,
jim v.
0
 
LVL 3

Expert Comment

by:imu79
ID: 11819820
Happy to help :)
In the process, I learnt something new too :)
enjoy!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question