Calling C# dll in vbscript

Edward Leno picture Edward Leno · Sep 20, 2009 · Viewed 8k times · Source

I am trying to call a C# dll from QTP (uses vbscript). I have tried a number of things with no success:

  • Visual Studio 2010
  • Create C# class libary (st.dll)

code:

using System;
using System.Collections.Generic;
using System.Text;   

namespace st
{
    public class Class1
    {
        public static int GetValue()
        {
            return 34;
        }
    }
}
  • regasm /codebase st.dll
    • fails 'because it is not a valid .NET assembly'

In QTP/vbscript, I have tried

  • extern.Declare micInteger, "GetValue", "e:\st.dll", "GetValue"
    • Returns message: 'Invalid procedure call or argument'

Regardless of QTP, I would greatly appreciate any insight on how to call the c# dll from a .vbs file.

Answer

Edward Leno picture Edward Leno · Sep 21, 2009

I was able to get this working by doing the following:

Create a new C# dll in VS 2010.

namespace st4
{
    public class st4_functions
    {
        public int GetValue()
        {
            return 34;
        }
    }
}

In QTP I added the following lines:

Set obj = DotNetFactory.CreateInstance("st4.st4_functions", "c:\\st4.dll")
MsgBox obj.GetValue()

Thanks to all that responded to my problem. Though I did not do the COM solution, it got me thinking that I could stay with .NET and led to this solution. Good job all!

EDIT:

I created a blog post to detail the steps and provide additional information:

http://www.solutionmaniacs.com/blog/2012/5/29/qtp-calling-c-dll-in-vbscript.html