Site Loader
Auckland, New Zealand
There are situations where we might need more functionality for our test framework which our QTP and Vbscript might not able to be provide us. We know the .Net really powerful and there are lot of in-build classes, methods and properties available which we use them in C# or VB.Net, if we need to use the same in QTP, we can do so by using our very know “CreateObject” method. There are lot of DLL files available in .Net framework once you install, you can see them of course from your assembly folder under Windows directory, which we call as GAC (Global Assembly Cache). We can almost use all of them, if we really need them and provided we know the exact functions and parameters to pass with the function. In this demonstration, I am going to create a simple DLL file using “Class Library” project in Visual Studio and then going to use the same DLL in QTP. So lets get started. Step 1 As I was saying, first we need to create a DLL file, which you can do so by using Visual Studio Class library project. Step 2 Next create a Class say “Libs” and create a very simple  functions as shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ForQTPLibs
{
    public class Libs
    {
        /// Adds Two integer values and returns the result
        public int AddIntegers(int val1, int val2)
        {
            return val1 + val2;
        }
        /// Adds Two decimal values and returns the result
        public decimal AddDecimals(int val1, int val2)
        {
            return val1 + val2;
        }

        /// Returns todays date as like Date() function in Vbscript
        public DateTime Today()
        {
            return DateTime.Today;
        }
    }
}
Step 3 Before deploying the DLL file, all we need to do is make the DLL accessible from your QTP. This we can do so by following simple procedure. Click on the project properties and Click on the Assembly Information from from Application Tab Make sure that you Check the “Make assembly COM-Visible” Next go to “Build” tab and Check the “Register for COM interop” as shown Step 4 Now build the complete project and now as you know your DLL file should have generated at your \bin\debug folder. Step 5 The final step is to call the DLL file from QTP, all you need to write a simple code as shown below
'Use .Net DLL files Using Create Object
Set DotNetObject = CreateObject("ForQTPLibs.Libs")

msgbox(DotNetObject.AddIntegers(20,30))
msgbox(DotNetObject.Today())
Result Once you run the script, you will get the output That’s it !!! Please leave your comments and rate the post !!! Also please mail me @ karthik@executeautomation.com if you find this post needs any modification.

Post Author: Karthik kk

3 Replies to “Calling DLL files in QTP using CreateObject”

  1. Can you please help me in the below scenario,
    I have to call the method from from a .vb file in UFT:
    I should display/call the function Getmyname() and display the MsgBox(“Hello world”)

    Here you can see the details:

    Public Module globalMod
    Public gCompCollection As New ArrayList

    Public Sub Getmyname()
    Try
    MsgBox(“Hello world”)
    Catch ex As Exception

    End Try
    End Sub

    End Module

Leave a Reply

Your email address will not be published. Required fields are marked *