QTP: Accessing Custom DLL Methods Through DOTNetFactory

by Anshoo Arora ON February 20, 2010 · Posted In All, DOTNetFactory, QTP · 107 comments

In the past few weeks, I have seen a lot of new topics on the use of custom DLLs. VBScript’s lack of certain functionality and slow performance, more and more QTP Engineers are turning towards their custom DLLs to simplify and speed-up and simplify part of their framework design. This article shows how the DLLs can be loaded to QTP and their methods accessed with the help of DOTNetFactory.

The DOTNetFactory Object was introduced with QuickTest Professional version 9.1. If you are using version 9.0 or less, the below code may not work 1 [Credits go to Christian for this update].

Below is the code shown in both C# NET and VB NET:

namespace oNamespace
{
    public class PublicClass
    {
        public int myInteger()
        {
            return 9;
        }
 
        public string myString()
        {
            return "Testing DLL [DOTNetFactory]";
        }
    }
}
Namespace oNamespace
    Public Class PublicClass
        Public ReadOnly Property Get myInteger() As Integer
            Get
                Return 9
            End Get
        End Property
 
        Public Function myString() As String
            Return "Testing DLL [DOTNetFactory]"
        End Function
    End Class
End Namespace

There are 2 outputs I can retrieve from the code above: myInteger and myString as they are both public methods of the Class.

After building the Project and referencing the DLL saved in one of the Project’s folders (bin or debug), I copied the DLL to my D drive:

DLL File Created

DLL File Created

I can now call the methods of the DLL using the following code:

Dim oCustom 
 
Set oCustom = DOTNetFactory.CreateInstance("oNamespace.PublicClass", "D:\\RelevantCodes.dll")
 
MsgBox oCustom.myInteger
MsgBox oCustom.myString

Upon running the above code, I will have the following 2 outputs:

myInteger myString

I hope you find this article useful :)

Subscribe to Relevant Codes (by Anshoo Arora)

Hello! We're always posting interesting articles on Relevant Codes. Why not subscribe so you don't miss out?

Leave a Comment

{ 107 comments… read them below or add one }

Rajiv January 30, 2012 at 5:44 pm

Could you please share your email id plz
I need an urgetn help: getting the below Error:
Could not load file or assembly ‘file:///C:\PracProj\PublicClass\PublicClass\bin\Debug\PublicClass.dll’ or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

Line (3): “Set oCustom = DOTNetFactory.CreateInstance(“oNamespace.PublicClass”, “C:\PracProj\PublicClass\PublicClass\bin\Debug\PublicClass.dll”)

Reply

Anshoo Arora February 13, 2012 at 11:51 am

Rajiv, what is the version of .NET framework of the assembly?

Reply

Dhaval Soni October 20, 2011 at 7:17 am

ho

Reply

Nell July 25, 2011 at 6:40 am

Hi All,

Can anyone help me on how we can convert or migrate all the C # (sharp) code which is written using teliric web UI functional testing tool to QTP VB scripting.

Let me know in case you need any further information.

Please note that In our project we have created all the reusable functions and the test scripts in ‘.cs’ format and not as any dll’s. We are using Visual studio and Telerik libraries for script designing.

Hence please guide how I can move further.

Looking forward for your guidance and support too..

Thank You

Best Regards,
Nell

Reply

Anshoo Arora August 1, 2011 at 4:10 pm

Nell: See this link: http://relevantcodes.com/qtp-custom-dlls-through-dotnetfactory/

Can you share a code snippet with me? I will convert it and you can try to see if that works.

Reply

Vishal July 20, 2011 at 5:22 am

Hi,

My QTP script is not able to recognize the text box in a tabel where it need to write the data picked from a pop up window.
First time it recognizes it but second time (after relaunching screen) not able to go through says unable to read text box.

Thanks in advance.
Vishal.

Reply

Anshoo Arora August 1, 2011 at 4:03 pm

Vishal: Can you please share the HTML source of the page?

Reply

Xiaofu June 25, 2011 at 3:51 am

Anshoo, in your most recent example above what data type is QTP seeing returned your iInteger property? A string, correct? If so, any ideas about how to get an int back as you’d expect?

Reply

Anshoo Arora June 25, 2011 at 9:18 pm

Xiaofu: I believe the type should be returned as ‘object’. What type of editor did you use? VSVB, VSC# or VSC++?

Reply

Maite March 3, 2011 at 12:02 pm

Hi! I’m starting to test a desktop application coded in .NET (c#), as it is quite complex I would like to access to its objects, but when I use the DotNetFactory.CreateInstance wthout the asembly it cannot find the dll loaded in memory… Any suggestion?

Thanks in advance!!

Reply

Joe Loyzaga February 6, 2011 at 11:21 pm

trying to use the webaii framework controls via qtp
e.g
Set Manager = Nothing
Set Manager = DotNetFactory.CreateInstance (“ArtOfTest.WebAii.Core.Manager”,”C:\\Program Files (x86)\Telerik\WebAii Testing Framework 2010.3\Bin\ArtOfTest.WebAii.dll”, false)
Manager.LaunchNewBrowser
wait 7
Set Manager = Nothing

the vs2010 code is

Settings mySettings = new Settings(BrowserType.InternetExplorer, @”c:\log\”);
// Create the manager object
Manager myManager = new Manager(mySettings);
Settings settings = new Settings();
settings.AnnotateExecution = true;
// Start the manager
myManager.Start();
// Launch a new browser instance. [This will launch an IE instance given the setting above]
myManager.LaunchNewBrowser();

how can I make it work in qtp?
If you need any of these dlls I can send via email……

Reply

DEVENDRA SHARMA February 3, 2011 at 9:17 am

Thanks!!

Reply

DEVENDRA SHARMA February 2, 2011 at 1:32 pm

Hi Anshoo – It would be nice, if you could please send the DDL file created in VB.Net for the below code on my email (devendra1784@hotmail.com)

Namespace oNameSpace

Public Class PublicClass

Public ReadOnly Property myInteger () As Integer
Get
myInteger = 9
End Get
End Property

Public Function myString () As String
myString = “Testing DLL [DOTNetFactory]”
End Function

End Class

End Namespace

Looking forward for your positive response!!

Regards,
Devendra Sharma

Reply

Anshoo Arora February 3, 2011 at 2:35 am

Devendra, you can download the DLL from here: http://relevantcodes.com/Articles/DLL_DOTNetFactory/Testing.zip

Usage:

Dim ref

Set ref = DOTNetFactory.CreateInstance("MyCustomNamespace.Math", "C:\Testing.dll")

MsgBox ref.sString
MsgBox ref.iInteger
MsgBox ref.Add(1, 2, 3, 4, 5, 6, 7, 8)

Namespace: MyCustomNamespace

Code:

Option Strict On
Option Explicit On

Public Class Math
    Public Function Add(ByVal ParamArray integers As Object()) As Integer
        Dim ix As Integer = 0
        Dim iy As Integer = 0

        For Each ix In integers
            iy = iy + ix
        Next

        Return iy
    End Function

    Public ReadOnly Property sString() As String
        Get
            sString = "This string is returned from the .NET assembly!"
        End Get
    End Property

    Public ReadOnly Property iInteger() As Integer
        Get
            iInteger = 10
        End Get
    End Property
End Class

Reply

DEVENDRA SHARMA January 24, 2011 at 2:23 pm

Hello Anshu – I have written the below code in VB (Template : Class Library) using .NET Framework3.0 in Microsoft Visual Studio10.0.

————————————————————————————————-
Namespace oNameSpace

Public Class PublicClass

Public ReadOnly Property myInteger () As Integer
Get
myInteger = 9
End Get
End Property

Public Function myString () As String
myString = “Testing DLL [DOTNetFactory]”
End Function

End Class

End Namespace
————————————————————————————————-

When, I build the Project using option “Build Solution” the following message is displayed:

—— Build started: Project: ClassLibrary2, Configuration: Debug Any CPU ——
ClassLibrary2 -> C:\Devendra\ClassLibrary2\ClassLibrary2\bin\Debug\ClassLibrary2.dll
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
————————————————————————————————-

When I have tried to access the DDL methods using QTP10.0 with the help of below code:

Dim oCustom

Set oCustom = DOTNetFactory.CreateInstance(“oNamespace.PublicClass”, “C:\Devendra\ClassLibrary3\ClassLibrary3\bin\Debug\ClassLibrary3.dll”)

MsgBox oCustom.myInteger
MsgBox oCustom.myString

The following ERROR Message is displayed:

“Type ‘oNamespace.PublicClass’ not found in any assembly”

Note: I have also tried by adding the Namespace using Solution Explorer.

Looking forward for your response!!

Reply

Anshoo Arora January 25, 2011 at 9:32 am

Devendra,

Kedar (above) reported that this issue was not encountered when the DLL was created with C#. I’m not sure why this issue exists, as I have not been able to reproduce it with either VB.NET or C#.

Also, if you’ve set your global namespace through the Project/Solution Explorer, make sure you remove the namespace statement from your code and only have a Public Class. Another thing you would need to do is to set your assembly COM-visible.

Reply

Kedar Sawant December 29, 2010 at 11:18 pm

Even I was facing same problems when I was trying to access a dll made in “vb” but no problem when I tried accessing the dll function of C#.net.
See my comments above in this blog.
Thankx

Reply

Prasad Kalaskar December 29, 2010 at 11:48 pm

Please Let me know How Did U reslove It

Reply

Anshoo Arora January 2, 2011 at 7:10 pm

Kedar,

I haven’t been able to reproduce this in either VB or C#. Assemblies work well using either. Should there be any difference though?

Reply

Prasad Kalaskar December 29, 2010 at 9:41 pm

Hi I am trying to calla .net dll in QTP Environment,

However I am getting the error “Type ‘oNamespace.second.PublicClass’ not found in any loaded assembly”
oNamespace.second.PublicClass is my organizant of namespace.
also note my class is public.
COM visibility is set to true.

Reply

Anshoo Arora January 2, 2011 at 7:10 pm

Prasad, this generally happens if you’re trying to access an invalid Class, method or Namespace or all three.

Reply

Kedar Sawant October 1, 2010 at 4:09 am

Hi Anshoo,

I am trying to pass an array from QTP Script to the function in the dll, but I am getting an error stating “Variable uses an Automation type not supported in VBScript:”

Can you please tell me what I can do to pass an array from QTP Script to a function in a dll

Reply

Anshoo Arora November 14, 2010 at 4:53 pm

I think I’ve seen that error before. Can you try passing the array from .NET using ToString or pass it as an UnManaged type?

Reply

Kedar Sawant September 30, 2010 at 11:39 pm

Hi Anshoo,

Things working fine, the code is working fine now, actually I tried creating the dll in C# and it started accepting the dll and loaded the class and I am able to access the function within that class.

But yes it is not working with the dll made in vb, dont know why.

Really nice article.

great, thanks

Reply

Kedar Sawant September 28, 2010 at 3:00 am

Hi, I have tried the above code for accessing the dll created in VB.Net in QTP, Below is my code, and I am getting an error saying “Type ‘oNamespace.Class1′ not found in any loaded assembly’ at the line number 2 of my code.
Can anyone tell me where am I wrong so that I correct that

””’ Code Start ””””””””””””””””””””””””””””””’

1. Dim oCustom

2. Set oCustom = DOTNetFactory.CreateInstance(“oNamespace.Class1″, “U:\\ClassLibrary3.dll”)

3. MsgBox oCustom.sayHello

””’ Code End””””””””””””””””””””””””””””””’

Reply

Anshoo Arora September 29, 2010 at 3:13 pm

Kedar,

Have you specified the Namespace as a global Namespace for the Projec?

Reply

PruebasTool September 20, 2010 at 9:26 am

Hi Everyone!
I’m a begginer tester, and I’m a bit lost with the scripting yet, I need to go a bit further using DotNetFactory, but I don’t know if it is possible. I would like to get the object loaded in memory instead create a new instance. I’m trying but it just gets empty objects, so I suposse tha is a new instance of it.
My code:

assemblyVec = “C:\Proyectos\Exes\Release\Vec.dll”
assemblyFramework = “C:\Proyectos\Exes\Release\Framework.dll”
assemblyClip = “C:\Proyectos\Exes\Release\Clip.exe”

Set transformacion = DotNetFactory.CreateInstance(“Tool.Vec.Transformacion2T”, assemblyVec, 10, 10)
Set serviciosProyecto = DotNetFactory.CreateInstance(“Tool.Clip.Servicios.Impl.ServiciosProyecto”, assemblyFramework)
Set serviciosProyectoMemory = DotNetFactory.CreateInstance(“Tool.Clip.Servicios.Impl.ServiciosProyecto”)

the vble transformación works because it creates the object it should, but the Vble ServiciosProyecto just have empty properties, I tried to not add the addembly to manage the loaded memory object, but it didn’t work…
Any idea???
Thanks in advance!!!

Reply

Anshoo Arora September 29, 2010 at 3:04 pm

PruebasTool,

I’m not sure what exactly the issue here is, but are you binding to the correct Namespace/Class? Did you try to register the assembly as codebase and checking if you’re able to access the methods using CreateObject?

Reply

vidya September 12, 2010 at 9:49 am

Hi Anshoo,

I sent you my DLL code to your email id mentioned in one of the above posts. Please have a look.
Thanks

Reply

vidya September 10, 2010 at 9:43 am

I tried arraylist but getting object required error. Here is my code
Set MyList = CreateObject(“System.Collections.Arraylist”)
MyList = objCustom.CalculateError()
cnt = MyList.Count()

I get object required error at second line of my code. The function CalculateError() is in my custom dll where I am returning the arraylist.
Public Function CalculateError() As ArrayList
Dim myAL As New ArrayList()
CalculateError = myAL
End Function
This is my custom dll. I am unable to figure out why it is giving “Object required error”. I am able to see the arraylist in dll but I think it is not coming back to QTP. Can you please find what’s wrong with this?
Thanks

Reply

rezapb November 16, 2010 at 4:40 pm

Vidya:

The reason you are getting the object required error is because you should need to use the ‘set’ keyword to get an object ie:
set myList = objCustom.CalculateError()
msgBox myList.count()

Hope that helps,
Reza

Reply

vidya September 9, 2010 at 4:02 pm

Hello,
Is it not possible to return an array from custom dll to QTP? I need to return an array from custom dll to an array in QTP. I get type mismatch error. Here is my code:
Dim Arr(4)
Arr = objCustom.CalculateError()
Here objCustom is an instance for the dotnetfactory and CalculateError() is my function in dll where I am returning an array. This gives Type mismatch error. Please help me.

Thanks

Reply

rezapb September 9, 2010 at 10:35 pm

I also faced this problem so instead I chose to use System.Collections.ArrayList. (ie:
Set arr = CreateObject(“System.Collections.ArrayList”)

This datastructure can be passed back and forth between QTP and C# or VB.NET

The API for the ArrayList object is available on MSDN website

Reply

Anshoo Arora September 11, 2010 at 1:32 am

Vidya, its possible to pass arrays across .NET to other languages. What is your DLL code?

Reply

Kedar Sawant October 1, 2010 at 5:48 am

Hi Vidya,

I am also trying to to access the values in the form of an array returned from the function in the dll written in C#, did you cracked the issue, if yes please tell me how did you do that.

Thanks,

Reply

vidya September 9, 2010 at 9:00 am

Anshoo,
It is really a painful process to debug my custom dll but it is worth to take that pain because of its lightening fast when compared with QTP VBScript. Thanks for the idea of the custom dll.

Reply

Anshoo Arora September 9, 2010 at 1:58 pm

Yeah, its definitely a trade-off. I still prefer coding everything in .vbs files as its easier for me to debug right off a text-editor. I have always thought of using DLLs but after few days of using them, I drop the idea every time and come back to what I’m mostly used to with QTP. The other issue I face in projects is not everyone on the Automation Team works with C#.NET or VB.NET – therefore, creating code that no one will maintain besides me fails a collective (aka team) effort.

Reply

vidya September 8, 2010 at 2:47 pm

Hi Anshoo,

I figured this out yesterday after some trials. It is really hard to debug this custom dll since it is not possible to do it from QTP. Also, every time I make a change to dll, I have to shut down QTP to make it execute the modified dll next time. It is not dynamic in creating instance for the custom dll. Is there any easy way to debug the custom dll that I have created from VB 2008 Express Edition 2008 UI? Thanks for the reply to my previous post.

Vidya

Reply

Anshoo Arora September 9, 2010 at 1:19 am

Vidya: All your observations are correct. This is the same behavior I notice and I have to shut down QTP each time – not sure why it binds to the DLL in a manner it can’t be compiled. The only way to debug the DLL is through VS.

Reply

vidya September 7, 2010 at 10:33 am

Hi Anshoo,
I was following this article. I have downloaded VB 2008 Express Edition and created a “VB class library (.dll) (.NET Framework 3.5)” . I don’t know how to build this project and reference the DLL saved in one of the Project’s folders (bin or debug). I tried some things in VB 2008 Express Edition but was unable to build the project. I appreciate if you could put detailed steps for building the project.

Thanks in advance
Vidya

Reply

Anshoo Arora September 8, 2010 at 8:09 am

Vidya,

You can build DLLs from: Build -> Build

Once you do that, navigate to the project’s folders bin or debug and the DLL will be located there.

Reply

madras mozart June 16, 2010 at 1:26 am

Anshoo Arora,

Pretty impressive ! I Went through your post as well as comments and discussion.
I have a .Net Dll , should i have .Net framework in the my system to access it through Dotnetfactory ?

Reply

Anshoo Arora June 22, 2010 at 12:29 pm

Well, QTP by default requires the .NET framework to be installed on the machine before we can use it. The DOTNetFactory object is specific to QTP and will only work within QTP.

Reply

Devendra Sharma May 14, 2010 at 9:17 am

Hi Anshoo,

I have selected the option “A project for creating a VB class library (.dll) (.NET Framework 3.5)” and created the Class using ‘Microsoft Visual Basic 2008 Express Edition’, now how I can build the Project and adding the DLL as a reference (and re-building the Project).

Regards,
Devendra Sharma

Reply

Anshoo Arora May 14, 2010 at 9:28 am

Devendra,

Once you build the project, the DLL should be available in the Bin and/or Debug folders. You can reference that DLL using DOTNetFactory.

Reply

Murali April 22, 2010 at 7:18 am

Anshoo,

I have created DLL and Integrated the same with QTP. But I am not able to see the intellisense. Do I need to do any specific changes to DLL to get the intellisense.

Reply

Anshoo Arora April 25, 2010 at 3:15 pm

What type of DLL is this? .NET DLL?

Reply

rezapb April 14, 2010 at 4:16 pm

Hi Anshoo:

I have one more question for you also:

Suppose my custom DLL has references to another DLL (ie log4net). For some reason that is not working in QTP, there is some exception being thrown. Do I need to combine the 2 DLL’s into a single DLL?

Thanks!

Reply

Anshoo Arora April 16, 2010 at 5:46 am

Not so sure about this one. I will test this scenario and post my observations here. So far, I have only used a single DLL with several class libraries. I would need to look into this to be able to provide my feedback. From my initial observation though, I don’t see why this would be an issue..

Reply

rezapb April 13, 2010 at 7:13 pm

After I access my custom DLL in QTP using DotNetFactory, I have noticed that QTP puts a lock on that file and I can no longer make changes/recompile my code again. I am forced to close QTP, then reopen it in order to be able to recompile my classes. I was wondering if you are familiar with this issue, and if so is there any workaround?

Reply

Anshoo Arora April 14, 2010 at 3:23 pm

I tried to reproduce this, but am unable to. Can you please share the code you used?

Reply

Vivek April 8, 2010 at 8:52 am

Hi Anshoo,so far I did not get the success for using dll file other than dotnetfactory.Where can I locate RegAsm.exe so that I can use it.Is it part of Window OS? Guide me the right steps to use it to registering my dll.

One more thing,I have written log writer functions in my project which keep writing data updation happening in my scripts in log file for tracibility and these methods have been used frquently across the scripts.What is my observation ;when calling these methods using Call statement it is taking bit more time than nromal ,I would say.Its like on each line where these mthods have been called scripts pause for some moment to get the value.Though it is not too much pause still I guess the script can somehow would run faster by someway.Do you think using dll which would have all my methods enclsoed can make the scripts running faster?

Thanks
Vivek

Reply

Anshoo Arora April 12, 2010 at 12:25 am

RegAsm.exe can be found under your .NET installation folders. You can also search it – and always remember to use the one which comes with the latest .NET package.

For your data update functions, they will certainly run faster through a DLL. A DLL is compiled code and its bound to be quicker than equivalent VBScript.

Reply

Jay March 31, 2010 at 6:12 am

Hi Anshoo, oh please help!
I have VS 2010, I’m wondering if this is going to be my issue, but none the less…
I follow you directions above to the letter, I’ve done this about 5 times, new project everytime. When I reference the dll with dot net factory in QTP I get the error “Type ‘oNameSpace.PublicClass’ not found in any loaded assembly. At first I though it may be permissions, or something similar, however if I try to delete the dll I created I get the windows msg that it is in use by Quick Test proffessional. So it must be loading. Have you got any idea’s, I’ve been working on trying to get my custom dll’s working for two day solid (14 hours a day). I went back to your example to just see if I could get something working. Is there something I am doing wrong in VS or QTP? Kind regards, and thanks for your great help, Jay

Reply

Anshoo Arora April 1, 2010 at 7:33 pm

Jay,

Would it be to share the DLLs code with me? I can try to convert it to a DLL and if it works, e-mail it to you? Or, are you trying to create the same DLL as we have in this article?

I have been planning on creating a quick video of this process with VS, and if you want, I can expedite it and send you the link as soon as its done.

Reply

Vivek March 29, 2010 at 9:42 pm

Hi Anshoo,I know DotNetFactory is definetly easier than using Extern.Declare but all companies may not have QTP 9.0 onwards and it is good to know the earlier methods for knowledge.I am not aware what is RegAsm.exe that you have mentioned.I will try regsvr32 for registering my dll file and try to see if works after that.For time being you can let me know about the RegAsm.exe? Is it some utility tool for registering dll?

Its really helpful to come on your site and having discussion with you.I really appreciate you for your time and efforts.
Thanks
Vivek

Reply

Anshoo Arora April 1, 2010 at 7:29 pm

Vivek,

To register .NET DLLs, we use RegAsm.exe. Its the same as RegSvr32, but for .NET DLLs. :)

Reply

Vivek March 29, 2010 at 9:17 am

I tried following syntax to access my function to show me the studentName:

Extern.Declare micString,”studentName”,”Vivek.dll”,”studentName”,micString

1.RetType I chose as micString
2.LibName will be Vivek.dlll or with full path “C:\Vivek.dll”.I tried both still getting same error.
3.Procedure Name as my function Name e.g. studentName
4.Alias as methodName which is studentName
5.argType as micString

Am I doing anything wrong here in passing arguments then let me know.
Thanks
Vivek

Reply

Anshoo Arora March 29, 2010 at 8:57 pm

Hmm.. I think you might have to register the assembly first using RegAsm.exe. I don’t have much experience using Extern :( as I find it much easier (and its more flexible) to work with DOTNetFactory. But, I think you can try the following:

Extern.Declare micString,”studentName”,”Vivek.dll”,”studentName”
Extern.Declare micVoid,”studentName”,”Vivek.dll”,”studentName”
Extern.Declare micInteger,”studentAge”,”Vivek.dll”,”studentAge”
Extern.Declare micVoid,”studentAge”,”Vivek.dll”,”studentAge”

Reply

Vivek March 29, 2010 at 8:54 am

Hi Anshoo,
I saw the above code in help file of QTP.But that does not give me idea how to use my custom methods and properties I included in DLL file which you sent.Can you illustrate it further using my DLL file.

As the above example is using user32.dll of windows OS and changing the title of the window of notepad but in my case I have to simply invoke my methods and properties from dll file and show its parameter’s value in message box…
This is our class in dll file:

Public Class Student

    Public Property studentAge() As Integer
        Get
            studentAge=20
        End Get
    End Property

    Public Function studentName() As String
        studentName=”Vivek”
    End Function

End Class

Thanks
Vivek

Reply

Anshoo Arora March 29, 2010 at 8:55 pm

Hmm.. I think we will have to mark the DLL as COM visible in order to use it with extern. I think you should be able to find a solution in this thread: http://www.sqaforums.com/showflat.php?Cat=0&Number=339332

Reply

Vivek March 26, 2010 at 10:09 pm

Hi Anshoo,with 10.0 the DLL you sent worked.Great!!
I have a question how can I access method and properties of dll for QTP 9.0? I guess Dot Net Factory does not help there.There should be some other way to access it using Extern.Declare.Are you aware about that then let me know.
Thanks
Vivek

Reply

Anshoo Arora March 27, 2010 at 10:36 pm

Vivek: The following is an example of Extern.Declare from QTP help:

'Declare FindWindow method
Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString
'Declare SetWindowText method
Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString
'Get HWND of the Notepad window
hwnd = Extern.FindWindow("Notepad", vbNullString)
if hwnd = 0 then
    MsgBox "Notepad window not found"
end if

'Change the title of the notepad window
res = Extern.SetWindowText(hwnd, "kuku")

I think it should be quite easy to use.. Because generally for custom DLLs, you would know which methods to use and what type of values they return or do not return. :)

Reply

Vivek March 25, 2010 at 8:59 pm

Anshoo,I got the dll.Thanks a lot forthe kind help.Though I ran it but getting error “The format of the file Vivek.dll is incorrect”.I guess this is because I have QTP 9.0 on my machine.I will upgrade it to 10.0 and try again.

Thanks
Vivek

Reply

Anshoo Arora March 27, 2010 at 10:34 pm

Yes Vivek,

If I am not wrong, DOTNetFactory was supported version 9.2 onwards.

Reply

Vivek Kumar March 24, 2010 at 8:44 am

Hey Anshoo,I got the way to use My Custom Funcitons.It was my mistake that I assumed it is available just on runtime.I can see after putting dot after my object my functions are available to be used.

I will wait eagerly for DLL from your side to see its uses and power.

How creating Classes help in QTP and where can we use it where it will have leverage over common functions and vb scripting in generic way?
Thanks
Vivek

Reply

Anshoo Arora March 24, 2010 at 10:20 am

Vivek: I think this article will probably give you a very broad picture of how classes can be used in QTP and how powerful they can be.

Reply

Vivek March 23, 2010 at 11:21 pm

Hi Anshoo thanks for quick reply…timezone diff is helping me in a way :)
I will wait for the dll that you will create and send to me.I remember when last time I asked someone to create dll I included namespace section as well.Probably that landed me with error saying “The format of the dll is incorrect”..I guess othe guys as well got that error.May be they also did that mistake.Anyway your site is very helpful and I really appreciate your effort to share your knowledge and experience.

Onemore question;do we need to register the dll before using DotNetFactory using regsvr32 cmd or its simply just use it without registering.And register one is old mehtod.

Regarding RegisterUserFunc I guess we just can use our custom function on runtime and after that it is not available.My question is can we add certain custom functions around any object so that it will be available anytime.Let say we write object.GetROProperty()..instead of that we can use our own custom functions using dot without using RegisterUserFunc..

I saw somewhere somepost which mentioned this but I could not understand that completely….

Thanks
Vivek

Reply

Anshoo Arora March 24, 2010 at 10:18 am

Vivek: I just sent the DLL for the code that you had written in the above post. The only thing with VB.NET that you must remember is that you will have to explicitly declare a property as ReadOnly if it is missing the Property Let statement.

To answer your question about Namespace, you can view the Project’s options and give the Namespace any name you want. I have named the namespace of this project as Vivek.

Onemore question;do we need to register the dll before using DotNetFactory using regsvr32

No.

.Let say we write object.GetROProperty()..instead of that we can use our own custom functions using dot without using RegisterUserFunc.

Unfortunately, the only way this is possible is with the use of RegisterUserFunc.. You can have intellisense for classes by some modification to your registry though. But, that’s a different topic altogether.

Reply

Vivek Kumar March 23, 2010 at 9:18 am

I am putitng my class right over here..please guide what code changes you will do to it when converting it to DLL file..I am not aware about .Net coding much so help me out:
Code is here…..

Public Class Student

    Public Property studentAge() As Integer
	Get
  	 studentAge=20
	End Get
    End Property

    Public Function studentName() As String
	studentName="Vivek"
    End Function
End Class

After chaging the code ,I guess you will add namesapce to it,can you please convert it to DLL file and send it over to my email id vivek dot kumar 2005 at-the-rate gmail dot com.You can save the file as Data.dll

Thanks
Vivek

Reply

Anshoo Arora March 23, 2010 at 10:04 am

I currently do not have VS on this PC, but as soon as I get home, I will create a DLL and e-mail it to you. You don’t need to add the class to a separate namespace; I only showed that as a demo.

Reply

Vivek Kumar March 23, 2010 at 9:05 am

Hi,I am new to using DLL in QTP.I have written few functions and properties in a class.Will you please guide me how can I convert my class which I wrote in vbs file or simply notepad into DLL file.Please guide me through step by step process.Do I need to install VB 2008 Express edition from anywhere then let me know the site.I guess it will be available on microsoft site.

Now my next question is why should we use DLL files over simple functions or classes using vbs file.What are its benefit part of it over vbs file.

My another question is; is it possible to register my functions with any given object in Obj Repositary permanenetly so that when I use that object(.) then I can see MyMethod(my registered functions} in QTP expert view.

Normally we use RegisterUserfunc for registering our functions in runtime but there may be better way to use it and share it with other applications.

Thanks in advance..

Vivek

Reply

Anshoo Arora March 23, 2010 at 10:02 am

Vivek,

You can download the VB 2008 Express Edition from here.

Now my next question is why should we use DLL files over simple functions or classes using vbs file.What are its benefit part of it over vbs file.

I generally do not use DLL files, as they encapsulate all code in a location that cannot be accessed. We can create project files that will have the code open, but as I work for clients most of the time, its not very ethical to have code that the other team member may not be able to understand.

But, if I were to use it, I would because the performance gain over normal VBScript is tremendous. VBScript is extremely slow, and if you use a DLL, you will notice that it works are a much greater performance as compared to any VBS code.

My another question is; is it possible to register my functions with any given object in Obj Repositary permanenetly so that when I use that object(.) then I can see MyMethod(my registered functions} in QTP expert view.

Yes, you can register almost anything to your objects. You will have to use RegisterUserFunc to do so, but yes, when you are writing your code, you will have the intellisense available for that method.

Reply

Rohith Vishwanath March 8, 2010 at 11:50 pm

Yes Anshoo. That is the link to download White.

Reply

Rohith Vishwanath March 8, 2010 at 11:40 pm

Yes Anshoo. That is the link to download White. The dll name is White.Core.dll

Reply

Anshoo Arora March 9, 2010 at 12:37 am

The DOTNetFactory syntax would be:

Set oCore = DOTNetFactory.CreateInstance("Core.Application", "C:\White_Bin\White.Core.dll")

The documentation has all the methods that you would need to use with the created object..

Reply

naveen March 8, 2010 at 11:34 pm

Hi ,I want to convert my library files to dlls and use the functions from QTP .This is because we do not want to expose the code to client…

Reply

Anshoo Arora March 8, 2010 at 11:37 pm

Hi Naveen,

I don’t think you should hide code from the client – you may be asked to expose your code when you transition anyways but that’s a different story :)

To answer your question, which programming language are you most comfortable with? You would see the most number of people creating DLLs in either VB or C# – if you’re comfortable with either of these 2 languages then you can download the Express Editions of either and write your DLLs there. For this article, I have used Microsoft Visual Basic 2008 Express Edition. The exact same DLL would be created if I were using C#.

Reply

Rohith Vishwanath March 8, 2010 at 1:16 pm

Hello Anshoo,

I was trying to access the dll from a project called White. This is the site for the same – http://white.codeplex.com/
I in fact created my own dll which would refer this dll and create my own class which would work on some SWT objects. I was not able to do so. Could you please let me know how to refer this white dll or my own class which I have written.

namespace Designer
{
    public class FloorArea
    {
        public FloorArea()
        {
            //C#
        }
        void Configuration(String Item)
        {
            Application Configurator = Application.Attach("javaw");
            Window InfraStruXure = Configurator.GetWindow("InfraStruXure");
            InfraStruXure.Get(SearchCriteria.ByText(Item)).Click();
        }

    }
}

From QTP:
I was trying – set obj = DotNetFactory.CreateInstance(“Designer.FloorArea”,”C:\Documents and Settings\rvishwan\Desktop\Designer\Designer\bin\Debug\Designer.dll”)

This is really important as the QTP is not able to recognize the object on the application, but White does.

Hoping to hear from you soon.

Reply

Anshoo Arora March 8, 2010 at 1:35 pm

Hi Rohith,

I currently have VB Express Edition installed on my laptop. I can try to test your code from there. This is its conversion to VB:

Namespace Designer
    Public Class FloorArea
        Public Sub New()
            'C#
        End Sub
        Private Sub Configuration(ByVal Item As [String])
            Dim Configurator As Application = Application.Attach(javaw)
            Dim InfraStruXure As Window = Configurator.GetWindow(InfraStruXure)
            InfraStruXure.[Get](SearchCriteria.ByText(Item)).Click()
        End Sub
       
    End Class
End Namespace

Reply

Anshoo Arora March 8, 2010 at 1:37 pm

Oh wait, I do not have the correct references. Is this the correct link for the download: http://white.codeplex.com/wikipage?title=Is%20White%20the%20right%20choice&referringTitle=Home

If possible, can you e-mail me the files to anshoo [dot] arora [at] relevantcodes [dot] com.

Reply

Sarnendu De March 4, 2010 at 3:47 am

Can we call qtp from asp.net c#?

Reply

Anshoo Arora March 4, 2010 at 7:39 pm

If you mean, you would like to create an instance of QTP through C#? Then, yes, you can :)

Reply

suni February 22, 2010 at 7:33 am

Hi Anshoo,

can we load DLL to QTP without DOTNetFactory

Thanks
suni

Reply

Hari Sreekanth Upadrasta February 24, 2010 at 4:58 am

if the dll is a c language dll then use extern object or if it is a com dll then find out the namaspace and class in it and use it using createobject method. In the above case you will have to make the dll com enabled and then you can use it with createobject method.
Regards,
Hari

Reply

saikrishna February 28, 2012 at 11:41 pm

2 Hari Sreekanth Upadrasta February 24, 2010 at 4:58 am
if the dll is a c language dll then use extern object or if it is a com dll then find out the namaspace and class in it and use it using createobject method. In the above case you will have to make the dll com enabled and then you can use it with createobject method.
Regards,
Hari

HI hari can you please tell me how to make “dll com ” enable so that i can use create object plese

Reply

Anshoo Arora February 24, 2010 at 8:22 am

Hari,

I thought the Extern object could be used for any DLL regardless of its underlying technology/language.

You’re absolutely correct about CreateObject: the assembly must be marked as COM visible.

Reply

Rohith Vishwanath March 8, 2010 at 11:44 pm

I have mailed you the dlls..

Reply

Anshoo Arora March 8, 2010 at 11:53 pm

Thank you Rohith. Just received your e-mail.. I will try them in the morning tomorrow. Its quite late here :)

Reply

Rohith Vishwanath March 9, 2010 at 12:53 am

Yes. That is the syntax. But I am getting the error – The Format of the file “white.core.dll” is invalid.

Reply

Anshoo Arora March 9, 2010 at 10:40 pm

Rohith,

The syntax is working perfectly on my end. Its quite strange that you’re seeing that error on the same DLL and I am not.. Not very sure what may be causing this. I did however get a lot of search hits for the following keywords:

The Format of the file “dll” is invalid.

Reply

Anonymous March 12, 2010 at 12:30 am

Sorry for the delayed reply. I was out of town.
I just figured out that it only with the version of QTP (which is 9.0) we are using has this problem and working fine with 9.2.
I guess we can declare that the problem is with the version of QTP we are using.

Anyway, Thanks for all your effort Anshoo :)

Reply

Anshoo Arora March 12, 2010 at 10:11 am

Ah! Excellent observation. I never thought of that.

Glad its working now :)

Reply

Christian Deserich April 3, 2010 at 9:47 pm

Just for future reference, I am using 9.1 and it has the DotNetFactory, so it must be 9.1 and later.

Reply

Anshoo Arora April 3, 2010 at 10:09 pm

Christian: Thanks, and good to see you here :)

I will update this post with the correct version information.

Reply

rezapb April 14, 2010 at 4:10 pm

Hi Anshoo:

This is not a code issue, but rather an application issue (actually just an annoyance).

But I can give a cleaner example:
Let’s say I have the following C# very simple code:

namespace Foo {
  public class Bar {
      int x;
      public int Num { get { return x; } set { x= value; }}
   }
}

and I compile it into c:\foo.dll using whatever means I have.

Next I use QTP:

Set foo = DotNetFactory.CreateInstance("Foo.Bar", "c:\foo.dll")
msgBox foo.Num & " is the num"

Now suppose I leave QTP open and then I make some changes to my C# code and then I try to recompile the c# code back into c:\foo.dll, I am not able because QTP has usurped the file. I cannot even remove it at the command prompt using del c:\foo.dll

Once I shut QTP down, then I am able to recompile my DLL

Just wondering if there is any solution to this issue

Reply

Anshoo Arora April 16, 2010 at 5:44 am

Does QTP exhibit the same behavior if you dispose the object foo?

Reply

rezapb April 16, 2010 at 11:49 am

Yes, I tried Set foo = Nothing, but I still am not able to recompile my DLL until I close QTP…

Reply

Anshoo Arora April 17, 2010 at 10:57 am

Still unable to reproduce this issue.. I would suggest you to copy the DLL to another location and use that instead of the file in your project’s folder. :)

Reply

Chris Davis June 10, 2010 at 8:12 am

You mentioned that you can have intellisense for classes by altering the registry. What keys would your add/edit to control that?

Thanks!
Chris

Reply

Anshoo Arora June 10, 2010 at 2:13 pm

Hi Chris,

Please refer to this article by Yaron.

Reply

rezapb September 9, 2010 at 10:34 am

Hi Anshoo:

Glad to see that I am not the only one who noticed this behavior. In my earlier comment you told me you were unable to reproduce this issue. At least now I know I’m not crazy!

But seriously this post has helped me out a lot, I used this technique to do some validation on some files that were generated by my AUT, and it worked out really nice because I was able to code and debug the custom DLL independently of QTP, however after that part was done I used QTP to automate the gui operations which generate the output files and the call my custom DLL using DotNetFactory.

Cheers,
Reza

Reply

Anshoo Arora September 9, 2010 at 2:04 pm

Strangely so, and I think it could be because I was using a different version but with 10.0 – I keep seeing this issue every time. I haven’t been using custom DLLs much these days and using the old-fashioned way of scripting everything from scratch. If I do require using DLLs, I RegAsm them as a codebase assembly. It makes it easier for everyone in the team to know what methods are available to them as DotNetFactory does not offer IntelliSense.

If you would like to register your assembly, you can use the following code snippet:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe C:\MyDLL.dll /codebase

or simply:

RegAsm C:\MyDLL.dll /codebase

The only thing you have to make sure when registering codebase assemblies is that the assembly must be strong-named. It will work even if it isn’t but it will throw an error when you run the above command. Once you do this, you can call all methods using CreateObject in QTP.

Reply

Kedar Sawant September 30, 2010 at 2:13 am

yes the Namespace is declared in the Global Section.

Is there anything that I need to declare the class as a Com class

Reply

Anshoo Arora November 14, 2010 at 4:48 pm

You have to create the assembly as a COM-visible assembly. The class must be public, so should the methods that are being called.

Reply

Kedar Sawant January 3, 2011 at 12:51 am

Anshoo,

I tried doing the same with in VB and C#, but was able to access the dll functions developed in C#. As that was working well in C# I did not do any furthur RnD for VB dlls.

Reply

Anshoo Arora January 10, 2011 at 7:30 am

Sounds good Kedar. I would have probably done the same for the time being. Btw, I have been told about this issue by a few other folks as well, and each time with VB.NET.

Reply

Previous post:

Next post: