Enum Windows & SendMessage API

SendMessage

Declared as:

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

SendMessage API is a very powerful function and lets you do truly amazing things. To understand SendMessage API you have to understand the concept of ‘window messages’. I’ll try to make it as simple as possible for you. The Windows operating system completely runs with the help of messages. If any job that has to be performed by a window there is a unique message for that job. For instance, in a textbox when you press a key there are two messages that are passed to textbox called WM_KEYDOWN and WM_KEYUP along with further info about the keys pressed. Remember that with every message two more arguments are associated called lParam and wParam called message parameters. The values of lparam and wparam depends upon the message, like for WM_KEYDOWN message the:

wParam specifies the virtual-key code of the nonsystem key, and

lParam specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag.

Sometimes its also possible that lParam and wParam parameters may not contain any value and will be null, like WM_COPY message copies any selected data of a given window to the clipboard. Now if you know some window that has a selected text and you know the handle of that window you can copy the selected text of that window with the following single line of code, no matter that window is your own or belongs to some other application:

Public Const WM_COPY = &H301
SendMessage (hwnd, WM_COPY, 0, 0)

Here the hwnd is the handle of the window whose selected text you want.

You can use SendMessage to enhance the features of any object, the editbox does not give you the option of undo but you can do it through WM_UNDO message and for this also the lparam and wparam are null or 0. Similarly you can pass the message WM_LBUTTONDOWN to any command button to simulate mouse click on that command button without actually clicking on it. How will you know what lparam and wparam values to pass along with this message? You have to observe. One way is to consult (go lost) in the MSDN library. Another:I often use the spying tool Spy++ that comes with Visual C++. It is a very useful program especially if you want to ‘learn’ everything about how a window ‘lives’ in a system. I would simply hook a window for its messages in Spy++ and click any of its command buttons and see what messages are involved when I click, and through studying this procedure I will make my own message simulator.

In the above examples your were passing some data along the message in form of lparam and wparam and that data was used or in case of null values it was being ignored. But there are messages for which you pass the variable required and the data is copied into the variable. Read previous sentence again. This is what I’ve done in EnumerationX to get the text of the edit boxes and captions of command buttons that could not be get by GetWindowsText API. First you have to pass the WM_GETTEXTLENGTH which returns the length of whats written inside the window then you create a variable whose length is windowtextlength+1 and fill it with some junk. Now you pass the variable to the SendMessage along with its parameters. In EnumerationX the GetText function (declared in module) was doing the real work and the statement which gets the text looked like this:

Textlen = SendMessage(Ihwnd, WM_GETTEXT, Textlen, ByVal Text)

Here Ihwnd is handles of the window whose text you want

WM_GETTEXT is the message with a constant value of &HD

Textlen is wparam parameter

Text is lparam parameter and must be is pass by value. You always have to take care in APIs of whether the arguments passed requires a by value or by reference, if you mistake there your app will simply crash without letting you save your work!

This is a quite magical statement and to share with you an interesting truth (if you still haven’t discovered it) if you apply the same principle above for a window that has a password field which write asterisks “*******” the WM_GETTEXT message will retrieve the password behind asterisks, which is what EnumerationX can also do. (This applies only to Windows 95/98, text behind password field cannot be retrieved by WM_GETTEXT message in Windows2000).

In EnumerationX I also gave an option of changing (patching) the contents of any window. This was even simpler with the help of WM_SETTEXT, and the statement in the Form2 Command2_Click event of EnumerationX looks like this:

SendMessage Val(Text1.Text), WM_SETTEXT, 0, ByVal Text2.Text

Val(Text1.Text) has got the handle, WM_SETTEXT is the messge , wParam should always be zero in this message as written in the MSDN documentation. Text2.text is what is to be written to the window and should be passed by value. Its that simple ! <and the text is changed>

You might also like...

Comments

About the author

Muhammad Abubakar United States

Nothing to say anything about me yet.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer Science is no more about computers than astronomy is about telescopes.” - E. W. Dijkstra