Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C# dll in vbscript

Tags:

c#

dll

vbscript

qtp

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.

like image 474
Edward Leno Avatar asked Sep 20 '09 20:09

Edward Leno


People also ask

What is a calling function in C?

Function Calling: A function call is an important part of the C programming language. It is called inside a program whenever it is required to call a function. It is only called by its name in the main() function of a program. We can pass the parameters to a function calling in the main() function.

How do you call C in C++?

Just declare the C++ function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code: extern "C" void f(int);

Can I code C in Mobile?

You can add C and C++ code to your Android project by placing the code into a cpp directory in your project module. When you build your project, this code is compiled into a native library that Gradle can package with your app.


2 Answers

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

like image 172
Edward Leno Avatar answered Sep 30 '22 01:09

Edward Leno


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

like image 41
flq Avatar answered Sep 30 '22 03:09

flq