Sometimes you need functionality that QTP does not provide. Other times you may need to interact with an existing DLL. Using a simple C# DLL, that we create, I will show how you can easily call it in QuickTest Pro.
Creating a Simple .NET DLL
Open Visual Studio (If you don't have a copy of Visual Studio you can download a free copy of Visual C# Express.) and create a new 'Class Library' project named QTP.UI.Automation:

Rename Class1 to QTP:

Next let's add a simple function that take two numbers and adds them together:

Go to the QTP.UI.Automation properties and make sure you have the following:

Make sure the 'Make assembly COM-Visible' is checked.

Go to the Build section and make sure that the 'Register for COM interop' is checked:

Build your solution (F6) and make sure there are no errors.
Call Your DLL in QuickTest Pro
Start QTP and add the following code:
Set oTestCom = CreateObject("QTP.UI.Automation.QTP")
msgbox oTestCom.Add(6,4)
QTP should return 10

Cool – that's it.

{ 17 comments… read them below or add one }
May appear as a very basic question.
What is the relation between COM and DLL?
Hi Malay – the way I understand it is the DLL contains your functions and COM (Component Object Model) enables QTP to be able to communicate with the DLL. Cool~Joe
Thank you for this – opens up all sorts of new possibilities for work and play for me.
ElizaF » Awesome – have fun! Cheers~Joe
Hi Joe,
Good informative article..
I have the following queries regarding this topic –
1. Where is this DLL saved. If I want to execute the same logic in some other machine, the how do I copy the DLL?
2. Can you specify any real scenarios where using this approach will be helpful?
Thanks
Hi Joe,
How QTP knows that, the method exists in that dll file?
I know, you included the reference i.e “QTP.UI.Automation”, does it mean while building the solution it saves the solution reference some where and when i refere this in QTP, it looks in that location?
Regards,
Uday
Uday » When you build the DLL in Visual Studio I believe that it automatically enters all the Method info in the registry. If you wanted to use the DLL on another machine you need to use RegAsm.exe to register it. Does that make sense? Thanks for the question! Cheers~Joe
Hi Joe,
Thanks for providing this interesting stuff. Can you please share the information of how Intellisense works for the custom dll.
In the above example you provided, after creating the object oTestCom, Intellisense should prompt with the available functions (in this case “Add” function)
Thanks
KG
KG » Thanks KG! Good question – I have not tried myself yet. AdvancedQTP had an excellent article on Intellisense and COM that might help you: http://www.advancedqtp.com/2008/03/intellisense-and-com/ . Cheers~Joe
Thanks Joe. Also I found an interesting article on Intellisense using .NET
http://relevantcodes.com/intellisense-for-your-custom-class-in-6-steps/
Cheers
KG
Hi Joe,
i Added developers dll in reference to subcribe event . Finally i created the dll to use in qtp. But it doesnt give the triggered event state. what would be the problem?
Dim oEventObject
Dim pjstate1,cjstate1
Function subscribepj(pjstates)
msgbox pjstates
End Function
Function subscribecj(cjstates)
msgbox cjstates
End Function
Set pjstate1 = GetRef(“subscribepj”)
Set cjstate1 = GetRef(“subscribecj”)
Set oEventObject = DotNetFactory.CreateInstance(“JobManager.Listener”,”C:\Documents and Settings\uarumuga\My Documents\visual studio 2010\projects\JobManager\JobManager\bin\Debug\JobManager.dll”)
msgbox oEventObject.getcjstate(cjstate1)
msgbox oEventObject.getpjstate(pjstate1)
Nice one. This helped me a lot. Those COM checkboxes did the trick for me. Couldn’t find that tip anywhere else.
lagunascorpio » Great! Glad to hear it worked for you.
hi,
if I want use the dll in the other pc, how can i do?
leon » Hi Leon – you should be able to copy the dll over to another machine that has .net installed. Make sure to place it in the same folder location
Hi Joe, one question:
i can use description.create in visual basic .net?
Thanks¡¡
Is there a VB DLL example? instead of C#
Thanks
Mark
{ 1 trackback }