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
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
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses
Course of the Month3 days, 11 hours left to enroll

630 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