6

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Edward Leno
  • 6,257
  • 3
  • 33
  • 49
  • Have you marked it as COM-visible? http://msdn.microsoft.com/en-us/library/ms182157(VS.80).aspx – Marc Gravell Sep 20 '09 at 20:47
  • I have gone into Properties > Assembly Information and checked 'Make assembly COM-Visible. Still get the above errors. – Edward Leno Sep 20 '09 at 21:13
  • Still struggling ... I removed the 'static' keyword, ran VS 2010 as admin and turned on Properties > Build > Register for COM Interop. – Edward Leno Sep 20 '09 at 21:41
  • For those, like, me, who didn't knwo what Qtp was...it's TLA for "Quick Test Professional" and this page here...http://www.advancedqtp.com/ seems to talk a bit about it. – flq Sep 21 '09 at 07:55

3 Answers3

9

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

Edward Leno
  • 6,257
  • 3
  • 33
  • 49
1

As Marc said, but I think it merits an answer. If you ensure that your dll will be available though the COM mechanics, your script should be able to call into it with things like CreateObject.

How to register .NET assembly for COM interop

flq
  • 22,247
  • 8
  • 55
  • 77
  • I made the assembly COM-Visible. In QTP/vbs I have added: Dim example Set example = CreateObject("st.Class1") But I get an error: ActiveX component can't create object: 'st.Class1' – Edward Leno Sep 20 '09 at 21:49
1

Your function is static. Static class members can't be matched up to interface members, and if it can't implement a .NET interface then it certainly won't implement a COM interface.

Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
  • I have removed the static keyword. Same problem. Are there any tutorials on getting a simple class to work in this way without QTP? I am looking for the simplest example possible to then understand what I need to do. – Edward Leno Sep 20 '09 at 21:52