Tuesday
May292012
QTP - Calling C# DLL in VBScript
Tuesday, May 29, 2012 at 1:05AM
A while ago I wanted to call a C# dll from a QTP script. I posted my question here on stackoverflow.
Short Explanation
- Create a C# dll using Visual Studio.
- Use the DotNetFactory in QTP to call the public methods in the C# dll.
Step 1 - Create a C# DLL
Create Visual Studio Solution and Project
- Start Visual Studio 2010
- File > New > Project
- .NET Framework 3.5
- Visual Studio > Windows > Class Library
- Name = mydll
- Location = c:\
- Solution name = ClassLibrary
- OK
Create Public Method
- Rename Class1.cs to functions.cs. Yes to rename all references.
- Create a public method: public int GetValue() { return 34; }
Build DLL
- Save
- Build > Build Solution
- Look in C:\ClassLibrary\mydll\bin\Debug (or Release) for the mydll.dll
- Close Visual Studio 2010
Step 2 - Create QTP Script
Create QTP Script
- Start QTP
- File > New > Test
- Set obj = DotNetFactory.CreateInstance("mydll.functions", "c:\\classlibrary\\mydll\\bin\\debug\\mydll.dll")
- MsgBox "GetValue() from C# dll returns : " & obj.GetValue()
Run QTP Script
- Run the script.
- Message box with '34' is displayed.
Reader Comments