#RequireAdmin #cs WLAN Profiles Manager Author: Fred (www.carifred.com) This is a simple program that allows to perform WIFI related actions that have been removed from the Windows 8/8.1 interface, such as deleting saved WIFI profiles. This program can be easily extended to a full WIFI manager. #ce #Region Global constants Global Const $h__WLANAPI = DllOpen('Wlanapi.dll') Global Const $WLAN_CLIENT_VERSION = StringInStr('WIN_XP WIN_XPe WIN_2003',@OSVersion) ? 1 : 2 Global Const $tWLAN_INFO_LIST = 'DWORD;DWORD' Global Const $tGUID = 'DWORD Data1;WORD Data2;WORD Data3;BYTE Data4[8]' Global Const $tWLAN_INTERFACE_INFO = 'struct;'&$tGUID&';endstruct;WCHAR Description[256];LONG isState' Global Const $tWLAN_PROFILE_INFO = 'WCHAR ProfileName[256];DWORD dwFlags' Global Const $tagLVITEM = 'UINT mask;int iItem;int iSubItem;UINT state;UINT stateMask;ptr pszText;'& _ 'int cchTextMax;int iImage;LPARAM lParam;int iIndent;int iGroupId;UINT cColumns;UINT puColumns; int piColFmt;int iGroup' Global Const $LVM_GETITEMTEXT = 0x1000 + 115 Global Const $LVM_GETNEXTITEM = 0x1000 + 12 Global Const $LVM_SETCOLLUMNWIDTH = 0x1000 + 30 Global Const $LVM_DELETEALLITEMS = 0x1000 + 9 Global Const $LVM_DELETEITEM = 0x1000 + 8 Global Const $LVM_GETITEMCOUNT = 0x1000 + 4 Global Const $GUI_SHOW = 16 Global Const $GUI_HIDE = 32 Global Const $GUI_ENABLE = 64 Global Const $GUI_DISABLE = 128 Global Const $CB_GETCURSEL = 0x147 Global Const $CB_GETDROPPEDSTATE = 0x157 #EndRegion Global constants #Region Application Core Global $hClient = _WlanOpenHandle() Global $ExitVal = _Main() _WlanCloseHandle($hClient) Exit $ExitVal Func _Main() Local $aInterfaces = _WlanGetInterfaces($hClient) If @error Or $aInterfaces[0][0] = 0 Then MsgBox(16,'No WLAN Interfaces', 'No WLAN interfaces found.') Return 1 EndIf Local $hwnd = GUICreate('WLAN Profiles Manager',600, 500) GUISetFont(9) GUICtrlCreateLabel('WLAN profiles manager lists the available Wifi profiles in the local computer.'&@CRLF& _ 'It allows to connect/disconnet and delete the listed profiles.',20,20) GUICtrlCreateLabel('Please select an inteface:',20,80) Local $intCombo = GUICtrlCreateCombo('',180,76,380,-1,3) For $i = 1 To $aInterfaces[0][0] GUICtrlSetData(-1,$aInterfaces[$i][1],$aInterfaces[1][1]) Next Local $Msg, $aButton[6], $combosel = 0, $pcombosel = 0, $lvsel = -1, $plvsel = -1 Local $clabel = GUICtrlCreateLabel('No profiles found for the selected interface.', 220, 180,340,20,1) Local $listview = GUICtrlCreateListView(' Profile name| Type', 200,120, 380,360) GUICtrlSendMsg(-1,$LVM_SETCOLLUMNWIDTH,0,190) GUICtrlSendMsg(-1,$LVM_SETCOLLUMNWIDTH,1,160) Local $aProfiles = _ListWlanProfiles($listview, $clabel, $aInterfaces[1][0]) $aButton[0] = GUICtrlCreateButton('Disconnect', 20,140,160,25) $aButton[1] = GUICtrlCreateButton('Connect selected', 20,190,160,25) $aButton[2] = GUICtrlCreateButton('Delete selected', 20,240,160,25) $aButton[3] = GUICtrlCreateButton('Delete all', 20,290,160,25) $aButton[4] = GUICtrlCreateButton('Refresh', 20,340,160,25) $aButton[5] = GUICtrlCreateButton('Exit', 20,390,160,25) GUICtrlSetState($aButton[1], $GUI_DISABLE) GUICtrlSetState($aButton[2], $GUI_DISABLE) GUICtrlSetState($aButton[0], $GUI_DISABLE) Local $ProfileName, $state, $count, $connected = 0 If $aInterfaces[1][2] = 1 Then GUICtrlSetState($aButton[0], $GUI_ENABLE) $connected = 1 EndIf If $aProfiles[0][0] = 0 Then GUICtrlSetState($aButton[3], $GUI_DISABLE) GUISetState() While 1 $lvsel = GUICtrlSendMsg($listview, $LVM_GETNEXTITEM, -1, 2) If $lvsel <> $plvsel Then If $lvsel >= 0 And $plvsel >= 0 Then $plvsel = $lvsel ContinueLoop EndIf $state = ($lvsel < 0) ? $GUI_DISABLE : $GUI_ENABLE GUICtrlSetState($aButton[1], $state) GUICtrlSetState($aButton[2], $state) $plvsel = $lvsel EndIf $Msg = GUIGetMsg() Switch $Msg Case $intCombo ; Change interface If GUICtrlSendMsg($intCombo,$CB_GETDROPPEDSTATE,0,0) Then ContinueLoop $combosel = Int(GUICtrlSendMsg($intCombo,$CB_GETCURSEL,0,0),1) If $combosel <> $pcombosel Then $aProfiles = _ListWlanProfiles($listview, $clabel, $aInterfaces[$combosel+1][0]) $pcombosel = $combosel $state = ($combosel+1 = $connected) ? $GUI_ENABLE : $GUI_DISABLE GUICtrlSetState($aButton[0], $state) $state = $aProfiles[0][0] ? $GUI_ENABLE : $GUI_DISABLE GUICtrlSetState($aButton[3], $state) EndIf Case $aButton[0] ; Disconnect selected _WlanDisconnect($hClient,$aInterfaces[$combosel+1][0]) GUICtrlSetState($aButton[0], $GUI_DISABLE) $connected = 0 Case $aButton[1] ; Connect selected If $lvsel < 0 Then ContinueLoop $ProfileName = _LvGetItemText($listview,$lvsel) If _WlanConnect($hClient,$aInterfaces[$combosel+1][0],$ProfileName) Then GUICtrlSetState($aButton[0], $GUI_ENABLE) $connected = $combosel+1 EndIf Case $aButton[2] ; Delete selected If $lvsel < 0 Then ContinueLoop If MsgBox(52, 'Delete selected profile', _ 'Are you sure you want to delete the selected profile?',0,$hwnd) = 7 Then ContinueLoop $ProfileName = _LvGetItemText($listview,$lvsel) If _WlanDeleteProfile($hClient,$aInterfaces[$combosel+1][0],$ProfileName) Then _ GUICtrlSendMsg($listview, $LVM_DELETEITEM, $lvsel, 0) _ListWlanProfiles($listview, $clabel, $aInterfaces[$combosel+1][0]) Case $aButton[3] ; Delete all $count = GUICtrlSendMsg($listview, $LVM_GETITEMCOUNT,0,0) If $count = 0 Then ContinueLoop If MsgBox(52, 'Delete all profiles', _ 'Are you sure you want to delete the saved profiles belongint to this interface?', _ 0,$hwnd) = 7 Then ContinueLoop For $i = $count -1 To 0 Step -1 $ProfileName = _LvGetItemText($listview,$i) If _WlanDeleteProfile($hClient,$aInterfaces[$combosel+1][0],$ProfileName) Then _ GUICtrlSendMsg($listview, $LVM_DELETEITEM, $i, 0) Next GUICtrlSetState($aButton[3], $GUI_DISABLE) _ListWlanProfiles($listview, $clabel, $aInterfaces[$combosel+1][0]) Case $aButton[4] GUIDelete() Return _Main() Case -3, $aButton[5] Return 0 EndSwitch WEnd EndFunc ;==> _Main #EndRegion Application core #region Functions Func _LvGetItemText($lvId, $item, $subitem = 0) Local $sLVITEM = DllStructCreate($tagLVITEM) DllStructSetData($sLVITEM, 'mask', 1) DllStructSetData($sLVITEM, 'iSubItem', $subitem) DllStructSetData($sLVITEM, 'cchTextMax', 1024) Local $sText = DllStructCreate('WCHAR[1024]') DllStructSetData($sLVITEM, 'pszText', DllStructGetPtr($sText)) GUICtrlSendMsg($lvId, $LVM_GETITEMTEXT, $item, DllStructGetPtr($sLVITEM)) Return DllStructGetData($sText,1) EndFunc ;==> _LvGetItemText Func _ListWlanProfiles($listview, $clabel, $interface) Local $aProfiles = _WlanGetProfiles($hClient, $interface) If $aProfiles[0][0] = 0 Then GUICtrlSetState($listview, $GUI_HIDE) GUICtrlSetState($clabel, $GUI_SHOW) Else GUICtrlSetState($clabel, $GUI_HIDE) GUICtrlSendMsg($listview,$LVM_DELETEALLITEMS,0,0) For $i = 1 To $aProfiles[0][0] GUICtrlCreateListViewItem($aProfiles[$i][0]&'|'& _ _ProfileTypeStr($aProfiles[$i][1]),$listview) Next GUICtrlSetState($listview, $GUI_SHOW) EndIf Return $aProfiles EndFunc ;==> _ListWlanProfiles Func _ProfileTypeStr($state) Switch $state Case 0 Return 'Wifi profile' Case 1 Return 'Group policy Wifi profile' Case 2 Return 'User Wifi profile' EndSwitch Return 'Unknown Wifi profile' EndFunc ;==> _ProfileTypeStr Func _WlanGetInterfaces($hClientHandle) Local $aRet[1][3] = [[0]] If $hClientHandle = 0 Then Return SetError(1,0,$aRet) Local $pItf = _WlanEnumInterfaces($hClientHandle) If @error Then Return SetError(1,0,$aRet) Local $wlinfo = DllStructCreate($tWLAN_INFO_LIST,$pItf) Local $Total = DllStructGetData($wlinfo,1) If $Total = 0 Then Return $aRet Local $vPtr = $pItf + DllStructGetSize($wlinfo), $sIntInfo ReDim $aRet[$Total+1][3] For $i = 1 To $Total $sIntInfo = DllStructCreate($tWLAN_INTERFACE_INFO, $vPtr) $aRet[$i][0] = DllStructCreate($tGUID) _CopyStruct($sIntInfo, $aRet[$i][0], 4) $aRet[$i][1] = DllStructGetData($sIntInfo, 'Description') $aRet[$i][2] = DllStructGetData($sIntInfo, 'isState') $vPtr += DllStructGetSize($sIntInfo) Next $aRet[0][0] = $Total _WlanFreeMemory($pItf) Return $aRet EndFunc ;==> _WlanGetInterfaces Func _WlanGetProfiles($hClientHandle, $sGUid) Local $aRet[1][2] = [[0]] Local $pItf = _WlanGetProfileList($hClientHandle, $sGUid) If @error Then Return SetError(1,0,$aRet) Local $wlinfo = DllStructCreate($tWLAN_INFO_LIST,$pItf) Local $Total = DllStructGetData($wlinfo,1) If $Total = 0 Then Return $aRet Local $vPtr = $pItf + DllStructGetSize($wlinfo), $sProfInfo ReDim $aRet[$Total+1][2] For $i = 1 To $Total $sProfInfo = DllStructCreate($tWLAN_PROFILE_INFO, $vPtr) $aRet[$i][0] = DllStructGetData($sProfInfo, 'ProfileName') $aRet[$i][1] = DllStructGetData($sProfInfo, 'dwFlags') $vPtr += DllStructGetSize($sProfInfo) Next $aRet[0][0] = $Total _WlanFreeMemory($pItf) Return $aRet EndFunc ;==> _WlanGetProfiles Func _CopyStruct($sSrc, $sDst, $Elems = 1) If Not IsDllStruct($sSrc) Or Not IsDllStruct($sDst) Then Return SetError(1,0,0) For $i = 1 To $Elems DllStructSetData($sDst,$i,DllStructGetData($sSrc, $i)) Next Return 1 EndFunc ;==> _CopyStruct Func _WlanOpenHandle() Local $aCall = DllCall($h__WLANAPI, 'DWORD','WlanOpenHandle', _ 'DWORD',$WLAN_CLIENT_VERSION, 'ptr',0, 'DWORD*',0, 'HANDLE*',0) If Not @error Then Return SetExtended($aCall[3],$aCall[4]) EndFunc ;==> _WlanOpenHandle Func _WlanCloseHandle($hClientHandle) If $hClientHandle = 0 Then Return SetError(1,0,0) Local $aCall = DllCall($h__WLANAPI, 'DWORD','WlanCloseHandle', _ 'HANDLE',$hClientHandle, 'ptr',0) If Not @error Then Return Number($aCall[0] = 0) EndFunc ;==> _WlanCloseHandle Func _WlanEnumInterfaces($hClientHandle) If $hClientHandle = 0 Then Return SetError(1,0,0) Local $aCall = DllCall($h__WLANAPI, 'DWORD','WlanEnumInterfaces', _ 'HANDLE',$hClientHandle, 'ptr',0, 'ptr*',0) If Not @error And $aCall[0] = 0 Then Return $aCall[3] Return SetError(1,0,0) EndFunc ;==> _WlanEnumInterfaces Func _WlanGetProfileList($hClientHandle, $sGUid) Local $pGuid = IsDllStruct($sGUid) ? DllStructGetPtr($sGUid) : $sGUid Local $aCall = DllCall($h__WLANAPI, 'DWORD','WlanGetProfileList', _ 'HANDLE',$hClientHandle, 'ptr',$pGuid, 'ptr',0, 'ptr*',0) If Not @error And $aCall[0] = 0 Then Return $aCall[4] Return SetError(1,0,0) EndFunc ;==> _WlanGetProfileList Func _WlanFreeMemory($pmem) If $pmem = 0 Then Return SetError(1,0,0) DllCall($h__WLANAPI, 'none','WlanFreeMemory', 'ptr',$pmem) Return 1 EndFunc ;==> _WlanFreeMemory Func _WlanDeleteProfile($hClientHandle, $sGUid, $ProfileName) Local $pGuid = IsDllStruct($sGUid) ? DllStructGetPtr($sGUid) : $sGUid Local $aCall = DllCall($h__WLANAPI, 'DWORD','WlanDeleteProfile', _ 'HANDLE',$hClientHandle, 'ptr',$pGuid, 'wstr',$ProfileName, 'ptr',0) If Not @error Then Return Number($aCall[0] = 0) EndFunc ;==> _WlanDeleteProfile Func _WlanConnect($hClientHandle, $sGUid, $ProfileName) Local $pGuid = IsDllStruct($sGUid) ? DllStructGetPtr($sGUid) : $sGUid Local $sProfile = DllStructCreate('WCHAR['&StringLen($ProfileName)+1&']') DllStructSetData($sProfile, 1,$ProfileName) Local $sConnParams = DllStructCreate('LONG wlanConnectionMode;'& _ 'ptr strProfile;ptr 0;ptr 0;LONG BssType;DWORD dwFlags') DllStructSetData($sConnParams, 'strProfile', DllStructGetPtr($sProfile,1)) DllStructSetData($sConnParams, 'BssType', 3) Local $aCall = DllCall($h__WLANAPI, 'DWORD','WlanConnect', _ 'HANDLE',$hClientHandle, 'ptr',$pGuid, 'struct*',$sConnParams, 'ptr',0) If Not @error And $aCall[0] = 0 Then Return 1 Return SetError(1,0,0) EndFunc ;==> _WlanConnect Func _WlanDisconnect($hClientHandle, $sGUid) Local $pGuid = IsDllStruct($sGUid) ? DllStructGetPtr($sGUid) : $sGUid Local $aCall = DllCall($h__WLANAPI, 'DWORD','WlanDisconnect', _ 'HANDLE',$hClientHandle, 'ptr',$pGuid, 'ptr',0) If Not @error And $aCall[0] = 0 Then Return 1 Return SetError(1,0,0) EndFunc ;==> _WlanDisconnect #EndRegion Functions