Full Text

December 30th, 2007

Code of the Samurize

I wrote a VB script for Samurize, an easy-to-use, free Windows desktop playground for creating MacOS-like widgets and powerful system monitoring tools. Building my own desktop widget, I discovered that there wasn’t a built-in tool or any plugins available for identifying the current WiFi security mode in use. That is, I want my widget to tell me if I’m connected via WEP, WPA, etc. On my Linux systems, I can see at a glance what network I’m connected to and what security is in use. In Windows, this is slightly more challenging.

It wasn’t terribly difficult to create my own once I learned a bit about navigating the Windows Management Instrumentation, and I also learned that Samurize doubles as a handy testing ground for all kinds of VB scripts. Here is the result (click on the image to see my desktop widget that I’ve integrated this into):

Wireless Mode Indicator

You can download my Samurize config file, which contains the script and indicator icons, from the Samurize website. I have also included the VB code below.

VBA Code:

'================================================================
'Get WiFi Connection Mode (WEP/WPA Status)
'
'Version 0.02:  Added WiFiModeByImg function 12.30.07
'Version 0.01:  Initial Release 12.29.07
'_______________________________________________________________
'
'Code by Raindog samurize@daqron.com
'                www.daqron.com
'_______________________________________________________________
'
'Key to the numeric codes returned:
'  0 : WEP is in use
'  2 : Connection is unsecured
'  4 : WPA-PSK is in use
'  6 : WPA is in use
'  7 : Disconnected
'================================================================

On Error Resume Next

'================================================================
'String Constants
'================================================================

'Image root
const ImageDir = "C:\Program Files\Samurize\Skins\WiFi\"

'WMI query
const Qry = _
"Select * from MSNdis_80211_WEPStatus where active=true"

'================================================================
'Connect to the WMI database
'================================================================

'I adapted this bit from David Wheeler's wlan.vbs script; I
'assume it's a fairly standard way to interface with the WMI

Private Sub GetWMI(WMIArray, WMIQuery)
  Wstr = "winmgmts:{impersonationLevel=impersonate}!\\.\root\wmi"
  Set WMIClass = GetObject(Wstr)
  Set WMIArray = WMIClass.ExecQuery(WMIQuery)
End Sub

'================================================================
'Get connection mode by number
'================================================================

Function WiFiModeByNum()

  Call GetWMI(objMSNdis_80211_WEPStatus, Qry)

  For Each objMSNdis_80211_WEPStatus in objMSNdis_80211_WEPStatus
    WiFiModeByNum = objMSNdis_80211_WEPStatus.Ndis80211WEPStatus
  Next

End Function

'================================================================
'Get connection mode by image
'================================================================

Function WiFiModeByImg()

  Call GetWMI(objMSNdis_80211_WEPStatus, Qry)

  For Each objMSNdis_80211_WEPStatus in objMSNdis_80211_WEPStatus
    WiFiModeByImg = objMSNdis_80211_WEPStatus.Ndis80211WEPStatus
  Next

  'Customize these images as you see fit.
  'secure.png is green, secure2.png is orange,
  'and unsecure.png is red. Default, secure_off.png, is grey.

  If WiFiModeByImg = 6 Then
    WiFiModeByImg = ImageDir & "secure2.png"
  ElseIf WiFiModeByImg = 0 Then
    WiFiModeByImg = ImageDir & "secure.png"
  ElseIf WiFiModeByImg = 4 Then
    WiFiModeByImg = ImageDir & "secure2.png"
  ElseIf WiFiModeByImg = 2 Then
    WiFiModeByImg = ImageDir & "unsecure.png"
  End If

End Function

'================================================================
'Get connection mode by name
'================================================================

Function WiFiModeByName()

  Call GetWMI(objMSNdis_80211_WEPStatus, Qry)

  For Each objMSNdis_80211_WEPStatus in objMSNdis_80211_WEPStatus
    WiFiMode = objMSNdis_80211_WEPStatus.Ndis80211WEPStatus
  Next

  'Customize the values returned as you see fit. Default
  'is ??? in case some other number is returned that is
  'unknown to this script.

  WiFiModeByName = "???"
  If WifiMode = 0 Then
    WiFiModeByName = "WEP"
  ElseIf WifiMode = 2 Then
    WiFiModeByName = "Open"
  ElseIf WifiMode = 4 Then
    WiFiModeByName = "WPA-P"
  ElseIf WifiMode = 6 Then
    WiFiModeByName = "WPA"
  ElseIf WifiMode = 7 Then
    WiFiModeByName = "N/A"
  End If

End Function
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • Technorati
  • Facebook
  • LinkedIn
  • Twitter
Posted in Code  |  No Comments

Leave a Reply