文章关键字 ‘VBScript’

对VBS效率的再思考——处理二进制数据

2011年02月16日,星期三

那天无意中搜到一篇名为《我为什么喜欢VBS和C?》的文章,里面引用了《VBS和C语言效率比较》中的程序。

Option Explicit
Dim begin_time,end_time,elapse_time
Dim str,length,i,c
Dim ado,fso,file
begin_time = Timer
Set ado = CreateObject("adodb.stream")
Set fso = CreateObject("scripting.filesystemobject")
Set file = fso.OpenTextFile("foo.txt",2,True)
ado.Type = 1
ado.Open
ado.LoadFromFile("foo.jpg")
str = ado.Read
length = LenB(str)
For i = 1 To length
    c = AscB(MidB(str,i,1))
    file.WriteLine c & "," & "_"
Next
ado.Close
end_time = Timer
elapse_time = end_time - begin_time
WScript.Echo elapse_time

时隔半年多再看这段代码,实在没有什么效率可言。正如有人在《用VBS读写二进制文件》中的回复:

(更多…)

用VBS监视进程创建和删除

2011年01月27日,星期四

微软脚本中心里的例子,用到了WMI事件,抄下来备查。

监视进程的创建,在每次创建新的进程时,临时事件消费程序都发出警报。

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
    ExecNotificationQuery("select * from __instancecreationevent " _
        & " within 1 where TargetInstance isa 'Win32_Process'")
i = 0
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    Wscript.Echo objLatestProcess.TargetInstance.Name
Loop

监视进程的删除,在每次进程终止时,临时事件消费程序都发出警报。

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colMonitoredProcesses = objWMIService. _
    ExecNotificationQuery("select * from __instancedeletionevent " _
            & "within 1 where TargetInstance isa 'Win32_Process'")
i = 0
Do While i = 0
    Set objLatestProcess = colMonitoredProcesses.NextEvent
    Wscript.Echo objLatestProcess.TargetInstance.Name
Loop

参考链接:

  1. 监视进程的创建
  2. 监视进程的删除

用VBS检测U盘插入和弹出事件(二)

2011年01月9日,星期日

鉴于很多人反映之前写的那篇在XP下无效,做了一下修改。说是修改,其实是直接复制粘贴脚本专家的代码。

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colEvents = objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 10 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

Do While True
    Set objEvent = colEvents.NextEvent
    If objEvent.TargetInstance.DriveType = 2 Then 
        Select Case objEvent.Path_.Class
            Case "__InstanceCreationEvent"
                Wscript.Echo "Drive " & objEvent.TargetInstance.DeviceId & _
                    " has been added."
            Case "__InstanceDeletionEvent"
                Wscript.Echo "Drive " & objEvent.TargetInstance.DeviceId & _
                    " has been removed."
        End Select
    End If
Loop

参考链接:How Can I Determine When a Removable Drive Gets Connected?

用VBS实现PHP的md5_file函数

2011年01月4日,星期二
Function md5_file(filename, raw_output)
    Dim HashedData, Utility, Stream
    Set HashedData = CreateObject("CAPICOM.HashedData")
    Set Utility = CreateObject("CAPICOM.Utilities")
    Set Stream = CreateObject("ADODB.Stream")
    HashedData.Algorithm = 3
    Stream.Type = 1
    Stream.Open
    Stream.LoadFromFile filename
    Do Until Stream.EOS
        HashedData.Hash Stream.Read(1024)
    Loop
    If raw_output Then
        md5_file = Utility.HexToBinary(HashedData.Value)
    Else
        md5_file = HashedData.Value
    End If
End Function

参考链接:HashedData Object

用VBS实现PHP的sha1_file函数

2011年01月3日,星期一
Function sha1_file(filename, raw_output)
    Dim HashedData, Utility, Stream
    Set HashedData = CreateObject("CAPICOM.HashedData")
    Set Utility = CreateObject("CAPICOM.Utilities")
    Set Stream = CreateObject("ADODB.Stream")
    HashedData.Algorithm = 0
    Stream.Type = 1
    Stream.Open
    Stream.LoadFromFile filename
    Do Until Stream.EOS
        HashedData.Hash Stream.Read(1024)
    Loop
    If raw_output Then
        sha1_file = Utility.HexToBinary(HashedData.Value)
    Else
        sha1_file = HashedData.Value
    End If
End Function

参考链接:HashedData Object