How do I call .NET code (C#/vb.net) from vbScript?

后端 未结 3 1606
北荒
北荒 2021-02-04 18:59

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. I\'m just not sure the steps involved in doing this...

3条回答
  •  清酒与你
    2021-02-04 19:01

    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:

    • Your class has to have a parameterless constructor.
    • It can't expose static methods.
    • Is deploying your COM DLL in the global assembly cache OK? If not, you're going to have to give it a strong name and register it using regasm /codebase.
    • Do you care what GUIDs identify the class and its interfaces? If not, you can let regasm assign them, but they'll be different every time (and every place) the class is registered. If you need the GUIDs to remain invariant across installations, you'll need to mark members with the Guid attribute.
    • Are you going to use the default marshaling of data types between .NET and COM? If not, you're going to need to mark properties and methods with the MarshalAs attribute.
    • Does it matter to you what kind of COM interface your class exposes? If so, you're going to need to deal with the InterfaceType attribute.
    • Does your class need to raise or respond to events? If so, there are implications for how you design your class interface.

    There's a very good (if dated) article about COM interop and .Net here. (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.

提交回复
热议问题