English 中文(简体)
Why does VBScript sometimes block in WshShell.Exec?
原标题:

I ve got an html application (HTA) that uses WshShell.Exec to get the version of Windows. I m using wmic os get Caption to get the specific version, which works fine on the command line and in a batch script. I ve also tested the way I m calling WshShell.Exec and it works fine with other commands (i.e. echo Windows 2008). The problem occurs when I try to combine these things the Exec seems to just freeze. Can you recommend a way around this? Here s my code:

Function GetWinVersion
     Returns 2008, XP, or 7
    set WshShell = CreateObject("WScript.Shell")
    set oExec = WshShell.Exec("wmic os get Caption")
    do while oExec.Status = 0
         I added this very busy wait, though it doesn t seem to help
         Would sleep if it was available in an hta
    loop
    While oExec.StdOut.AtEndOfStream <> True
        thisLine = oExec.StdOut.ReadLine
         MsgBox "Found line: " & thisLine
        if InStr(thisLine, "2008") > 0 then
            GetWinVersion=2008
            Exit Function
        elseif InStr(thisLine, "XP") > 0 then
            GetWinVersion=XP
            Exit Function
        elseif InStr(thisLine, "Windows 7") > 0 then
            GetWinVersion=7
            Exit Function
        end if
    Wend
    MsgBox "Error parsing output of wmic os get Caption"
    self.Close
End Function
最佳回答

WMIC is a wrapper for WMI, which you can use directly in VBS;

function GetWinVersion
    dim WMI: set WMI = GetObject("winmgmts:\.
ootcimv2")
    dim colResults: set colResults = WMI.ExecQuery("Select * from Win32_OperatingSystem")
    dim item
    for each item in colResults
        GetWinVersion = item.caption
    next
end function
问题回答

暂无回答




相关问题
How can I query NTFS disk quotas in C#?

I need to be able to find, for all users on a given remote machine, those users disk quotas and actual disk usage. I need to be able to do this reporting in a C# application. (Well, technically a DLL ...

WMI Poor Performance

I wrote a code in C# that maps logical drives to their physical disks, using WMI (System.Management). The code working perfectly, but slow like hell. In my machine (Windows 7 x64, Dual-Core with 3 GB ...

Closing Open Files using C#

I have a situation where people are connected to files on a share and it s blocking me from overwriting the file. I m trying to write a method that will look to see if a filePath that I provide is ...

Binding an SSL certificate to a port programmatically

I m working on a self-hosted WCF service for which encrypted communications is an option. Everything works fine when a certificate is already bound to the port as described here. However, I want to ...

热门标签