Search code examples
c++vbscriptcomcreateobject

What is the purpose of using a prefix with WScript.CreateObject?


Does anyone know the actual purpose of using a prefix when using WScript.CreateObject?

I have a C++ COM project example and it has a VB Script file that calls a method of COM object that's in the C++ dll project.

Function Testing_Event(Text)
  WScript.Echo CStr(Text)
End Function

Dim Test
Set Test = WScript.CreateObject("Hello.World", "Testing_")
Test.Method("Hello World!")

I also found an another answer that have the same calling behavior with a prefix.

Above works fine with VB Script, but if I remove the prefix, wscript runs but outputs nothing like before.

Please can anyone explain me what is the usage of Testing_ prefix here? Is it possible to stop using that prefix and call this method normally like Hello.World? Do I need to make any changes to example's source files to call this method without this prefix?


Solution

  • Testing_ is an event prefix, so in your example when Test raises an event called Event then prefix+event_name maps to Testing_Event and that local procedure is called.

    If you omit the prefix then no events will be sunk, so if you need events you need the prefix.

    The rationale for doing it this way is that it it allows multiple instances of the same class to raise their own independent events by specifying different prefixes.