Gra*_*ant 5 .net c# vbscript com-interop regasm
我试图用VBScript中的C#编写一个方法.
我已经遵循了我在网上找到的所有说明,但仍然遇到问题.
具体来说,我得到了
错误:ActiveX组件无法创建对象
代码:800A01AD
到目前为止,我已经做了以下事情:
ComVisible(true) regasm /codebase 我的VBScript看起来像这样:
set oObject = CreateObject("TTTTTT.FFFFF.CCCCCCCCC")
Run Code Online (Sandbox Code Playgroud)
我的C#代码如下所示:
using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace XXXXX.YYYYY
{
[ComVisible(true)]
[Guid("3EB62C37-79BC-44f7-AFBD-7B8113D1FD4F")]
[ProgId("TTTTTT.FFFFF.CCCCCCCCC")]
public class CCCCCCCCC
{
public void MyFunc()
{
//
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
这是一个简单的项目,只需几个步骤即可帮助您入门。
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
构建:
(produce your .snk file, once)
csc.exe /t:library /debug+ /keyfile:Foo.snk /out:TestObj.dll TestObj.cs
regasm /codebase TestObj.exe
Run Code Online (Sandbox Code Playgroud)
然后运行 vbscript(通过 cscript.exe)。
一旦你让基本的东西工作起来,你就可以调整它,添加 GAC,使类型库显式,添加显式的 ProgId,等等。
ps:仅供参考,此示例显示了注册互操作的类上重载 .NET 方法时会发生什么情况。方法名称后附加了隐式 _2(_3、_4 等)。