I recently came to a situation where I needed to perform some task in a VBScript that exceeded what the language permitted. I could add a new DLL that includes the needed functionality which the script could make use of. Ideally you'd probably want to use some form of Visual Basic to create the DLL, since at would allow all your data types to match what the script was using. I didn't really want to use Visual Basic, so decided that if it was possible in C# then that's how I'll create my library.
First we'll need a library in C#, let's just use something simple to demonstrate:
namespace Demo {
class COMTest {
public int SimpleAddition(int iVal1, int iVal2) {
return iVal1 + iVal2;
}
}
}
There we go. Now we'll need to inform the C# compiler that we'll need to expose the COMTest class and the SimpleAddition() function for COM Interop. To do this we'll need to add the System.Runtime.InteropServices namespace and mark those pieces as COM visible.
using System.Runtime.InteropServices;
namespace Demo {
[ComVisible(true)]
class COMTest {
[ComVisible(true)]
public int SimpleAddition(int iVal1, int iVal2) {
return iVal1 + iVal2;
}
}
}
Compiling this thing is a tad more complex. We'll need to add our library to the GAC (general assembly cache) and add it to the registry. This means we need to have it be strongly named, so let's start there.
First we need to generate a key for our new library. This requires the strong name tool, sn. You can either get this with the Microsoft .NET SDK or with Mono. You just call it as sn -k keyfile.snk to have it generate the key and save it in the file keyfile.snk.
Next when you compile your library you include the key using the /keyfile option. You'll also want to change the target to library so the compiler is aware that you'd like a DLL. The whole command should look something like this:
csc /target:library /out:MyCOMLib.dll MyCOMLib.cs /keyfile:keyfile.snk
After that completes you'll need to add your library to the GAC using gacutil. Again, this comes with the Microsoft .NET SDK as well as Mono. The command will look like this:
gacutil /i MyCOMLib.dll
Last step is the registration. You'll need the regasm tool for this. This comes with the Microsoft .NET framework, but there's no equivalent with Mono. I'm assuming if you really needed this elsewhere you'd have to pull some tricks in Wine, I haven't tried this so I'm not really sure. Anyway, on windows you just give this command:
regasm /codebase MyCOMLib.dll
After all this your .NET DLL is created and available for use within VBScript. Here's what your code in VBScript would look like to make use of this:
Set oComTest = CreateObject("Demo.COMTest")
MsgBox oComTest.SimpleAddition(1, 2)
CreateObject() instantiates your .NET object, and then you can call any of the visible methods or access the public properties.
Just as a not, if your .NET library depends on other .NET libraries, then you'll need to put all of those libraries into the GAC. You only need to register the one that VBScript will directly use though.
No comments:
Post a Comment