Search Tools Links Login

API TUTORIAL for Beginners-II


Visual Basic 6, or VB Classic

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

Original Author: venky_dude

Code







API TUTORIAL for Beginners


API TUTORIAL FOR BEGINNERS-II


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)



Example1


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



Private Const WM_GETTEXTLENGTH = &HE


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


str1="Venky"


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 venky_dude@yahoo.com


 


 


 




About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 116 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.