« SQUID - "The specified module could not be found" | Main | LoadRunner - Copying Parameters Between Scripts »
Tuesday
May292012

QTP - Calling C# DLL in VBScript

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

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>