The Power of Extern


One of the very powerful tools in QTP - the Extern object. Thinking why this is so powerful !!!! The Extern object is one of the techinques of "extending" the capabilities of QTP. Understand that QTP, by itself, has numerous functionalities like Data Tables, Checkpoints, Synchronization points etc..etc.. But have you not ever come across a situation where you found that QTP could not do something on your test application !! I am sure you would have !!!!

OK...lets try this... Lets open the windows explorer...





How do we get the column width for the "Name", "Date modified" or "Type" column. QTP will be able to identify the area marked in red and so you can perform all the available methods QTP gives to you for the WinListView but is there a method to actually get the width of a column ? H-m-m not really... and that is where the Power of Extern comes into the picture. But first what is this Extern Object ???


The Extern object enables you to declare calls to "external" functions from an "external" dll or dynamic-link library.


OK...that would have been a pretty complex statement for beginners so lets try to simplify this statement.... DLL is a Microsoft implementation of a concept called as shared libraries. I dont want to deviate too much from our current topic but as of now just understand this, DLLs would export some functions which we will be able to use later on. Some of these functions can be called from the user level and others only at the kernel level. Different programming languages provide different techniques for achieving this and in QTP it is through the Extern object. I guess that should be sufficient for now and when we see the example below, we are going to get a real good idea of this concept.

So how are we going to find the width of the columns above. We know that QTP's WinListView object does not have any capability of its own to give us this information but there is someone else - user32.dll. This is a file residing in C:\Windows\System32 and it has a function called as SendMessage which we are now going to use in QTP.... now its getting interesting right... alright so lets get into it.

We first need to declare the function because QTP does not know about this function yet and this is how we are going to declare it

Extern.Declare micLResult, "SendMessage", "user32.dll", "SendMessage", micHwnd, micUInteger, micWParam, micLParam


Now you must be wondering how I knew the prototype of this function, right ? Just go to MSDN... SendMessage function description

At this point, QTP knows that there is a function called as SendMessage and will now allow us to use this. However SendMessage is not just for getting the column width, it has numerous other uses which you can research on your own time but what I am going to show is just the mechanism to get the column width.

The first argument to the SendMessage function is the handle to the object and in this case its the WinListView object.

The second argument is the message to be sent and in this case it should be LVM_GETCOLUMNWIDTH. As of now, just understand that the decimal value for this is 4125.

The third argument is the index of the column which starts at 0.

The fourth argument should always be 0.

Dont get too overwhelmed at this stage !! We are not trying to explain what SendMessage is but rather the Power of Extern object which helps in using external functions. A developer in your team could provide you a dll as well and you can make use of the functions in that dll if you know the function prototype. OK... so lets move ahead..

We are all set now to get the column width and here is how our program is going to look like.



The third argument is the column index. So in this example, it would give the column width of the first column which is "Name". If you change it to 1, it would give the column width of the second column which is "Date modified".

Well, we just experienced the Power of the Extern object which "extends" the capability of QTP. We were able to do something which the WinListView object was not capable of. A very powerful tool which you would definitely need as you do more advanced programming.


I dont want to have any sort of copyright or copyleft in this site but if you are reusing this code in any other site, I would appreciate you to provide this page as a link so that others get a more descriptive explanation of this concept. Your suggestion and comments are always welcome. Cheers !!!
As always,
Your friend in need,

George, Reju

Read Users' Comments (4)

4 Response to "The Power of Extern"

  1. Ralf Lauber Says:
    February 23, 2011 at 4:42 AM

    Hi,
    it's a nice approach to get more information on an Object.
    I tried it out with LVM_getitemstate but an error occurs.
    I use:
    Extern.Declare _ micResult,"SendMessage","user32.dll","SendMessage",micHwnd,_ micUinterger,micWParam
    Const my_LVM=4140
    lngHwnd=VbWindow("VbWindow").WinListView _("ListView20WndClass").GetROProperty("hwnd")
    my_Result=Extern.SendMessage (lngHwnd,my_LVM,0)

    What is wrong about this and how do I do it right
    Best regards
    Ralf

  2. Reju George says:
    February 23, 2011 at 5:02 AM

    Not sure if its a typo but your declaration seems to be wrong..

    The declaration is
    Extern.Declare micLResult, SendMessage","user32.dll","SendMessage",micHwnd,_ micUInterger,micWParam, micLParam

    - George, Reju -

  3. Ralf Lauber Says:
    February 23, 2011 at 5:35 AM

    Oh thanks for your fast reply,

    you are right, I forgott the last Parameter.

    best regards
    Ralf
    PS: by the way, to send you a comment is a quite tricky. The capture is to big (or the view is to small). So I cannot see the button.

  4. Anonymous Says:
    July 3, 2012 at 11:03 PM

    When i tried to use SendMessage method to get the text from the 2 part of the Notepad status bar, QTP and notepad both hangs .. what could be the possible reason

Post a Comment

Visitors

Website Counter