这是一个简单的项目,只需几个步骤即可开始。
C#代码:
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly:System.CLSCompliant(true)]
[assembly: ComVisible(true)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7d9c5cd3-73d4-4ab1-ba98-32515256c0b0")]
namespace Cheeso.ComTests
{
[Guid("7d9c5cd3-73d4-4ab1-ba98-32515256c0b1")]
public class TestReply
{
public string salutation;
public string name;
public string time;
}
[Guid("7d9c5cd3-73d4-4ab1-ba98-32515256c0b2")]
public class TestObj
{
// ctor
public TestObj() {}
public TestReply SayHello(string addressee)
{
return SayHello(addressee, "hello");
}
public TestReply SayHello(string addressee, string greeting)
{
string x = String.Format("{0}, {1}!", greeting, addressee);
Console.WriteLine("{0}", x);
TestReply r = new TestReply
{
salutation = greeting,
name = addressee,
time = System.DateTime.Now.ToString("u")
};
return r;
}
}
}
的VBScript客户端代码:
Function Main()
Dim obj
Dim reply
set obj = CreateObject("Cheeso.ComTests.TestObj")
Set reply = obj.SayHello("Evgeny")
WScript.Echo "Reply at: " & reply.time
Set reply = obj.SayHello_2("Evgeny", "wassup")
WScript.Echo "Reply at: " & reply.time
End Function
Main
编译:
(produce your .snk file, once)
csc.exe /t:library /debug+ /keyfile:Foo.snk /out:TestObj.dll TestObj.cs
regasm /codebase TestObj.exe
然后,只需运行VBScript(通过的Cscript.exe)。
一旦你得到了基本的工作,你可以调整它,添加GAC,使typelib明确,添加一个明确的ProgId等等。这个例子显示了在一个为interop注册的类上重载的.NET方法会发生什么情况。有一个隐含的_2(_3,_4等)附加到方法名称。
你能张贴的C#类类的声明,包括封闭的命名空间声明和类的任何属性? – itowlson 2009-11-14 03:11:31
嗨,我用我的代码更新了我的问题。 – Grant 2009-11-14 03:14:07
你见过http://stackoverflow.com/questions/769332/how-to-call-c-dll-function-from-vbscript? – abatishchev 2010-06-21 20:29:26