Go Premium for a chance to win a PS4. Enter to Win


subclass other window

Posted on 2006-06-09
Medium Priority
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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 2000 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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

824 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