从 JScript 代码调用用 VB 编写的函数(经典 ASP) [英] Call a function written on VB from a JScript code (classic ASP)

查看:14
本文介绍了从 JScript 代码调用用 VB 编写的函数(经典 ASP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我必须处理用 VB 编写的遗留系统.我不擅长 VB 和 ASP,所以我决定这个系统的新代码将用 JScript 编写.

Currently I have to deal with the legacy system written in VB. I'm not good with VB and ASP, so I decided that new code for this system will be written in JScript.

但是,两种语言之间的互操作性存在一些问题:即,当我尝试调用在 <script language="vbscript"> 标记中声明的某些函数时,它会失败出现Object expected"错误(如果页面语言是 VBScript),反之亦然.

However, there is some problem with interoperability between the two languages: namely, when I'm trying to call some function declared in <script language="vbscript"> tag, it fails with a "Object expected" error (if the page language is VBScript), and vice versa.

即如下代码:

inc.asp

<script language="vbscript" runat="server">
Sub VBTestFunction(Message)
    Response.Write "VBTestFunction: " & Message
End Sub
</script>
<script language="javascript" runat="server">
function JSTestFunction(Message) {
    Response.Write("JSTestFunction: " + Message);
}
</script>

testjs.asp

<%@ Language="JavaScript" %>
<!-- #include file="inc.asp"-->
<script language="javascript" runat="server">
    VBTestFunction("from javascript");
    JSTestFunction("from javascript");
</script>
<script language="vbscript" runat="server">
    Call VBTestFunction("from vbscript")
    Call JSTestFunction("from vbscript")
</script>

失败并出现以下错误:

VBTestFunction: from vbscript
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'JSTestFunction'
/test.asp, line 9

(如果我要评论特定的行,其他三个语句都可以正常工作);将页面语言设置为 VBScript

(if I'll comment the specific line, other three statements will work fine); setting the page language to VBScript

<%@ Language="VBScript" %>
<!-- #include file="inc.asp"-->
<script language="javascript" runat="server">
    VBTestFunction("from javascript");
    JSTestFunction("from javascript");
</script>
<script language="vbscript" runat="server">
    Call VBTestFunction("from vbscript")
    Call JSTestFunction("from vbscript")
</script>

失败并出现以下错误:

Microsoft JScript runtime error '800a138f'
Object expected
/test.asp, line 4

(同样,如果我将特定行注释掉,其他三个语句将正常工作).

(again, if i'll comment out the specific line, other three statements will work fine).

有一篇MSDN 文章介绍了在同一个应用程序中混合 VB 和 JS,但从文章看来,示例代码应该可以工作,因为 TestFunction 在另一个文件中声明并且毕竟是一个函数.

There is an MSDN article on mixing VB and JS in the same application, but from the article it seems that the example code should work, as TestFunction is declared in another file and is a function after all.

有没有办法让这一切工作,并从 VB 和 JS 代码调用 VBTestFunctionJSTestFunction ?我想应该有一个,否则JS和VB混合就没有意义了.

Is there some way to make all this thing working, and to call both VBTestFunction and JSTestFunction from both VB and JS code? I guess there should be one, otherwise there would be no point in mixing JS and VB.

推荐答案

我一直在关注这个问题,Salman 已经回答了很多,但有些事情需要澄清.首先,被引用的文章存在关键问题.它说执行顺序是这样的:-

I've been monitoring this question for a while and Salman has pretty much answered it but there are some things that could be clarified. First of all there is key problem with the article being referenced. It says that the order of execution is this:-

1.以非默认语言在元素中编写脚本
2.内联脚本
3.以默认语言在元素中编写脚本

1.Script in elements in nondefault languages
2.Inline script
3.Script in elements in the default language

它是错误的,或者至少它已经过时了(它毕竟引用了 IIS4).内联脚本"(即默认语言的脚本)的处理方式与相同语言的脚本元素没有任何区别.

Its wrong, or at least its out of date (it does after all reference IIS4). The "inline script" (that is script in the default language) is not treated any differently than script elements of the same language.

下面是解释正在发生的事情的方法.

Here is how to reason through what is happening.

  • 在任何解析开始之前,所有的包含点都被解析并被包含文件中的内容替换,以创建一个单一的词法文件".这是在进行任何解析之前创建的.

  • Before any parsing begins all the include points are resolved and replaced by content from the include files to create a single lexical "file". This is created before any parsing takes place.

从这个文件"中收集每种语言的脚本代码.您可以想象在找到每个块时附加多个文件(每种语言一个).请注意,这就是为什么默认语言的 <% %><script runat="server" 实际上无法区分的原因.

Script code is collected from this "file" for each language. You can imagine multiple files (one per language) being appended to as each chunk is found. Note that this is the reason why <% %> and <script runat="server" for the default language are practically indistiguishable.

任何静态内容(即 runat="server" 脚本标签或 <% %> 之外的内容)都被视为一部分默认语言的代码.创建了一种特殊形式的 Response.Write,它将静态内容字节逐字发送到响应中,并将其附加到在原始文件中找到的默认语言代码.

Any static content (that is content outside of a runat="server" script tag or <% %>) is considered to be part of the code for the default language. A special form of Response.Write that sends the static content bytes verbatim to the response is created and appended to the default language code at the point it was found in the original file.

现在我们有一个或多个脚本准备好由各自的脚本引擎处理.非默认语言脚本首先被解析执行.为函数或变量创建的任何全局标识符都将添加到脚本环境中.但是,由于此时根本没有处理默认语言脚本,因此它可能随后添加到全局脚本环境中的任何内容都不可用.

Now we have one or more scripts that are ready to be processed by their respective script engines. The non-default language scripts are parsed and executed first. Any global identifier created, be that for a function or a variable, is added to the script environment. However since the default language script has not been processed at all at this point nothing that it might subsequently add to the global script environment is yet available.

在解析和执行默认语言脚本时,由先前语言脚本创建的所有全局标识符都将添加到脚本环境中,因此可以从内联代码中使用.

By the time the default language script is parsed and executed all the global identifiers created by the previous language scripts will have been added to the script environment and are therefore available for use from inline code.

您应该仔细注意默认语言函数可以在条件下由非默认语言的函数中的代码调用对非默认函数的调用可以追溯到默认语言的内联执行.

You should note carefully that a default language function can be called by code in a function in the non-default language on the condition that call into the non default function can be traced back to the inline execution of the default language.

例如,如果默认语言是 JScript,您可以让 VBScript 函数 (fnA) 调用在 JScript (fnB) 中声明的函数只要对 fnA 的调用是作为 JScript 内联执行的一部分进行的.IOW JScript 可以调用 VBScript,VBScript 又可以调用 VBScript 等等,限制因素是在这种情况下这个链的顶部必须是 JScript 的引擎.

For example if the default language is JScript you can have a VBScript function (fnA) call a function declared in JScript (fnB) as long as the call to fnA is made as part of the inline execution of JScript. IOW JScript can call into VBScript which in turn can call into VBScript and so on, the limiting factor is the engine that the top of this chain must be JScript in this scenario.

在您的示例代码中,您的 VBScript 全局(内联)级别的代码试图调用以默认 JScript 语言声明的函数.如果您按照上面的项目符号进行操作,您将看到在执行时被调用的函数不存在.

In your example code you had code at the global (inline) level of your VBScript attempting to call a function declared in the default JScript language. If you follow the above bullets you will see that at the point it is executed the the function being called does not exist.

这篇关于从 JScript 代码调用用 VB 编写的函数(经典 ASP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆