[英]How do I call .NET code (C#/vb.net) from vbScript?
I imagine I can compile a C# DLL and then expose it as a COM object so that it can be CreateObject'd from VBscript. 我想我可以编译一个C#DLL,然后将它作为COM对象公开,以便它可以从VBscript创建。 I'm just not sure the steps involved in doing this...
我只是不确定这样做的步骤......
It can be very simple to do this. 这样做非常简单。 But there are a lot of places where it's not so simple.
但是有很多地方并不那么简单。 It depends a lot on what your class needs to be able to do, and how you intend to deploy it.
这很大程度上取决于您的课程需要做什么,以及您打算如何部署它。
Some issues to consider: 需要考虑的一些问题:
regasm /codebase
. regasm /codebase
注册它。 regasm
assign them, but they'll be different every time (and every place) the class is registered. regasm
分配它们,但是每次(和每个地方)这个类都注册时它们会有所不同。 If you need the GUIDs to remain invariant across installations, you'll need to mark members with the Guid
attribute. Guid
属性标记成员。 MarshalAs
attribute. MarshalAs
属性标记属性和方法。 InterfaceType
attribute. InterfaceType
属性。 There's a very good (if dated) article about COM interop and .Net here . 有一个关于COM互操作和.Net一个很好的(如果日)的文章在这里 。 (A lot of things that article talks about, like generating type libraries, is handled for you automatically now.) And Microsoft's documentation is up to date, but not quite so detailed.
(文章讨论的很多内容,比如生成类型库,现在都会自动处理。)而且Microsoft的文档是最新的,但不是那么详细。
You should use the regasm
utility to register an assembly (just like you do regsvr32
with COM servers). 您应该使用
regasm
实用程序来注册程序集(就像使用COM服务器执行regsvr32
一样)。 Then you can use it from COM. 然后你可以在COM中使用它。 Make sure it's installed in the GAC.
确保它已安装在GAC中。 The stuff should have
[ComVisible(true)]
to be usable from COM. 这些东西应该可以从COM中使用
[ComVisible(true)]
。
There's a "COM Accessible" option for each project you can select. 您可以选择的每个项目都有一个“COM Accessible”选项。 Then, of course, you have to register the assembly to use it, but that's about it.
然后,当然,你必须注册程序集才能使用它,但这就是它。
I had to go hunting a bit to find it for C# (I'm used to VB.Net), but you can find the option by double-clicking on the Properties
folder in the Solution Explorer, choosing the Application
tab and then clicking the Assembly Information...
button. 我不得不去寻找C#(我习惯VB.Net),但你可以通过双击解决方案资源管理器中的
Properties
文件夹找到该选项,选择Application
选项卡,然后单击Assembly Information...
按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.