subclass other window

Posted on 2006-06-09
Last Modified: 2008-02-01
hi everyone..

I want to ask about subclassing.

I know that we can subclass our form to catch messages come to the form..(WM_XXX)

But I want to know if I can subclass other window..

Like this I mean:

think that there is a window on desktop..(poor  a vb form,  Form.exe)
I handle it's hwnd.

And then I write another vb project that has codes for subclassing windows..

If I change this line  

OldWindowProc = SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)

to this

OldWindowProc = SetWindowLong(OtherHWND, GWL_WNDPROC, AddressOf NewWindowProc)

and update NewWindowProc callback function like this..

Can I subclass other exe window form?  (I tried and I couldn't do.)

TO summarize;
 can I catch messages come to other windows ? And how can I do that?

Think WinSPY. locating icon to a window and shows information about that window..
So I want to do like this at the end, Locating an icon to a window and subclass it and show all message traffic with that window..(maybe "window messages monitor")

Need your help..
Question by:hbarisxx
LVL 15

Expert Comment

ID: 16868153
This code example intercepts the windows message to a listbox scrollbar determined by its window handle.  You will need to find out the handle of the control that you wish to manipulate.  You may need a program such as WinSight to find all the addresses that you need.

Option Explicit
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Declare Function GetSystemMenu Lib "user32" _
   (ByVal hwnd As Long, ByVal bRevert As Long) As Long

Declare Function AppendStringMenu Lib "user32" Alias _
   "AppendMenuA" (ByVal hMenu As Long, _
   ByVal wFlags As Long, ByVal wIDNewItem As Long, _
   ByVal lpNewItem As String) As Long

Declare Function EnableMenuItem Lib "user32" _
   (ByVal hMenu As Long, ByVal wIDEnableItem As Long, _
    ByVal wEnable As Long) As Long

Declare Function CallWindowProc Lib "user32" Alias _
   "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
   ByVal hwnd As Long, ByVal Msg As Long, _
   ByVal wParam As Long, ByVal lParam As Long) As Long

Declare Function SetWindowLong Lib "user32" Alias _
   "SetWindowLongA" (ByVal hwnd As Long, _
   ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Global glpPrevWndProc As Long           ' address of the original WindowProc
Global gHwnd As Long                   ' handle to the form
Public Const GWL_WNDPROC = (-4)        ' used in SetWindowLong to change address of the target window's windowproc

Public Const WM_USER = &H400           ' custom menu item command constants
Public Const SC_UDMENUITEM1 = WM_USER + 1
Public Const SC_UDMENUITEM2 = WM_USER + 2

Public Const MF_SEPARATOR = &H800&      ' menu interpretation flags
Public Const MF_STRING = &H0&
Public Const MF_BYCOMMAND = &H0&
Public Const MF_DISABLED = &H2&
Public Const MF_ENABLED = &H0&
Public Const MF_GRAYED = &H1&

Public Const SB_ENDSCROLL = 8
Public Const SB_BOTTOM = 7
Public Const SB_LINEDOWN = 1
Public Const SB_LINEUP = 0
Public Const SB_PAGEDOWN = 3
Public Const SB_PAGEUP = 2
Public Const SB_THUMBTRACK = 5
Public Const SB_TOP = 6
Public Const LB_ADDSTRING = &H180

Public Const WM_VSCROLL = &H115
Public Const WM_INITMENU = &H116
Private Const WM_SYSCOMMAND = &H112
Public Const WM_COMMAND = &H111
Public Const LBN_SELCHANGE = 1

Public Sub SubClass()
   '//store pointer to the address of the target window's original windproc
   glpPrevWndProc = SetWindowLong(gLBHwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnSubClass()
   '// reverse SubClass Proc and put back the address of the original windProc
   SetWindowLong gLBHwnd, GWL_WNDPROC, glpPrevWndProc
End Sub

Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   Select Case uMsg
      Case WM_VSCROLL:
         Select Case wParam
            Case SB_ENDSCROLL:
               Debug.Print "endscroll"
            Case SB_BOTTOM:
               Debug.Print "bottom"
            Case SB_LINEDOWN:
               Debug.Print "linedown"
            Case SB_LINEUP:
               Debug.Print "lineup"
            Case SB_PAGEDOWN:
               Debug.Print "pagedown"
            Case SB_PAGEUP:
               Debug.Print "pageup"
            Case SB_THUMBPOSITION:
               Debug.Print "Thumbposition: " & wParam
               Debug.Print "thumbposition"
            Case SB_THUMBTRACK:
               Debug.Print "Thumbtrack: " & wParam
            Debug.Print "thumbtrack"
            Case SB_TOP:
               Debug.Print "top"
         End Select
         WindowProc = CallWindowProc(glpPrevWndProc, hw, uMsg, wParam, lParam)
      Case Else: '// allow inherited message processing
         WindowProc = CallWindowProc(glpPrevWndProc, hw, uMsg, wParam, lParam)
   End Select
End Function
LVL 15

Expert Comment

ID: 16868164
LVL 29

Expert Comment

ID: 16868416
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 16869721
OK.  good..
 But Actually ;
 I asked this question for this: Can I show the message when user kill form.exe from task manager?
Because I want to prevent window close from everywhere..I subclassed and prevent WM_CLOSE, WM_DESTROY...But when kill order message comes from taskmanager I don't know what is the message...

1)what is the message if order comes from task manager (end process)
2)Can I prevent closing window like this or how can I prevent colsing window? (Messages come from whatever)

LVL 29

Expert Comment

ID: 16897319
If the user is an administrator than can end your process regardless. However you can use CreateProcess and block any requests for the handle to your process this will not allow un-authorized termination of your process.
LVL 28

Accepted Solution

Ark earned 500 total points
ID: 16938012
LVL 33

Expert Comment

ID: 17043399
    sorry to post a comment here. But then I knew you are good in API.
    Could you have a look at my question?

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

820 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