Using DLLs in VBScript

41,043

Solution 1

Important: Both methods will work only if the DLL exposes a COM interface.

If your dll is registered with the system, use CreateObject with it's ProgID.

Set myObject = CreateObject("MyReallyCoolObject.HelloWorld")
myObject.Print

If your object is not registered on the system, use GetObject with a path to the file containing your object. Make sure your object exposes the proper interface. (The second parameter is optional. Here you can provide a class name if your object exposes more than one.)

Set myObject = GetObject("C:\some\path\helloworld.dll", "appname.HelloWorld")
myObject.Print

Solution 2

I think you might be looking for Registration-Free COM. This SO answer regarding the Microsoft.Windows.ActCtx should help specifically for VBScript.

Keep in mind that COM doesn't support static methods, so you'll have to make your Print method into an instance method.

Solution 3

How to call a .NET DLL from a VBScript

Share:
41,043
steventnorris
Author by

steventnorris

Applications developer with a preference for object-oriented languages. Currently working at FortyAU, a development firm in Nashville, TN. http://www.fortyau.com

Updated on July 05, 2022

Comments

  • steventnorris
    steventnorris almost 2 years

    I've compiled C# code into a DLL, but have little experience with them. My C# code contains a class HelloWorld with a static method Print(). I'd like to use this DLL in VBScript to call the method Print(). I know this is base, but I'm using this as a test for a larger scale project that will be compiled to DLL in the end. What's the declare look like for that and how would the method call look?

  • steventnorris
    steventnorris over 12 years
    Ok so that's how to register the DLL with the server/system as a whole, but how would I directly declare/call a dll in VBScript? I read the below link that seems to say it's possible, but how would that declare look if it were for a static method of a class? support.microsoft.com/kb/106553
  • Jimmy D
    Jimmy D over 12 years
    your link talks about visual basic. not vbscript. and your question has already been answered: stackoverflow.com/a/769346/736170
  • steventnorris
    steventnorris over 12 years
    That link does not answer my question. And apologies on the language mix-up. I do need this for VBScript, but I assumed there would be a similar process. I want to call the dll from my script without having to register with the server. It should be dynamically loaded at runtime from a file path.
  • bluish
    bluish over 11 years
    I only succeded using regasm and then CreateObject. I never could make GetObject work. Am I missing something? Thanks!
  • HighTechProgramming15
    HighTechProgramming15 over 7 years
    When I enter two parameters: I got an error: ActiveX Component can't create object 'GetObject'. When I enter only the first parameter: I got: File name or class name not found during Automation.
  • Nilpo
    Nilpo over 7 years
    @HighTechProgramming15 Double check the path you have provided to the dll file.
  • BLuM
    BLuM almost 7 years
    Is this possibile without registering dll? I don't think so.
  • Nilpo
    Nilpo almost 7 years
    @BLuM It is if it exposes a COM interface.
  • BLuM
    BLuM almost 7 years
    How to ezpose it?
  • kampi
    kampi over 6 years
    Could you please post a very simple example (about the dll and the vbscript as well) how to do this without the need to register the dll?
  • Nilpo
    Nilpo over 6 years
    @kampi There already is. Please see the second part of the answer.
  • kampi
    kampi over 6 years
    @Nilpo: you are right, but I meant the DLL side. Could you post a sample code is inside the DLL? I am interested in the GetObject way (dll without registration) Or do I have to make some changes? I always get the error people are describing in the previous comments.
  • Jacob M.
    Jacob M. almost 5 years
    I have the same results as @HighTechProgramming15 and cannot get to a working result with GetObject, but registering the DLL works. There has to be more to this?
  • Mahyar Mottaghi Zadeh
    Mahyar Mottaghi Zadeh about 4 years
    @Nilpo I am trying to inject the VBScript into another application and receive error <'CreateObject' is not declared. It may be inaccessible due to its protection level.">. The error is the same while trying 'GetObject' too. Any idea how to solve it?