吐个槽,对VB6.0 还有VBS 说ByeBye

往事不堪回首,折腾了个把月的老系统,心中郁结,不吐不快。系统架构是ASP +VBS +VB6.0 + SQL Server2000, 第一个版本开发完成大概是在2000年。基本是处于交接无力,看代码就如同走迷宫的节奏。不过我觉得第一批开发人员应该可以自傲了,它们做的系统这么多年过去了居然还活着。

为了对得起这几天死掉的脑细胞,就权且在这里留下它们阵亡的痕迹。吐个槽,对VB6.0 还有VBS 说ByeBye

变量的声明

VB6.0:

Dim x as integer

VBS:

Dim x

同样的功能,由于代码太过Dirty,有部分模块写在了ASP 的VBS中,有部分模块写在了COM 组件的VB6.0 中。某日直接拷贝VB6.0 的代码修改到VBS, 始终无法运行成功。

用>1 hour 脑细胞的代价,发现是声明变量的时候不能照拷,不能声明变量的类型,或者干脆直接抹掉不写声明直接用即可。

变量的赋值

今天要给Session 赋值。

VBS:

工作正常的代码

set session("record") = record;

悲剧的代码

set session("userId") = 50

两个同样的语句,第一句能够正常运行,第二句居然不行。

正确的使用方式:

set session("record") = record;

session("age") = 50

用> 2 hour 脑细胞的代价,发现set 方式用来赋值对象,如果直接是数值,字符串之类的东西不能用set,而用let, 当然可以省略!

VBS 基础

虽然说脑细胞死了不少,这里还是希望找个坑将VBS 的基础知识埋了。VB6.0 的功能和VBS 类似,但是如果同时学同时都不精悲剧的概率极大,故这里就只讲VBS.

VBScript 历史

全名Visual Basic Scripting Editon

从桌面系统的Windows98,服务版的Windows NT4.0 开始微软家就自带支持VBS了。

在浏览器端,功能和Java Script 类似,显然除了IE其他浏览器都不买账,被干趴下了。

在系统管理员端,微软自家都建议用Windows PowerShell,况且原来也可以用BAT.

在服务器端,ASP + VBS的结构看上去就吓坏不少小盆友, 不过这种公司还活着几个。

估计就QTP + Excel 里面用用还是不错的。(既然评论里面有人说起了vba,为了防止误解,这里暂时抹掉Excel )

但是:吐个槽,对VB6.0 还有VBS 说ByeBye 查看某语言的月度排行,VB6.0 可是只比C# 低一点点的存在,高的一塌糊涂。这段和VBS 无关,不该放这里

套用网上流行的段子:VB VBA VBS

VB和VBA本就是同宗的姐妹,只不过姐姐VB的功夫要比妹妹VBA历害些。不过姐姐只会单打独斗是女强人;妹妹却只会傍大款。姐姐有生育能力,是真正的女人;妹妹却不会生崽,但深谱相夫之道,一番教导指挥之下可使她老公增色不少,而VBS呢,也是大户人家的女儿,不过没有VB和VBA姐妹优秀的血统,娇小玲珑干不得粗活只能指挥些自动听话的对象来干活,她乐于助人品德好不象VBA那样只认大款,VB、VBAvbs三个女人我都喜欢。

VBS 语言特性

VBS 是以Visual Basic 为模型。因此有类似的过程,结构,常量,变量,时间日期,异常处理,正则表达式等。。。。。。

Procedure :

Function

Subroutine

参数传递:

传值

传引用

无数内置的常量:

Color Constants   
Comparison Constants   
Date and Time Constants   
Date Format Constants   
Miscellaneous Constants   
MsgBox Constants   
String Constants   
Tristate Constants   
VarType Constants 

变量类型:

不用显式Dim 变量类型,默认基础类型都是Variant

当然如果不指定Option Explicit, 变量可以不声明直接用。

无数内置的方法:

。。。。。。。。。。

CDate

CDbl

Chr

CInt

CLng

Conversions

Cos

CreateObject

CSng

CStr

Date

各种关键字

Empty : uninitialized

False  :0

Nothing :对象没有指向东西

Null : 变量没有包含有效数据

True : –1

各种入门级别的小代码块

这里的小代码都整理和来自于网络,windows 平台的随便记事本保存下即可运行。

弹出Hello World:

msgbox true=-1
msgbox true=1
MsgBox "Hello World!"

更复杂的弹出窗口

' These three produce the same result. However, the use of constants as in the third line
' is considered best practice.
x = MsgBox("Hello World:Text",1+64+4096,"Hello World:Title")
x = MsgBox("Hello World:Text",4161,"Hello World:Title")
x = MsgBox("Hello World:Text", vbOKCancel+vbInformation+vbSystemModal, _
           "Hello World:Title")

' Presents the number corresponding to the button pressed. Different constants will produce
' different behaviours. For example, vbOKCancel specifies two buttons in the dialogue box,
' whereas vbYesNoCancel specifies three.
x = MsgBox("Hello World:Text", vbYesNoCancel+vbInformation,"Hello World:Title")
MsgBox "The result is " & x

调用WMI 关闭记事本

Option Explicit
Dim strComputer, strProcessToKill, objWMIService, colProcess, objProcess

strComputer = "."
strProcessToKill = "notepad.exe"
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" _
   & strComputer _
   & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
   ("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")
For Each objProcess in colProcess
   MsgBox "... terminating " & objProcess.Name
   objProcess.Terminate()
Next

创建文件

For i = 1 to 10
createFile( i )
Next

Public sub createFile(a)
Dim fso,myFile
filePath = "C:\file_name" & a & ".txt"
Set fso=CreateObject("Scripting.FileSystemObject")
Set MyFile= fso.CreateTextFile( filePath)
MyFile.WriteLine("This is a separate file")
msgbox 1111
'MyFile.close
stop
end sub

这个可以改成Merry Christmas,不过估计无知小妹妹会觉得你很无聊。

Dim WshShell
Set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.Run"notepad"
WScript.Sleep 1500
WshShell.AppActivate"Untitled-Notepad"
WshShell.SendKeys"H"
WScript.Sleep 500
WshShell.SendKeys"a"
WScript.Sleep 500
WshShell.SendKeys"p"
WScript.Sleep 500
WshShell.SendKeys"p"
WScript.Sleep 500
WshShell.SendKeys"y"
WScript.Sleep 500
WshShell.SendKeys""
WScript.Sleep 500
WshShell.SendKeys"B"
WScript.Sleep 500
WshShell.SendKeys"i"
WScript.Sleep 500
WshShell.SendKeys"r"
WScript.Sleep 500
WshShell.SendKeys"t"
WScript.Sleep 500
WshShell.SendKeys"h"
WScript.Sleep 500
WshShell.SendKeys"d"
WScript.Sleep 500
WshShell.SendKeys"a"
WScript.Sleep 500
WshShell.SendKeys"y"
WScript.Sleep 500
WshShell.SendKeys"!"
WScript.Sleep 500
WshShell.SendKeys"%FS"
WScript.Sleep 500
WshShell.SendKeys"b"
WScript.Sleep 500
WshShell.SendKeys"i"
WScript.Sleep 500
WshShell.SendKeys"r"
WScript.Sleep 500
WshShell.SendKeys"t"
WScript.Sleep 500
WshShell.SendKeys"h"
WScript.Sleep 500
WshShell.SendKeys"%S"
WScript.Sleep 500
WshShell.SendKeys"%FX" 

生成一个GUID, 不用打开baidu了。

Set TypeLib = CreateObject("Scriptlet.TypeLib")
strGUID = Left(TypeLib.Guid,38)
myfilename = "C:\guid.txt"
MakeHelloWorldFile myfilename strGUID

Sub MakeHelloWorldFile (FileName,guid)
'Create a new file in C: drive or overwrite existing file
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set FileObject = FSO.CreateTextFile (FileName)
   FileObject.WriteLine 

   FileObject.Close()
End Sub

当然可以直接用VBS 执行数据库脚本,不用打开SQL Management了。

.........省略

写在最后

这个古董项目看来是没完没了了,于是我果断裸辞滚蛋了,希望这辈子都不要再碰什么VB6.0, 什么ASP了,也希望接手的人一路走好。

C# 还有 Java, 哥又满血复活回来了,想哥了没?

上一篇:Atitit图像处理的用途


下一篇:create dll project based on the existing project