在 VBScript 中重写 CreateObject 函数

发布于 2024-07-08 08:22:56 字数 391 浏览 5 评论 0原文

我想用我自己的函数覆盖 VBScript 中默认的 CreateObject() 函数。

基本上这个例子在VB6中:

http://www.darinhiggins.com/the-vb6 -createobject-function/

我无法弄清楚这一行是:

 Set CreateObject = VBA.CreateObject(Class$, ServerName$)

How do Irefer to "VBA" in VBSript?

I want to override the default CreateObject() function in VBScript with my own.

Basically this example in VB6:

http://www.darinhiggins.com/the-vb6-createobject-function/

I cannot figure out is this line:

 Set CreateObject = VBA.CreateObject(Class$, ServerName$)

How do I refer to "VBA" in VBSript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒绊 2024-07-15 08:22:56

这个快速测试似乎有效......

Function CreateObject(className, serverName)
   '---- override the CreateObject
   '     function in order to register what
   '     object is being created in any error message
   '     that's generated
   Dim source, descr, errNum

   WScript.echo "In custom CreateObject"
   If Len(serverName) > 0 Then
      Set CreateObject = WScript.CreateObject(className, serverName)
   Else
      Set CreateObject = WScript.CreateObject(className)
   End If

End Function

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject", "")
path = fso.GetAbsolutePathName(".")

WScript.echo path

不能保证! ;-)

This quick test seems to work...

Function CreateObject(className, serverName)
   '---- override the CreateObject
   '     function in order to register what
   '     object is being created in any error message
   '     that's generated
   Dim source, descr, errNum

   WScript.echo "In custom CreateObject"
   If Len(serverName) > 0 Then
      Set CreateObject = WScript.CreateObject(className, serverName)
   Else
      Set CreateObject = WScript.CreateObject(className)
   End If

End Function

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject", "")
path = fso.GetAbsolutePathName(".")

WScript.echo path

No guarantees! ;-)

爱的那么颓废 2024-07-15 08:22:56

我不认为你可以覆盖它,以便所有代码都使用它,只有你的代码。

在这种情况下,它的名称并不重要(除非您有大量无法更改的现有代码)。 您可以将其称为 CreateObjectEx() 或 ExCreateObject() 或类似的名称吗? 让这个函数添加所有错误处理等,然后转身调用 main/core CreateObject() 方法

I don't think you can override it so that all code will use it, only YOUR code.

In which case, it doesn't matter what it's called (unless you have tons of existing code you can't change). Can you call it CreateObjectEx() or ExCreateObject() or something like that? Have this function add all your error handling and such and then turn around and call the main/core CreateObject() method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文