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:
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:
I hope you find this article useful :)
{ 107 comments… read them below or add one }
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”)
Rajiv, what is the version of .NET framework of the assembly?
ho
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
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.
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.
Vishal: Can you please share the HTML source of the page?
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?
Xiaofu: I believe the type should be returned as ‘object’. What type of editor did you use? VSVB, VSC# or VSC++?
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!!
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……
Thanks!!
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
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 ClassHello 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!!
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.
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
Please Let me know How Did U reslove It
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?
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.
Prasad, this generally happens if you’re trying to access an invalid Class, method or Namespace or all three.
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
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?
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
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””””””””””””””””””””””””””””””’
Kedar,
Have you specified the Namespace as a global Namespace for the Projec?
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!!!
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?
Hi Anshoo,
I sent you my DLL code to your email id mentioned in one of the above posts. Please have a look.
Thanks
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
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
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
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
Vidya, its possible to pass arrays across .NET to other languages. What is your DLL code?
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,
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.
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.
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
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.
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
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.
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 ?
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.
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
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.
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.
What type of DLL is this? .NET DLL?
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!
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..
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?
I tried to reproduce this, but am unable to. Can you please share the code you used?
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
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.
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
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.
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
Vivek,
To register .NET DLLs, we use RegAsm.exe. Its the same as RegSvr32, but for .NET DLLs. :)
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
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: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 ClassThanks
Vivek
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=339332Hi 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
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. :)
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
Yes Vivek,
If I am not wrong, DOTNetFactory was supported version 9.2 onwards.
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
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.
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
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
ReadOnlyif it is missing theProperty Letstatement.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.
No.
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.
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 ClassAfter 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
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.
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
Vivek,
You can download the VB 2008 Express Edition from here.
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.
Yes, you can register almost anything to your objects. You will have to use
RegisterUserFuncto do so, but yes, when you are writing your code, you will have the intellisense available for that method.Yes Anshoo. That is the link to download White.
Yes Anshoo. That is the link to download White. The dll name is White.Core.dll
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..
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…
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#.
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.
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:
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.Can we call qtp from asp.net c#?
If you mean, you would like to create an instance of QTP through C#? Then, yes, you can :)
Hi Anshoo,
can we load DLL to QTP without DOTNetFactory
Thanks
suni
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
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
Hari,
I thought the
Externobject 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.I have mailed you the dlls..
Thank you Rohith. Just received your e-mail.. I will try them in the morning tomorrow. Its quite late here :)
Yes. That is the syntax. But I am getting the error – The Format of the file “white.core.dll” is invalid.
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:
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 :)
Ah! Excellent observation. I never thought of that.
Glad its working now :)
Just for future reference, I am using 9.1 and it has the DotNetFactory, so it must be 9.1 and later.
Christian: Thanks, and good to see you here :)
I will update this post with the correct version information.
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
Does QTP exhibit the same behavior if you dispose the object foo?
Yes, I tried Set foo = Nothing, but I still am not able to recompile my DLL until I close QTP…
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. :)
You mentioned that you can have intellisense for classes by altering the registry. What keys would your add/edit to control that?
Thanks!
Chris
Hi Chris,
Please refer to this article by Yaron.
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
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:
or simply:
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.
yes the Namespace is declared in the Global Section.
Is there anything that I need to declare the class as a Com class
You have to create the assembly as a COM-visible assembly. The class must be public, so should the methods that are being called.
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.
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.