API TUTORIAL for Beginners-II

Submitted on: 1/17/2015 6:06:00 PM
By: Venkat Mani (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB 6.0
Views: 746
     This is the 2nd in the series of articles and shows you the significance and importance of the sendmessage api and also showing you how the password text from a password field is captured using the sendmessage api


API TUTORIAL for Beginners


The SendMessage API


The SendMessage Api is one of the most powerful api functions . Before we  take  a look at it's uses and syntax let me give you a brief overview of how the windows os works.

The Windows Operating System  is a message based operating system .By saying message based means that whenever the operating system (os) has to comunicate with applications or  two applications need to communicate/send data among themselves they do so by sending messages to one another. For eg when an application is to be  terminated the os sends a WM_DESTROY message to that application, also when you are adding an item to a listbox ,the application/os sends a LB_ADDSTRING message to the listbox .  

While programming in VB the sendmessage api is not of much use when u want to manipulate objects controls in your own application.But say u wanted to change the title of some other application or wanted to get the text from a textbox of another application or want to terminate another application ,or set the text in a text box of another application. The uses are endless if u want to  play around with your system.Also if you are planning to move over to win32 programming using  c++ you just cannot escape the sendmessage api.

Let us look a the declaration of the sendmessage api

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

 The SendMessage api function basically takes 4 parameters

  1. hwnd-The handle of the window to which the message is being sent

  2. wMsg-The message that is being sent to the window.

  3. wParam-Parameter to be sent along with the message(depends on the message)

  4. lParam-Parameter to be sent along with the message(depends on the message)


Let us see a practical implementation of this api . Let us assume that we want  to get the *** masked text from a password textbox of a window!!! .We need to know a few things before we can do this. The first thing we need to know is  the handle to  the textbox window. One way of getting this is by using the windowfrompoint api.Check my first tutorial on how to use this api and get the window handle of the textbox.

Once we have this handle we need to send a WM_GETTEXTLENGTH message to the textbox .This message is essentially sent  to query the textbox and get the length of the text string in that textbox.After we know the length of the string we have to send a WM_GETTEXT message to the textbox and the textbox will return the text as the result .This is how it is done

Along with the declaration of the sendmessage api you need to declare the 2 message  constants that we are going to use

Private Const WM_GETTEXT = &HD


Put the following in any event of a control .In this example we are putting it in a command click event

Private Sub command1_click() 

Dim length As Long
Dim result As Long
Dim strtmp As String
length = SendMessage(hwnd, WM_GETTEXTLENGTH, ByVal 0, ByVal 0) + 1
strtmp = Space(length)
result = SendMessage(hwnd, WM_GETTEXT, ByVal length, ByVal strtmp)

End Sub

here hwnd is the handle of the password textbox.

Example 2

In this example we will try to change the title of any application ,in this case it will be a windows notepad application.

As was the case previously we have to get the handle of the notepad window .There are 2 ways to get this one is by using the windowfrompoint api and the other is by using the findwindow api.The findwindow api returns the handle of the window whose title has been specified in the function.

After we get the handle of this window we do a sendmesaage function

dim result as long

dim str1 as string


result = SendMessage(hwnd, WM_SETTEXT, ByVal 0, ByVal str1)

Using almost the similar techniques you can also put your own text in the edit window of the notepad application.


In this tutorial we have seen  a few uses of the sendmessage api.You can try out any of the numerous messages in the windows os system on any applciation. Sendmessage is in other words a bridge for communication between your application and another application

Questions,comments send them to




Other 14 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.