This is usually the error you get when the WMI class is not found. Did you follow this advice from the article: "In order to work with the next WMI classes your Wifi Network adapter must install a CIMWiFiProvider which implement these classes."
Stefan Glienke Yes, I tried but I did nothing :/ But I played a bit with the Rodrigo Ruz 's code, MSDN, etc. and finally I got what I wanted (I did test it only on Win 8.1). BTW. Is there a better way to publish code in G+?
The SSID of an active WiFi connection can be read with this code:
{$APPTYPE CONSOLE}
uses Windows, nduWlanAPI in 'nduWlanAPI.pas';
function GetCurrentSSID: string; var handle: THandle; version, infosize: DWORD; intflist: Pndu_WLAN_INTERFACE_INFO_LIST; intf: Tndu_WLAN_INTERFACE_INFO; info: Pndu_WLAN_CONNECTION_ATTRIBUTES; begin Result := ''; if (WlanOpenHandle(1, nil,@version, @handle)= ERROR_SUCCESS) then begin if (WlanEnumInterfaces(handle, nil,@intflist)= ERROR_SUCCESS) then begin for intf in intflist^.InterfaceInfo do begin if (intf.isState = wlan_interface_state_connected) and (WlanQueryInterface(handle,@intf.InterfaceGuid, wlan_intf_opcode_current_connection, nil,@infosize, Pointer(@info), nil) = ERROR_SUCCESS) and (info^.wlanAssociationAttributes.dot11Ssid.uSSIDLength > 0) then begin Result := PAnsiChar(@info^.wlanAssociationAttributes.dot11Ssid.ucSSID); WlanFreeMemory(info); Break; end; end; WlanFreeMemory(intflist); end; WlanCloseHandle(handle, nil); end; end;
begin Writeln('<' + GetCurrentSSID + '>'); Readln; end.
In case you want to be informed of WiFi connecting / disconnecting you could use this code:
procedure WlanNotification(AWLanNotificationData: Pndu_WLAN_NOTIFICATION_DATA; AP: Pointer); stdcall; var ssid, msg: string; begin if not (AWLanNotificationData^.NotificationCode in [Ord(wlan_notification_acm_connection_complete), Ord(wlan_notification_acm_disconnected)]) then Exit; if not (Pndu_WLAN_CONNECTION_NOTIFICATION_DATA(AWLanNotificationData^.pData)^.wlanReasonCode = NDU_WLAN_REASON_CODE_SUCCESS) then Exit;
ssid := ''; if Pndu_WLAN_CONNECTION_NOTIFICATION_DATA(AWLanNotificationData^.pData)^.dot11Ssid.uSSIDLength > 0 then ssid := PAnsiChar(@Pndu_WLAN_CONNECTION_NOTIFICATION_DATA(AWLanNotificationData^.pData)^.dot11Ssid.ucSSID);
if (AWLanNotificationData^.NotificationCode = lastCode) and (lastSSID = ssid) then Exit;
if Tndu_WLAN_NOTIFICATION_ACM(AWLanNotificationData^.NotificationCode) = wlan_notification_acm_connection_complete then msg := ssid + ' connected' else msg := ssid + ' disconnected'; Writeln(msg);
if WlanOpenHandle(1, nil,@dwSupportedVersion, @hWlan)= ERROR_SUCCESS then begin if WlanRegisterNotification(hWlan, NDU_WLAN_NOTIFICATION_SOURCE_ACM, True,@WlanNotification, nil, nil, nil) = ERROR_SUCCESS then begin Readln; WlanRegisterNotification(hWlan, NDU_WLAN_NOTIFICATION_SOURCE_NONE, True, nil, nil, nil, nil); end;
In Android ?
ReplyDeleteBarış Atalay
ReplyDeleteIn Windows
Mick Grove I saw that before ... but I missed the SSID info :/ Shame on me. Thanks :)
ReplyDeleteOne more thing. Which of these network is the active one?
ReplyDeleteDoes not work for me :/ I got exception with 'Invalid class' message. Any idea?
ReplyDeleteNo one?
ReplyDeleteThis is usually the error you get when the WMI class is not found. Did you follow this advice from the article:
ReplyDelete"In order to work with the next WMI classes your Wifi Network adapter must install a CIMWiFiProvider which implement these classes."
Stefan Glienke Yes, I tried but I did nothing :/ But I played a bit with the Rodrigo Ruz 's code, MSDN, etc. and finally I got what I wanted (I did test it only on Win 8.1). BTW. Is there a better way to publish code in G+?
ReplyDeleteThe SSID of an active WiFi connection can be read with this code:
{$APPTYPE CONSOLE}
uses
Windows,
nduWlanAPI in 'nduWlanAPI.pas';
function GetCurrentSSID: string;
var
handle: THandle;
version, infosize: DWORD;
intflist: Pndu_WLAN_INTERFACE_INFO_LIST;
intf: Tndu_WLAN_INTERFACE_INFO;
info: Pndu_WLAN_CONNECTION_ATTRIBUTES;
begin
Result := '';
if (WlanOpenHandle(1, nil,@version, @handle)= ERROR_SUCCESS) then begin
if (WlanEnumInterfaces(handle, nil,@intflist)= ERROR_SUCCESS) then begin
for intf in intflist^.InterfaceInfo do begin
if
(intf.isState = wlan_interface_state_connected) and
(WlanQueryInterface(handle,@intf.InterfaceGuid, wlan_intf_opcode_current_connection, nil,@infosize, Pointer(@info), nil) = ERROR_SUCCESS) and
(info^.wlanAssociationAttributes.dot11Ssid.uSSIDLength > 0)
then begin
Result := PAnsiChar(@info^.wlanAssociationAttributes.dot11Ssid.ucSSID);
WlanFreeMemory(info);
Break;
end;
end;
WlanFreeMemory(intflist);
end;
WlanCloseHandle(handle, nil);
end;
end;
begin
Writeln('<' + GetCurrentSSID + '>');
Readln;
end.
In case you want to be informed of WiFi connecting / disconnecting you could use this code:
{$APPTYPE CONSOLE}
uses
Windows,
nduWlanAPI in 'nduWlanAPI.pas';
var
hWlan: THandle;
dwSupportedVersion: DWORD = 0;
dwClientVersion: DWORD = 1;
lastSSID: string;
lastCode: Cardinal;
procedure WlanNotification(AWLanNotificationData: Pndu_WLAN_NOTIFICATION_DATA; AP: Pointer); stdcall;
var
ssid, msg: string;
begin
if not (AWLanNotificationData^.NotificationCode in [Ord(wlan_notification_acm_connection_complete), Ord(wlan_notification_acm_disconnected)]) then Exit;
if not (Pndu_WLAN_CONNECTION_NOTIFICATION_DATA(AWLanNotificationData^.pData)^.wlanReasonCode = NDU_WLAN_REASON_CODE_SUCCESS) then Exit;
ssid := '';
if Pndu_WLAN_CONNECTION_NOTIFICATION_DATA(AWLanNotificationData^.pData)^.dot11Ssid.uSSIDLength > 0 then
ssid := PAnsiChar(@Pndu_WLAN_CONNECTION_NOTIFICATION_DATA(AWLanNotificationData^.pData)^.dot11Ssid.ucSSID);
if (AWLanNotificationData^.NotificationCode = lastCode) and (lastSSID = ssid) then Exit;
if Tndu_WLAN_NOTIFICATION_ACM(AWLanNotificationData^.NotificationCode) = wlan_notification_acm_connection_complete then
msg := ssid + ' connected'
else
msg := ssid + ' disconnected';
Writeln(msg);
lastCode := AWLanNotificationData^.NotificationCode;
lastSSID := ssid;
end;
begin
hWlan := 0;
if WlanOpenHandle(1, nil,@dwSupportedVersion, @hWlan)= ERROR_SUCCESS then begin
if WlanRegisterNotification(hWlan, NDU_WLAN_NOTIFICATION_SOURCE_ACM, True,@WlanNotification, nil, nil, nil) = ERROR_SUCCESS then begin
Readln;
WlanRegisterNotification(hWlan, NDU_WLAN_NOTIFICATION_SOURCE_NONE, True, nil, nil, nil, nil);
end;
WlanCloseHandle(hWlan, nil);
end;
end.
Not on G+ itself but you can put it on pastebin and post the link here.
ReplyDelete