subclass other window

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..
Who is Participating?
ArkConnect With a Mentor Commented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

hbarisxxAuthor Commented:
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)

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.
    sorry to post a comment here. But then I knew you are good in API.
    Could you have a look at my question?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.