我试图用VBScript中的C#编写一个方法。
我已经遵循了我在网上找到的所有说明,但仍然遇到问题。
具体来说,我正在
错误:ActiveX组件无法创建对象
代码:800A01AD
到目前为止,我已完成以下工作:
ComVisible(true)
regasm /codebase
我的VBScript看起来像这样:
set oObject = CreateObject("TTTTTT.FFFFF.CCCCCCCCC")
我的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()
{
//
}
}
}
有人可以帮忙吗?
答案 0 :(得分:4)
你读过这篇文章: COM Interop Exposed
在文章的last page(3)中,有一个列表:
ClassInterface(ClassInterfaceType.None)
”
属性以防止regasm / tlbexp
从创建一个空的默认值
接口我不确定GAC和ClassInterfaceType.None
是否是您拼图中缺失的部分,您可能想尝试一下。祝你好运!
答案 1 :(得分:4)
您的代码没有任何问题,您按照正确的安装程序进行了操作。但是,您获得的错误代码清楚地表明脚本解释器无法定位或加载程序集。解决这个问题的最佳方法是使用SysInternals的ProcMon实用程序。
我毫无困难地运行了您的代码,这些是ProcMon日志中最相关的条目:
22 12:04:41.1795038 PM WScript.exe 55280 RegOpenKey HKCR\TTTTTT.FFFFF.CCCCCCCCC SUCCESS Desired Access: Read
26 12:04:41.1795682 PM WScript.exe 55280 RegOpenKey HKCR\TTTTTT.FFFFF.CCCCCCCCC\CLSID SUCCESS Desired Access: Read
29 12:04:41.1796996 PM WScript.exe 55280 RegQueryValue HKCR\TTTTTT.FFFFF.CCCCCCCCC\CLSID\(Default) SUCCESS Type: REG_SZ, Length: 78, Data: {3EB62C37-79BC-44F7-AFBD-7B8113D1FD4F}
34 12:04:41.1797653 PM WScript.exe 55280 RegOpenKey HKCR\CLSID\{3EB62C37-79BC-44F7-AFBD-7B8113D1FD4F} SUCCESS Desired Access: Read
62 12:04:41.1802539 PM WScript.exe 55280 RegOpenKey HKCR\CLSID\{3EB62C37-79BC-44F7-AFBD-7B8113D1FD4F}\InprocServer32 SUCCESS Desired Access: Read
71 12:04:41.1804181 PM WScript.exe 55280 RegQueryValue HKCR\CLSID\{3EB62C37-79BC-44F7-AFBD-7B8113D1FD4F}\InprocServer32\(Default) SUCCESS Type: REG_SZ, Length: 24, Data: mscoree.dll
824 12:04:41.2425662 PM WScript.exe 55280 RegQueryValue HKCR\CLSID\{3EB62C37-79BC-44F7-AFBD-7B8113D1FD4F}\InprocServer32\1.0.0.0\CodeBase SUCCESS Type: REG_SZ, Length: 124, Data: file:///c:/projects/ClassLibrary2/obj/Debug/ClassLibrary2.DLL
... Lots of .NET keys...
1239 12:04:41.2970169 PM WScript.exe 55280 CreateFile C:\projects\ClassLibrary2\obj\Debug\ClassLibrary2.dll SUCCESS Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
答案 2 :(得分:2)
这是一个只需几步的简单项目,可以帮助您入门。
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,使类型库显式化,添加一个显式的ProgId等等。
ps:仅供参考,此示例显示了在为interop注册的类上重载的.NET方法会发生什么。方法名称附加了一个隐含的_2(_3,_4等)。
答案 3 :(得分:1)
要使Cheeso的示例在64位平台上运行,请确保以下列方式调用CScript.exe:
%windir%\SysWOW64\cscript.exe test.vbs
答案 4 :(得分:0)
csc.exe /t:library AClass.cs /keyfile:Foo.snk - produce your key file with VS!
regasm /codebase /tlb AClass.dll
VS正在添加一些像/ warn:/ noconfig这样的东西,还会添加对System.Core等其他程序集的引用,有时会导致错误。
对我来说,从csc编译工作。