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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


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 27

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now