How to Call C# Dll Function from Vbscript

How to call C# DLL function from VBScript

You need to mark your assembly as COM visible by setting the COMVisibleAttribute to true (either at assembly level or at class level if you want to expose only a single type).

Next you register it with:

regasm /codebase MyAssembly.dll

and finally call it from VBScript:

dim myObj
Set myObj = CreateObject("MyNamespace.MyObject")

Using DLLs in VBScript

Important: Both methods will work only if the DLL exposes a COM interface.

If your dll is registered with the system, use CreateObject with it's ProgID.

Set myObject = CreateObject("MyReallyCoolObject.HelloWorld")
myObject.Print

If your object is not registered on the system, use GetObject with a path to the file containing your object. Make sure your object exposes the proper interface. (The second parameter is optional. Here you can provide a class name if your object exposes more than one.)

Set myObject = GetObject("C:\some\path\helloworld.dll", "appname.HelloWorld")
myObject.Print

Calling C# dll in vbscript

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

Call a function in a console app from VBScript

Idealistically, you should be making a DLL and set Com Visible on the functions you need to expose.

using System;
using System.Runtime.InteropServices;
namespace MyDLL
{
[ComVisible(true)]
public class Operations
{
[ComVisible(true)]
public int AddIntegers(int a, int b)
{
return a + b;
}
}
}

After you've compiled your DLL you need to register it with regasm.exe so that you can call it from VBScript:

Dim myObj
Set myObj = CreateObject("MyDLL.Operations")
Dim sum
sum = myObj.AddIntegers(3, 5)

This reply is based on the CodeProject posting How to call a .NET DLL from a VBScript by Raymund Macaalay. I recommend you read it.

Also, you should check other stackoverflow posting such as How to call C# DLL function from VBScript.

C# DLL from VBScript, no regasm

No, registration is required, the only way that the script interpreter can find the DLL that contains the ProgId that you use in the script's CreateObject() call. Isolated COM with a manifest doesn't work, you can't modify the manifest for the script interpreter.

There is a technical solution, you can write registry keys in the HKCU registry hive without acquiring UAC elevation. The Regasm.exe tool always writes them in the HKLM hive. That registers the assembly as well, but only for the user that runs Regasm.exe. It is however pretty painful and easy to get wrong, you have to write your own registration method and apply the [ComRegisterFunction] attribute. It is now your job to use the RegistryKey class to set the keys. Same for the [ComUnregisterFunction], it should delete the keys again. There are a lot of bad examples out on the interwebs, best way to get this right is to use SysInternals' ProcMon to observe the registry keys that get written when you use Regasm.exe normally, then reproduce that in your own code, using HKCU instead.

Do note the other side of that medal, you are in fact making configuration changes to the machine that allows arbitrary code to run. Trying to hide that doesn't do the user any favors and should never be considered if you honor the user's desire to keep the machine safe and predictable. UAC is not there to stop you from making changes, it is only there to inform the user about it.

Cannot call C# dll from VBscript in SYSPRO

SYSPRO is a 32 bit application, so if you try and run a 64 bit dll from within SYSPRO, it cannot find it because it is looking for the 32 bit version.

I solved this by registering the dll using 32 bit command prompt, regasm and then it worked correctly.



Related Topics



Leave a reply



Submit