ASP调用dll及封装dll实例,封装为dll可以提供运行效率,加密代码。
vbDLL和vcDLL的代码参考 《打通任督二脉:VB6.0的ActiveX DLL或exe调用VC6.0的DLL,含dll参数加密,VB调试vbDLL,VC调试vcDLL》。
写个ASP程序运行在IIS中,文件名test.asp,代码如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Session.CodePage=65001%>
<%Response.charset="utf-8"%>
<%'有上面这3行才不会出现asp本地调试utf-8中文无法显示问题%>
<%
lngN1 = 1
lngN2 = 100000000 '1亿次 在360浏览器好像只能运算5秒,超过5秒自动断开无法看到结果,建议用Chrome浏览器
lngN1=clng(lngN1)
lngN2=clng(lngN2) 'ASP只有一种数据类型variant,只能用强制转换,否则调用DLL会出现类型不匹配
if 1=1 then '开关,由于ASP太慢,所以这里设置个开关,如果1=1就运行,1=2就不运行
time1 = Now()
For i = lngN1 To lngN2
n = n + i
Next
time2 = Now()
Response.Write "【ASP运算】"&now&"
"
Response.Write "time1: " & time1 & "
"
Response.Write "time2: " & time2 & "
"
Response.Write "耗时: " & DateDiff("s", time1, time2) & " s
"
Response.Write "结果: " & lngN1 & "累加到" & lngN2 & "=" & n & "
"
end if
Set ObjDll = Server.CreateObject("vbdll.ClassFun")
if 1=1 then '开关,如果1=1就运行,1=2就不运行
Response.Write "-------------------------------------
"
Response.Write ObjDll.f_say ("【VB的ActivX_DLL运算】"&now&"
")
'输入字符串可以被打印出来
time1 = Now()
m=ObjDll.f_acc(""&lngN1, ""&lngN2)
time2 = Now()
Response.Write "time1: " & time1 & "
"
Response.Write "time2: " & time2 & "
"
Response.Write "耗时: " & DateDiff("s", time1, time2) & " s
"
Response.Write "结果: " & lngN1 & "累加到" & lngN2 & "=" & m & "
"
end if
if 1=1 then '开关,如果1=1就运行,1=2就不运行
Response.Write "-------------------------------------
"
Response.Write ObjDll.f_say ("【VB的ActivX_DLL调用vcDLL运算】"&now&"
")
time1 = Now()
m=ObjDll.f_acc_vc_use(""&lngN1, ""&lngN2)
time2 = Now()
Response.Write "time1: " & time1 & "
"
Response.Write "time2: " & time2 & "
"
Response.Write "耗时: " & DateDiff("s", time1, time2) & " s
"
Response.Write "结果: " & lngN1 & "累加到" & lngN2 & "=" & m & "
"
end if
%>
运行test.asp文件结果显示如下:
【ASP运算】2023/1/17 0:32:14
time1: 2023/1/17 0:31:44
time2: 2023/1/17 0:32:14
耗时: 30 s
结果: 1累加到100000000=5.00000005E+15
-------------------------------------
【VB的ActivX_DLL运算】2023/1/17 0:32:14
time1: 2023/1/17 0:32:14
time2: 2023/1/17 0:32:15
耗时: 1 s
结果: 1累加到100000000=5.00000005E+15
-------------------------------------
【VB的ActivX_DLL调用vcDLL运算】2023/1/17 0:32:15
time1: 2023/1/17 0:32:15
time2: 2023/1/17 0:32:15
耗时: 0 s
结果: 1累加到100000000=1874919424
当然由于VC6.0的数值局限性,这里是错的。
===========================================================
ASP+DLL对于更复杂的运用,大家可以通过这个实例向外扩展就可以了,比如:
Public Sub connstr()
Set conn = MyServer.CreateObject("ADODB.Connection")
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & MyServer.MapPath("data.mdb")
Set rs = conn.Execute("select * from news")
Do While Not rs.EOF
MyResponse.Write (rs("news_title") & "
")
rs.MoveNext
Loop
rs.Close
Set conn = Nothing
End Sub
这个是用数据库连接的代码封装,当然这里要添加ADO引用。
PS:此DLL不要用壳压缩,否则无法运行。记得每次更新DLL,需要重启下IIS,否则IIS会占用这个DLL。
VS2022的C++耗时1秒。VC6.0无法运算那么大的数据,就用VS2022的long long数据类型计算:
#include
using namespace std;
int main()
{
long long sum = 0;
// 100000000这个数太大,
// 求和后超过了int型能表示的范围,
// 采用long long这一数据类型
for (unsigned int i = 1; i <= 100000000; ++i)
sum += i;
cout << sum << endl;
system("pause");
return 0;
}
如果要在VB6.0中调用VS2022的DLL,代码较多,请看 《VB6.0调用VS2022的C++写的DLL,网上很多错的,这里才是最简单的:一定要采用DEF文件》