I have a piece of code implemented using the Managed Native WiFi. It is used to obtain the profile details of the connected network for displaying on screen.
// Start the wireless configuration service if it is stopped
startservice();
WlanClient client = new WlanClient();
bHasWiFi = false;
string strConnectionStatus = Constants.BlankString;
// Enumerate all interfaces
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
bHasWiFi = true;
strConnectionStatus = wlanIface.InterfaceState.ToString();
// Check whether disconnected
if (strConnectionStatus == Constants.Disconnected)
{
IsConnected = false;
continue; //iterate to next interface if any
}
else
{
string strProfileName = wlanIface.CurrentConnection.profileName.ToString();
ErrorLog.WriteLogMsg("Connected Profile : " + strProfileName);
strDefaultNetwork = wlanIface.CurrentConnection.wlanAssociationAttributes.dot11BssType.ToString();
ErrorLog.WriteLogMsg("Connected Network Type : " + strDefaultNetwork);
if (strProfileName.Length != 0)
{
// Obtain Profile XML
string strXmlProfile = wlanIface.GetProfileXml(strProfileName);
// Read channel information if OS not Windows XP
if(strCurOS != Constants.WindowsXP)
intChannel = wlanIface.Channel;
// Extract profile information
GetNetworkProfileXML(ref IsConnected, ref strDefaultAuth, ref strDefaultSSID, ref strDefaultEnc, ref strDefaultKey, wlanIface, ref strXmlProfile);
// Process and store the profile data
GetDatfromProfiles(strDefaultNetwork, strDefaultAuth, strDefaultSSID, strDefaultEnc, strDefaultKey, strCurOS, intChannel);
}
else
{
ErrorLog.WriteLogMsg("Blank profile name");
IsConnected = false; // Set error flag
}
break;
}
}
// Error cases
if (!IsConnected || !bHasWiFi)
{
if (!bHasWiFi)
throw new Exception("Unable to enumerate the wireless interfaces");
else if (!IsConnected)
throw new Exception("WiFi is not configured or is disconnected");
}
}
Whenever this code is executed in Vista/Windows 7 and a network is connected without the profiles being saved, the method GetProfileXMl throws an error
01:18:12 Method : ThrowIfError
01:18:12 Log Message : Element not found
01:18:12 Stack Trace : at NativeWifi.Wlan.ThrowIfError(Int32 win32ErrorCode) at NativeWifi.WlanClient.WlanInterface.GetProfileXml(String profileName)
And it has been observed that in Vista and Windows 7, profiles are not saved during connection for Infrastructure if user doesn't select "Connect Automatically". Also, adhoc profiles are never saved as this option is not provided to user during connection.
Does anyone know how to read the XML/details of a connected profile even when it is not saved? Thanks in advance.
If you register for notifications before the connection is initiated (see here) you should be able to examine the strProfileXml field of the WLAN_CONNECTION_NOTIFICATION_DATA structure when a wlan_notification_acm_connection_complete notification is received.