Pages

Tuesday, 18 October 2011

An example of how to create your own custom library.


This example will show you how to create your own customised code 
library, using Visual Basic 6 as an example.

First thing to do is open Visual Basic and create a new Active X DLL project... 
  


Before we add any code, we should give the Project and the Class Library sensible names.

Here I've called the project "QTP"...

 


For the Class Library I've simply called it "Library"...

 


Now we can add a function to our Library. For this example I'm going to use a very
basic function which will simply display a message box with a given parameter value...

 


Next thing we need to do is create the DLL, this can be done from the File menu in Visual Basic...

  


Note that during the development of the DLL, you can simply press F5 to run the code in Visual 
Basic. We can then still call the function from QTP, this allows us to put break-points inside
the Visual Basic code and do some debugging. 

Another thing to note is that when you finish the DLL and want to use it on other machines,
you will need to register the DLL on the system. This can be done by simply dragging and dropping
the DLL onto the file "RegSvr32.exe", which can be found in your Windows\System32 folder.

Now that we have our new library ready, we can call the functions from QTP... 


Dim objDLL

' create an object for our new library
Set objDLL = CreateObject("QTP.Library")

' call the function from the library
objDLL.QTPHelper_Example "Easy!"

' destroy the object
Set objDLL = Nothing


And here is the end result...
 


Using methods like this will open up several new doors for your automation by allowing you to 
execute code which isn't as easy to implement in VB Script as it is in other languages. 

0 comments:

Post a Comment