views:

69

answers:

1

My first attempt is to query Win32_OperatingSystem for the caption, and test whether the caption "equals" the operating system I am testing for:

Dim objWMIService, strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")


msgbox getOperatingSystemCaption()
msgbox meetsOperatingSystemRequirement()



Function getOperatingSystemCaption()

     Dim strCaption, colOperatingSystems, objOperatingSystem

     Set colOperatingSystems = objWMIService.ExecQuery _
          ("Select * from Win32_OperatingSystem")

     For Each objOperatingSystem in colOperatingSystems
        strCaption = objOperatingSystem.Caption
        Exit For 
     Next

    getOperatingSystemCaption = strCaption

End Function





Function meetsOperatingSystemRequirement()

    meetsOperatingSystemRequirement = False 

    If getOperatingSystemCaption() = "Microsoft Windows 7 Home Premium" Then 

       meetsOperatingSystemRequirement = True

    End If 


End Function

I suppose I can use InStr, however I still do not understand why the "Caption" and my string are not equal.

+2  A: 

Are you sure you have "Microsoft Windows XP" and not "Microsoft Windows XP Professional" ?. If you use "=" sign, then you will not catch it because it expects to match exact string. Use instr() would be better if you want partial match. Otherwise, add in "Professional"

You can put in some debugging after caption is found

....
        msgbox strCaption & " " & len(strCaption)
        getOperatingSystemCaption = strCaption
....

and try a different way

.....
    myCaption = getOperatingSystemCaption()
     msgbox myCaption & " " & len(myCaption)
    If myCaption = "Microsoft Windows XP Premium Home" Then 
......

check the length as well...

ghostdog74
Thanks for picking that up. I edited my question to reflect that I am trying to check for "Microsoft Windows 7 Home Premium". For some reason, I cannot get the strings to match without using instr(). Possible MS bug?
jJack
if you have specified to exact string, it should work.
ghostdog74
Thanks ghostdog74! It turns out the Caption property is padded with a space at the end--but only for Microsoft Windows 7 Home Premium (so far). So, testing for "Microsoft Windows 7 Home Premium " is a match.
jJack
Ok, then use Trim() to get rid of spaces in strCaption before returning from the function. that should do it
ghostdog74