-
ssid
I've googled this until my fingers bled and for the life of me I can't seem to find anything. What I basically need to know is how to get the SSID name of the current wireless connection if it exists.
I was able to do this for Windows Mobile platform using DeviceIoControl, but for the life of me I can't seem to find a definitive answer for the win32 api. I just need to be pointed towards the right direction.
My application needs to know it is connecting to the right SSID name for security reasons and I need it to work for windows mobile 5.0 and windows xp.
So far I've found either WMI which requires me to use COM or Windows Native Wifi API, which I need to install a library (which may not be feasible for our deployed windows xp devices). Any help is appreciated =)
-
Re: ssid
>> So far I've found ...
Those are your choices. Sample WMI code for VC++ can be found here: http://www.codeguru.com/forum/showthread.php?t=468109
gg
-
Re: ssid
Thanks for the reply. I bit the bullet and upgraded to the new windows sdk. I will just have to tell the upper ups that this will require some, uh, more time and work that I originally estimated. =)
Here's the code snippet for others who might be interested for getting the SSID for Windows XP SP2 or SP3.
Code:
DWORD serviceVersion = 0;
HANDLE client = 0;
DWORD result = 0;
result = WlanOpenHandle(WLAN_API_VERSION, NULL, &serviceVersion, &client);
if(result != ERROR_SUCCESS) {
// exit
}
// Sanity check.
if(client == 0) {
// exit
}
PWLAN_INTERFACE_INFO_LIST wlanInterfaceInfoList = 0;
result = WlanEnumInterfaces(client, 0, &wlanInterfaceInfoList);
if(result != ERROR_SUCCESS) {
// Clean up.
if(client != 0) {
WlanCloseHandle(client, 0);
client = 0;
}
// This might not be needed.
if(wlanInterfaceInfoList != 0) {
WlanFreeMemory(wlanInterfaceInfoList);
wlanInterfaceInfoList = 0;
}
// exit
}
PVOID data = 0;
DWORD size = 0;
for(unsigned int i = 0; i < wlanInterfaceInfoList->dwNumberOfItems; ++i) {
result = WlanQueryInterface(
client,
&wlanInterfaceInfoList->InterfaceInfo[i].InterfaceGuid,
wlan_intf_opcode_current_connection,
0,
&size,
&data,
0
);
if(result != ERROR_SUCCESS) {
break;
}
PWLAN_CONNECTION_ATTRIBUTES wlanConnectionAttributes = 0;
wlanConnectionAttributes = (PWLAN_CONNECTION_ATTRIBUTES)data;
if(wlanConnectionAttributes->isState != wlan_interface_state_connected) {
continue;
}
char ssidName[33];
memset(ssidName, 0, 33);
memcpy(
ssidName,
wlanConnectionAttributes->wlanAssociationAttributes.dot11Ssid.ucSSID,
wlanConnectionAttributes->wlanAssociationAttributes.dot11Ssid.uSSIDLength);
// ssidName should now contain the SSID name of the first
// detected interface which is connected. You can store this
// in a vector or whatever you wish.
WlanFreeMemory(data);
}
if(wlanInterfaceInfoList != 0) {
WlanFreeMemory(wlanInterfaceInfoList);
}
if(client != 0) {
WlanCloseHandle(client, 0);
}
wlanInterfaceInfoList = 0;
client = 0;
-
Re: ssid
This console application i have written might help you its fully working source code,
Code:
//wifi prog
//Copyright Madden Software ©
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#define DOT11_SSID_MAX_LENGTH 32
#define WLAN_MAX_NAME_LENGTH 256
#define WLAN_MAX_PHY_TYPE_NUMBER 8
#define WLAN_AVAILABLE_NETWORK_CONNECTED 0x00000001
#define WLAN_READ_ACCESS ( STANDARD_RIGHTS_READ | FILE_READ_DATA )
#define DOT11_RATE_SET_MAX_LENGTH 126
using namespace std ;
std::string ws2s(const std::wstring& s)
{
int len;
int slength = (int)s.length() + 1;
len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0);
char* buf = new char[len];
WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, buf, len, 0, 0);
std::string r(buf);
delete[] buf;
return r;
}
typedef ULONG WLAN_SIGNAL_QUALITY;
typedef UCHAR DOT11_MAC_ADDRESS[6];
typedef DOT11_MAC_ADDRESS * PDOT11_MAC_ADDRESS;
typedef enum _WLAN_INTERFACE_STATE
{
wlan_interface_state_connected = 1, wlan_interface_state_disconnected = 4, wlan_interface_state_authenticating = 7
}
WLAN_INTERFACE_STATE, * PWLAN_INTERFACE_STATE;
typedef struct _WLAN_INTERFACE_INFO
{
GUID InterfaceGuid;
WCHAR strInterfaceDescription[256];
WLAN_INTERFACE_STATE isState;
}
WLAN_INTERFACE_INFO, * PWLAN_INTERFACE_INFO;
typedef struct _WLAN_INTERFACE_INFO_LIST
{
DWORD dwNumberOfItems;
DWORD dwIndex;
WLAN_INTERFACE_INFO InterfaceInfo[];
}
WLAN_INTERFACE_INFO_LIST, * PWLAN_INTERFACE_INFO_LIST;
typedef struct
{
LPWSTR wszGuid;
}
INTF_KEY_ENTRY, * PINTF_KEY_ENTRY;
typedef struct
{
DWORD dwNumIntfs;
PINTF_KEY_ENTRY pIntfs;
}
INTFS_KEY_TABLE, * PINTFS_KEY_TABLE;
typedef struct _WLAN_NOTIFICATION_DATA
{
DWORD NotificationSource;
DWORD NotificationCode;
GUID InterfaceGuid;
DWORD dwDataSize;
PVOID pData;
}
WLAN_NOTIFICATION_DATA, * PWLAN_NOTIFICATION_DATA;
typedef struct _WLAN_RATE_SET
{
ULONG uRateSetLength;
USHORT usRateSet[DOT11_RATE_SET_MAX_LENGTH];
}
WLAN_RATE_SET, * PWLAN_RATE_SET;
typedef struct _DOT11_SSID
{
ULONG uSSIDLength;
UCHAR ucSSID[DOT11_SSID_MAX_LENGTH];
}
DOT11_SSID, * PDOT11_SSID;
typedef enum _DOT11_BSS_TYPE
{
dot11_BSS_type_infrastructure, dot11_BSS_type_independent, dot11_BSS_type_any
}
DOT11_BSS_TYPE, * PDOT11_BSS_TYPE;
typedef enum _DOT11_PHY_TYPE
{
dot11_phy_type_unknown, dot11_phy_type_any, dot11_phy_type_fhss, dot11_phy_type_dsss, dot11_phy_type_irbaseband,
dot11_phy_type_ofdm, dot11_phy_type_hrdsss, dot11_phy_type_erp, dot11_phy_type_ht,
dot11_phy_type_IHV_start, dot11_phy_type_IHV_end
}
DOT11_PHY_TYPE, * PDOT11_PHY_TYPE;
typedef enum _DOT11_AUTH_ALGORITHM
{
DOT11_AUTH_ALGO_80211_OPEN = 1, DOT11_AUTH_ALGO_80211_SHARED_KEY = 2, DOT11_AUTH_ALGO_WPA = 3, DOT11_AUTH_ALGO_WPA_PSK = 4,
DOT11_AUTH_ALGO_WPA_NONE = 5, DOT11_AUTH_ALGO_RSNA = 6, DOT11_AUTH_ALGO_RSNA_PSK = 7, DOT11_AUTH_ALGO_IHV_START =
0x80000000, DOT11_AUTH_ALGO_IHV_END = 0xffffffff
}
DOT11_AUTH_ALGORITHM, * PDOT11_AUTH_ALGORITHM;
typedef enum _DOT11_CIPHER_ALGORITHM
{
DOT11_CIPHER_ALGO_NONE = 0x00, DOT11_CIPHER_ALGO_WEP40 = 0x01, DOT11_CIPHER_ALGO_TKIP = 0x02, DOT11_CIPHER_ALGO_CCMP = 0x04,
DOT11_CIPHER_ALGO_WEP104 = 0x05, DOT11_CIPHER_ALGO_WPA_USE_GROUP = 0x100, DOT11_CIPHER_ALGO_RSN_USE_GROUP = 0x100,
DOT11_CIPHER_ALGO_WEP = 0x101, DOT11_CIPHER_ALGO_IHV_START = 0x80000000, DOT11_CIPHER_ALGO_IHV_END = 0xffffffff
}
DOT11_CIPHER_ALGORITHM, * PDOT11_CIPHER_ALGORITHM;
typedef DWORD WLAN_REASON_CODE, * PWLAN_REASON_CODE;
typedef struct _WLAN_BSS_ENTRY
{
DOT11_SSID dot11Ssid;
ULONG uPhyId;
DOT11_MAC_ADDRESS dot11Bssid;
DOT11_BSS_TYPE dot11BssType;
DOT11_PHY_TYPE dot11BssPhyType;
LONG lRssi;
ULONG uLinkQuality;
BOOLEAN bInRegDomain;
USHORT usBeaconPeriod;
ULONGLONG ullTimestamp;
ULONGLONG ullHostTimestamp;
USHORT usCapabilityInformation;
ULONG ulChCenterFrequency;
WLAN_RATE_SET wlanRateSet;
ULONG ulIeOffset;
ULONG ulIeSize;
}
WLAN_BSS_ENTRY, * PWLAN_BSS_ENTRY;
typedef struct _WLAN_BSS_LIST
{
DWORD dwTotalSize;
DWORD dwNumberOfItems;
WLAN_BSS_ENTRY wlanBssEntries[1];
}
WLAN_BSS_LIST, * PWLAN_BSS_LIST;
typedef struct _WLAN_AVAILABLE_NETWORK
{
WCHAR strProfileName[WLAN_MAX_NAME_LENGTH];
DOT11_SSID dot11Ssid;
DOT11_BSS_TYPE dot11BssType;
ULONG uNumberOfBssids;
BOOL bNetworkConnectable;
WLAN_REASON_CODE wlanNotConnectableReason;
ULONG uNumberOfPhyTypes;
DOT11_PHY_TYPE dot11PhyTypes[WLAN_MAX_PHY_TYPE_NUMBER];
// bMorePhyTypes is set to TRUE if the PHY types for the network
// exceeds WLAN_MAX_PHY_TYPE_NUMBER.
// In this case, uNumerOfPhyTypes is WLAN_MAX_PHY_TYPE_NUMBER and the
// first WLAN_MAX_PHY_TYPE_NUMBER PHY types are returned.
BOOL bMorePhyTypes;
WLAN_SIGNAL_QUALITY wlanSignalQuality;
BOOL bSecurityEnabled;
DOT11_AUTH_ALGORITHM dot11DefaultAuthAlgorithm;
DOT11_CIPHER_ALGORITHM dot11DefaultCipherAlgorithm;
DWORD dwFlags;
DWORD dwReserved;
}
WLAN_AVAILABLE_NETWORK, * PWLAN_AVAILABLE_NETWORK;
typedef struct _WLAN_RAW_DATA
{
DWORD dwDataSize;
BYTE DataBlob[1];
}
WLAN_RAW_DATA, * PWLAN_RAW_DATA;
typedef struct _WLAN_AVAILABLE_NETWORK_LIST
{
DWORD dwNumberOfItems;
DWORD dwIndex;
WLAN_AVAILABLE_NETWORK Network[1];
}
WLAN_AVAILABLE_NETWORK_LIST, * PWLAN_AVAILABLE_NETWORK_LIST;
typedef enum _WLAN_OPCODE_VALUE_TYPE
{
wlan_opcode_value_type_query_only = 0, wlan_opcode_value_type_set_by_group_policy = 1, wlan_opcode_value_type_set_by_user =
2, wlan_opcode_value_type_invalid = 3
}
WLAN_OPCODE_VALUE_TYPE, * PWLAN_OPCODE_VALUE_TYPE;
typedef enum _WLAN_INTF_OPCODE
{
wlan_intf_opcode_autoconf_start = 0x000000000, wlan_intf_opcode_autoconf_enabled, wlan_intf_opcode_background_scan_enabled,
wlan_intf_opcode_media_streaming_mode, wlan_intf_opcode_radio_state, wlan_intf_opcode_bss_type,
wlan_intf_opcode_interface_state, wlan_intf_opcode_current_connection, wlan_intf_opcode_channel_number,
wlan_intf_opcode_supported_infrastructure_auth_cipher_pairs, wlan_intf_opcode_supported_adhoc_auth_cipher_pairs,
wlan_intf_opcode_supported_country_or_region_string_list, wlan_intf_opcode_current_operation_mode,
wlan_intf_opcode_supported_safe_mode, wlan_intf_opcode_certified_safe_mode, wlan_intf_opcode_autoconf_end = 0x0fffffff,
wlan_intf_opcode_msm_start = 0x10000100, wlan_intf_opcode_statistics, wlan_intf_opcode_rssi, wlan_intf_opcode_msm_end =
0x1fffffff, wlan_intf_opcode_security_start = 0x20010000, wlan_intf_opcode_security_end = 0x2fffffff,
wlan_intf_opcode_ihv_start = 0x30000000, wlan_intf_opcode_ihv_end = 0x3fffffff
}
WLAN_INTF_OPCODE, * PWLAN_INTF_OPCODE;
DWORD( WINAPI * WlanOpenHandle ) ( DWORD, PVOID, PDWORD, PHANDLE );
DWORD( WINAPI * WlanEnumInterfaces ) ( HANDLE, PVOID, PWLAN_INTERFACE_INFO_LIST * );
VOID( WINAPI * WlanFreeMemory ) ( PVOID );
DWORD( WINAPI * WlanGetAvailableNetworkList ) ( HANDLE, const GUID *, DWORD, PVOID, PWLAN_AVAILABLE_NETWORK_LIST * );
DWORD( WINAPI * WlanScan ) ( HANDLE, const GUID *, const PDOT11_SSID, const PWLAN_RAW_DATA, PVOID );
DWORD( WINAPI * WlanCloseHandle ) ( HANDLE, PVOID );
DWORD( WINAPI * WlanGetProfile ) ( HANDLE, const GUID *, LPCWSTR, PVOID, LPWSTR *, DWORD *, PDWORD );
DWORD( WINAPI * WlanGetNetworkBssList ) ( HANDLE, const GUID *, const PDOT11_SSID, DOT11_BSS_TYPE, BOOL,
PVOID, PWLAN_BSS_LIST * );
DWORD( WINAPI * WlanQueryInterface ) ( HANDLE, const GUID *, WLAN_INTF_OPCODE, PVOID, PDWORD, PVOID *,
PWLAN_OPCODE_VALUE_TYPE );
DWORD( WINAPI * WlanReasonCodeToString ) ( DWORD, DWORD, PWCHAR, PVOID );
void Add_profile( char * szFile, char * chbuf )
{
char Buff[256];
GetCurrentDirectory( 256, Buff );
strcat( Buff, "\\wifiprofiles" );
strcat( szFile, ".txt" );
if ( strlen( Buff ) <= 256 - 20 ) // no buffer overrun
{
CreateDirectory( Buff, 0 );
SetCurrentDirectory( Buff );
HANDLE hFile = CreateFile( szFile, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
if ( hFile != INVALID_HANDLE_VALUE )
{
DWORD bytes = 0;
WriteFile( hFile, chbuf, strlen( chbuf ), & bytes, 0 );
CloseHandle( hFile );
}
SetCurrentDirectory( ".." );
}
}
BOOL func()
{
BOOL bResult = 0;
HMODULE hWinlib = LoadLibrary( "Wlanapi.dll" );
if ( hWinlib )
{
DWORD dwVers = 0, dummy;
puts( "Wifi module loaded successfuly" );
WlanOpenHandle = ( DWORD( WINAPI * ) ( DWORD, PVOID, PDWORD, PHANDLE ) ) GetProcAddress( hWinlib, "WlanOpenHandle" );
WlanEnumInterfaces = ( DWORD( WINAPI * ) ( HANDLE, PVOID, PWLAN_INTERFACE_INFO_LIST * ) )
GetProcAddress( hWinlib, "WlanEnumInterfaces" );
WlanFreeMemory = ( VOID( WINAPI * ) ( PVOID ) ) GetProcAddress( hWinlib, "WlanFreeMemory" );
WlanGetAvailableNetworkList =
( DWORD( WINAPI * ) ( HANDLE, const GUID *, DWORD, PVOID, PWLAN_AVAILABLE_NETWORK_LIST * ) )
GetProcAddress( hWinlib, "WlanGetAvailableNetworkList" );
WlanScan = ( DWORD( WINAPI * ) ( HANDLE, const GUID *, const PDOT11_SSID, const PWLAN_RAW_DATA, PVOID ) )
GetProcAddress( hWinlib, "WlanScan" );
WlanCloseHandle = ( DWORD( WINAPI * ) ( HANDLE, PVOID ) ) GetProcAddress( hWinlib, "WlanCloseHandle" );
WlanGetProfile = ( DWORD( WINAPI * ) ( HANDLE, const GUID *, LPCWSTR, PVOID, LPWSTR *, DWORD *, PDWORD ) )
GetProcAddress( hWinlib, "WlanGetProfile" );
WlanGetNetworkBssList =
( DWORD( WINAPI * ) ( HANDLE, const GUID *, const PDOT11_SSID, DOT11_BSS_TYPE, BOOL, PVOID, PWLAN_BSS_LIST * ) )
GetProcAddress( hWinlib, "WlanGetNetworkBssList" );
WlanQueryInterface = ( DWORD( WINAPI * ) ( HANDLE, const GUID *, WLAN_INTF_OPCODE, PVOID, PDWORD, PVOID *,
PWLAN_OPCODE_VALUE_TYPE ) ) GetProcAddress( hWinlib, "WlanQueryInterface" );
WlanReasonCodeToString =
( DWORD( WINAPI * ) ( DWORD, DWORD, PWCHAR, PVOID ) ) GetProcAddress( hWinlib, "WlanReasonCodeToString" );
//Note unused functions are here because they were removed due to being untested
if ( WlanQueryInterface == 0 || WlanReasonCodeToString == 0 || WlanGetNetworkBssList == 0 || WlanGetProfile == 0
|| WlanCloseHandle == 0 || WlanScan == 0 || WlanGetAvailableNetworkList == 0 || WlanFreeMemory == 0
|| WlanOpenHandle == 0 || WlanEnumInterfaces == 0 )
{
puts( "One of the functions did not load correctly" );
FreeLibrary( hWinlib );
return 0;
}
HANDLE hHandle = INVALID_HANDLE_VALUE;
PWLAN_INTERFACE_INFO_LIST list =
{
0
};
PWLAN_AVAILABLE_NETWORK_LIST w =
{
0
};
char szName[WLAN_MAX_NAME_LENGTH + 5];
if ( WlanOpenHandle( 1, 0, & dwVers, & hHandle ) == ERROR_SUCCESS )
{
printf( "Opened connection to server\r\nUsing native wifi API vrs(%ld)", dwVers );
if ( WlanEnumInterfaces( hHandle, 0, & list ) == ERROR_SUCCESS )
{
printf( "\r\n%ld Available Access Point%s\r\n", list->dwNumberOfItems, list->dwNumberOfItems > 1 ? "s" : "" );
if ( WlanScan( hHandle, & list->InterfaceInfo[0].InterfaceGuid, 0, 0, 0 ) == ERROR_SUCCESS )
{
if ( WlanGetAvailableNetworkList( hHandle, & list->InterfaceInfo[0].InterfaceGuid, 0x00000002 | 0x00000001, 0,
& w ) == ERROR_SUCCESS )
{
for ( dummy = 0; dummy <= list->dwNumberOfItems-1; dummy++ )
{
sprintf( szName, " -Network Name %ls ", w->Network[dummy].strProfileName );
printf( "%s\r\n - Signal Strenth/Quality %ld%%\r\n", szName, w->Network[dummy].wlanSignalQuality );
if ( w->Network[dummy].bSecurityEnabled )
puts( " - Security Enabled Connection" );
else
puts( " - Security Disabled Connection" );
if ( list->InterfaceInfo[dummy].isState == wlan_interface_state_connected )
puts( " - Connected" );
else if ( list->InterfaceInfo[dummy].isState == wlan_interface_state_disconnected )
puts( " - Not Connected" );
else if ( list->InterfaceInfo[dummy].isState == wlan_interface_state_authenticating )
puts( " - Authenticating" );
else
{
puts( " - Unimplemented network state" );
puts( " - Current version higher then WinXP SP2/3(?) continue" );
printf( " state (%ld) ", list->InterfaceInfo[dummy].isState );
}
}
bResult = 1;
}
else
FreeLibrary( hWinlib );
}
else
puts( "Unable to load module" );
return bResult;
}
}
}
}
int main()
{
if ( !func() )
puts( "\r\nUnable to Scan for Wireless Access \r\nPress Any Key To Continue" );
else
printf("Successful Scan Implemented !");
return 0;
}
hope this helps