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?
Asked
Active
Viewed 4.6k times
20

bluish
- 26,356
- 27
- 122
- 180

steventnorris
- 5,656
- 23
- 93
- 174
-
1possible duplicate of [How to call C# DLL function from VBScript](http://stackoverflow.com/questions/769332/how-to-call-c-sharp-dll-function-from-vbscript) – JeffO Mar 23 '12 at 13:06
-
I did check that link, but it doesn't quite get at what I want. I'm looking to dynamically load the dll at runtime through the VBScript from a given filepath rather than register with the server. Again, dlls are very new to me, so this may be what's happening in that answer, I just don't see it. – steventnorris Mar 23 '12 at 13:24
3 Answers
29
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

user692942
- 16,398
- 7
- 76
- 175

Nilpo
- 4,675
- 1
- 25
- 39
-
3I only succeded using regasm and then `CreateObject`. I never could make `GetObject` work. Am I missing something? Thanks! – bluish Oct 22 '12 at 14:10
-
4When 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.**_ – HighTechProgramming15 Jan 07 '17 at 09:59
-
@HighTechProgramming15 Double check the path you have provided to the dll file. – Nilpo Jan 07 '17 at 22:49
-
1
-
1
-
1
-
1Could 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? – kampi Feb 09 '18 at 13:48
-
-
1@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. – kampi Feb 11 '18 at 20:39
-
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? – Jacob M. Aug 05 '19 at 17:21
-
@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? – Mahyar Mottaghi Zadeh May 04 '20 at 12:40
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.

Community
- 1
- 1

Cheran Shunmugavel
- 8,319
- 1
- 33
- 40
1
-
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? http://support.microsoft.com/kb/106553 – steventnorris Mar 23 '12 at 13:10
-
your link talks about visual basic. not vbscript. and your question has already been answered: http://stackoverflow.com/a/769346/736170 – Jimmy D Mar 23 '12 at 13:14
-
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. – steventnorris Mar 23 '12 at 13:23