none
Connection Manager API RRS feed

  • Question

  • Hello everybody,

    I'm trying to use connection manager API to connect to WiFi ESS but no way, i'm only able to set profiles. It exists some usefull documentation about that? Is it so terrible to use WiFi on an embedded OS?

    I am aware that WlanAPI or another API is useless with that. I take the example NetUI, but this is very painfull cause no documentation.

    Can you explain the how to about WiFi with connection manager please?

    EDIT : 

    More specially, according to NetUI :

    		for (result =CmGetFirstCandidateConnection(this->hCMSession, NULL, CMSO_DEFAULT, &hConnection);
    			result == CMRE_SUCCESS;
    			result = CmGetNextCandidateConnection(this->hCMSession, &hConnection))
    		{
    			dwError = this->GetConnectionDetailsByHandle(hConnection, &pDetails);
    
    			// Connect the WiFi connection in configured order.
    			if (memcmp(&pDetails->Type, &CM_CSP_WIFI_TYPE, sizeof(CM_CONNECTION_TYPE)) == 0)
    			{
    				if (wcscmp(pDetails->szName, this->pszConnection) == 0)
    				{
    					
    					result = CmAcquireConnection(hConnection);
    					
    					if (CMRE_SUCCESS == result)
    					{
    						 this->b_connecting = FALSE;
    					}
    					
    					goto exit;
    				}
    			}

    Before, I set and add profile : Success

    But in that code, in &pDetails->Type, I only find network interface, and not available wifi connection. Someone knows something about that?

    Thank you,


    • Edited by Romano0101 Thursday, January 28, 2016 4:32 PM
    Wednesday, January 27, 2016 2:40 PM

Answers

  • Hi Romano,

    please find below my subroutine to connect to Wifi network.

    Good luck !

    Franz

    #define WIFIFILE         L"\\flashdisk\\settings\\wifi.txt"  

    DWORD GetConnectionDetailsByHandle(CM_CONNECTION_HANDLE hConnection, CM_CONNECTION_DETAILS** ppDetails) { DWORD dwError = ERROR_SUCCESS; CM_RESULT result = CMRE_INSUFFICIENT_BUFFER; CM_CONNECTION_DETAILS* pDetails = NULL; DWORD dwDetailSize = 255; // Get the connection details. for (int i = 0; i < MAX_TRY_COUNT_FOR_INSUFFICIENT_BUFFER && result == CMRE_INSUFFICIENT_BUFFER; i++) { LocalFree(pDetails); pDetails = (CM_CONNECTION_DETAILS *)LocalAlloc(0, dwDetailSize); if (pDetails) result = CmGetConnectionDetailsByHandle(hConnection,pDetails,&dwDetailSize); } *ppDetails = pDetails; pDetails = NULL; LocalFree(pDetails); return dwError; } bool wifi_connect(TCHAR * ssid, TCHAR * authmode, TCHAR * encryption, TCHAR * key, bool adhoc) { TCHAR wifi[MAX_LINE]; wsprintf(wifi,L"%s %s %s %s %d",ssid,authmode,encryption,key,(adhoc ? 1:0)); FILE * fp = _wfopen(WIFIFILE,L"wb"); if (fp) { fwprintf(fp,L"%s",wifi); fclose(fp); } bool r_value = false; PWLAN_INTERFACE_INFO_LIST ppInterfaceList; PWLAN_INTERFACE_INFO pSelectedInterface; HANDLE phClientHandle; DWORD dwNegotiatedVersion; PWLAN_AVAILABLE_NETWORK_LIST ppAvailableNetworkList; DWORD dwReturn = WlanOpenHandle(WLAN_API_VERSION_1_0, NULL, &dwNegotiatedVersion, &phClientHandle); if (phClientHandle) { if (WlanEnumInterfaces(phClientHandle, NULL, &ppInterfaceList)==ERROR_SUCCESS) { pSelectedInterface = (WLAN_INTERFACE_INFO*) &ppInterfaceList->InterfaceInfo[0]; if (WlanGetAvailableNetworkList(phClientHandle, &pSelectedInterface->InterfaceGuid, 0, NULL, &ppAvailableNetworkList) == ERROR_SUCCESS) { } } } TCHAR xmlData[3000]; swprintf(xmlData, L"<?xml version=\"1.0\"?>\r\n\ <WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\r\n\ <name>%s</name>\r\n\ <SSIDConfig>\r\n\ <SSID>\r\n\ <name>%s</name>\r\n\ </SSID>\r\n\ <nonBroadcast>%s</nonBroadcast>\r\n\ </SSIDConfig>\r\n\ <connectionType>%s</connectionType>\r\n\ <connectionMode>manual</connectionMode>\r\n\ <MSM>\r\n\ <security>\r\n\ <authEncryption>\r\n\ <authentication>%s</authentication>\r\n\ <encryption>%s</encryption>\r\n\ <useOneX>false</useOneX>\r\n\ </authEncryption>\r\n\ <sharedKey>\r\n\ <keyType>passPhrase</keyType>\r\n\ <protected>false</protected>\r\n\ <keyMaterial>%s</keyMaterial>\r\n\ </sharedKey>\r\n\ </security>\r\n\ </MSM>\r\n\ </WLANProfile>",ssid,ssid,(adhoc ? L"false":L"true"),(adhoc ? L"IBSS":L"ESS"),authmode,encryption,key); int xmlSize = (wcslen(xmlData) + 1) * sizeof(WCHAR); //reconnect - use stored configuration CM_CONNECTION_DETAILS *pDetails = NULL; CM_SESSION_HANDLE myhandle = CmCreateSession(); CM_CONNECTION_HANDLE hConnection = NULL; CM_RESULT result; CM_CONNECTION_CONFIG* pConfig = NULL; DWORD cbConfigSize = 10; result = CmSetPriority(myhandle,CM_PRIORITY_BACKGROUND_HIGHEST); DWORD ulConfig = offsetof(CM_CONNECTION_CONFIG, TypeSpecificInfo[0]) + offsetof(CspWiFiSpecificInfo, DevConfigXml[0]) + xmlSize; pConfig = (CM_CONNECTION_CONFIG*)LocalAlloc(LPTR, ulConfig); memset(pConfig,0,ulConfig); pConfig->Version = CM_CURRENT_VERSION; pConfig->fEnabled = TRUE; pConfig->ConnectBehavior = CMCB_ALWAYS_CONNECTED; pConfig->Type = CM_CSP_WIFI_TYPE; pConfig->cbTypeSpecificInfo = offsetof(CspWiFiSpecificInfo, DevConfigXml[0]) + xmlSize; CspWiFiSpecificInfo * pCspWiFiInfo = (CspWiFiSpecificInfo *)&pConfig->TypeSpecificInfo[0]; pConfig->cbTypeSpecificInfo = offsetof(CspWiFiSpecificInfo, DevConfigXml[0]) + xmlSize; pCspWiFiInfo->Version = CSP_WIFI_SPECIFIC_INFO_VERSION; pCspWiFiInfo->InterfaceSpecific = TRUE; pCspWiFiInfo->InterfaceGuid = pSelectedInterface->InterfaceGuid; pCspWiFiInfo->cbUserDataOffset = xmlSize; pCspWiFiInfo->cbDevConfig = xmlSize; memcpy(&pCspWiFiInfo->DevConfigXml[0], xmlData, pCspWiFiInfo->cbDevConfig); if (pConfig) { CspWiFiSpecificInfo * mypWifiInfo = (CspWiFiSpecificInfo *)&pConfig->TypeSpecificInfo[0]; unsigned char * ptr = (unsigned char *)pConfig; FILE * fp = _wfopen(L"\\temp\\wifi.txt",L"wb"); int numwritten = fwrite(ptr,1,ulConfig,fp); fclose(fp); unsigned char * ptr2 = (unsigned char *)&mypWifiInfo->DevConfigXml[0]; fp = _wfopen(L"\\temp\\wifi_xml.txt",L"wb"); if (fp) { fwprintf(fp,L"%c",0xfeff); fwprintf(fp,L"ssid:%s auth:%s encryption:%s key:%s adhoc:%d\r\n",ssid,authmode,encryption,key,adhoc); int numwritten = fwrite(ptr2,1,mypWifiInfo->cbDevConfig,fp); fclose(fp); } CmDeleteConnectionConfig(ssid); result = CmAddConnectionConfig(ssid,pConfig,ulConfig); LocalFree(pConfig); } for (result = CmGetFirstCandidateConnection(myhandle, NULL, CMSO_DEFAULT, &hConnection);result==CMRE_SUCCESS; result = CmGetNextCandidateConnection(myhandle, &hConnection)) { DWORD dwError = GetConnectionDetailsByHandle(hConnection, &pDetails); if (dwError==0) { if (memcmp(&pDetails->Type, &CM_CSP_WIFI_TYPE, sizeof(CM_CONNECTION_TYPE)) == 0) { if (wcscmp(pDetails->szName,ssid) == 0) { int i = 0; while (pDetails->State==CMCS_DISCONNECTED_UNAVAILABLE && i<10) { Sleep(100); i++; GetConnectionDetailsByHandle(hConnection, &pDetails); } if (pDetails->State==CMCS_DISCONNECTED) { result = CmAcquireConnection(hConnection); } i = 0; } } } } CmCloseSession(myhandle); return(r_value); }




    Monday, February 1, 2016 7:54 AM

All replies

  • Hi Romano,

    I suspect you are fighting against Connection Manager. It wants to manage connections for you and that is why profiles work.  Take a look at https://msdn.microsoft.com/en-us/library/ee495573.aspx?f=255&MSPPError=-2147217396 as it may clarify...

    "...The IP address configuration does not change within the same Extended Service Set (ESS). When the wireless client crosses an ESS boundary into a new subnet, the DHCP renewal obtains a new IP address configuration for that subnet..."

    Sincerely,

    IoTGirl

    Saturday, January 30, 2016 1:22 AM
    Moderator
  • Hi Romano,

    please find below my subroutine to connect to Wifi network.

    Good luck !

    Franz

    #define WIFIFILE         L"\\flashdisk\\settings\\wifi.txt"  

    DWORD GetConnectionDetailsByHandle(CM_CONNECTION_HANDLE hConnection, CM_CONNECTION_DETAILS** ppDetails) { DWORD dwError = ERROR_SUCCESS; CM_RESULT result = CMRE_INSUFFICIENT_BUFFER; CM_CONNECTION_DETAILS* pDetails = NULL; DWORD dwDetailSize = 255; // Get the connection details. for (int i = 0; i < MAX_TRY_COUNT_FOR_INSUFFICIENT_BUFFER && result == CMRE_INSUFFICIENT_BUFFER; i++) { LocalFree(pDetails); pDetails = (CM_CONNECTION_DETAILS *)LocalAlloc(0, dwDetailSize); if (pDetails) result = CmGetConnectionDetailsByHandle(hConnection,pDetails,&dwDetailSize); } *ppDetails = pDetails; pDetails = NULL; LocalFree(pDetails); return dwError; } bool wifi_connect(TCHAR * ssid, TCHAR * authmode, TCHAR * encryption, TCHAR * key, bool adhoc) { TCHAR wifi[MAX_LINE]; wsprintf(wifi,L"%s %s %s %s %d",ssid,authmode,encryption,key,(adhoc ? 1:0)); FILE * fp = _wfopen(WIFIFILE,L"wb"); if (fp) { fwprintf(fp,L"%s",wifi); fclose(fp); } bool r_value = false; PWLAN_INTERFACE_INFO_LIST ppInterfaceList; PWLAN_INTERFACE_INFO pSelectedInterface; HANDLE phClientHandle; DWORD dwNegotiatedVersion; PWLAN_AVAILABLE_NETWORK_LIST ppAvailableNetworkList; DWORD dwReturn = WlanOpenHandle(WLAN_API_VERSION_1_0, NULL, &dwNegotiatedVersion, &phClientHandle); if (phClientHandle) { if (WlanEnumInterfaces(phClientHandle, NULL, &ppInterfaceList)==ERROR_SUCCESS) { pSelectedInterface = (WLAN_INTERFACE_INFO*) &ppInterfaceList->InterfaceInfo[0]; if (WlanGetAvailableNetworkList(phClientHandle, &pSelectedInterface->InterfaceGuid, 0, NULL, &ppAvailableNetworkList) == ERROR_SUCCESS) { } } } TCHAR xmlData[3000]; swprintf(xmlData, L"<?xml version=\"1.0\"?>\r\n\ <WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\r\n\ <name>%s</name>\r\n\ <SSIDConfig>\r\n\ <SSID>\r\n\ <name>%s</name>\r\n\ </SSID>\r\n\ <nonBroadcast>%s</nonBroadcast>\r\n\ </SSIDConfig>\r\n\ <connectionType>%s</connectionType>\r\n\ <connectionMode>manual</connectionMode>\r\n\ <MSM>\r\n\ <security>\r\n\ <authEncryption>\r\n\ <authentication>%s</authentication>\r\n\ <encryption>%s</encryption>\r\n\ <useOneX>false</useOneX>\r\n\ </authEncryption>\r\n\ <sharedKey>\r\n\ <keyType>passPhrase</keyType>\r\n\ <protected>false</protected>\r\n\ <keyMaterial>%s</keyMaterial>\r\n\ </sharedKey>\r\n\ </security>\r\n\ </MSM>\r\n\ </WLANProfile>",ssid,ssid,(adhoc ? L"false":L"true"),(adhoc ? L"IBSS":L"ESS"),authmode,encryption,key); int xmlSize = (wcslen(xmlData) + 1) * sizeof(WCHAR); //reconnect - use stored configuration CM_CONNECTION_DETAILS *pDetails = NULL; CM_SESSION_HANDLE myhandle = CmCreateSession(); CM_CONNECTION_HANDLE hConnection = NULL; CM_RESULT result; CM_CONNECTION_CONFIG* pConfig = NULL; DWORD cbConfigSize = 10; result = CmSetPriority(myhandle,CM_PRIORITY_BACKGROUND_HIGHEST); DWORD ulConfig = offsetof(CM_CONNECTION_CONFIG, TypeSpecificInfo[0]) + offsetof(CspWiFiSpecificInfo, DevConfigXml[0]) + xmlSize; pConfig = (CM_CONNECTION_CONFIG*)LocalAlloc(LPTR, ulConfig); memset(pConfig,0,ulConfig); pConfig->Version = CM_CURRENT_VERSION; pConfig->fEnabled = TRUE; pConfig->ConnectBehavior = CMCB_ALWAYS_CONNECTED; pConfig->Type = CM_CSP_WIFI_TYPE; pConfig->cbTypeSpecificInfo = offsetof(CspWiFiSpecificInfo, DevConfigXml[0]) + xmlSize; CspWiFiSpecificInfo * pCspWiFiInfo = (CspWiFiSpecificInfo *)&pConfig->TypeSpecificInfo[0]; pConfig->cbTypeSpecificInfo = offsetof(CspWiFiSpecificInfo, DevConfigXml[0]) + xmlSize; pCspWiFiInfo->Version = CSP_WIFI_SPECIFIC_INFO_VERSION; pCspWiFiInfo->InterfaceSpecific = TRUE; pCspWiFiInfo->InterfaceGuid = pSelectedInterface->InterfaceGuid; pCspWiFiInfo->cbUserDataOffset = xmlSize; pCspWiFiInfo->cbDevConfig = xmlSize; memcpy(&pCspWiFiInfo->DevConfigXml[0], xmlData, pCspWiFiInfo->cbDevConfig); if (pConfig) { CspWiFiSpecificInfo * mypWifiInfo = (CspWiFiSpecificInfo *)&pConfig->TypeSpecificInfo[0]; unsigned char * ptr = (unsigned char *)pConfig; FILE * fp = _wfopen(L"\\temp\\wifi.txt",L"wb"); int numwritten = fwrite(ptr,1,ulConfig,fp); fclose(fp); unsigned char * ptr2 = (unsigned char *)&mypWifiInfo->DevConfigXml[0]; fp = _wfopen(L"\\temp\\wifi_xml.txt",L"wb"); if (fp) { fwprintf(fp,L"%c",0xfeff); fwprintf(fp,L"ssid:%s auth:%s encryption:%s key:%s adhoc:%d\r\n",ssid,authmode,encryption,key,adhoc); int numwritten = fwrite(ptr2,1,mypWifiInfo->cbDevConfig,fp); fclose(fp); } CmDeleteConnectionConfig(ssid); result = CmAddConnectionConfig(ssid,pConfig,ulConfig); LocalFree(pConfig); } for (result = CmGetFirstCandidateConnection(myhandle, NULL, CMSO_DEFAULT, &hConnection);result==CMRE_SUCCESS; result = CmGetNextCandidateConnection(myhandle, &hConnection)) { DWORD dwError = GetConnectionDetailsByHandle(hConnection, &pDetails); if (dwError==0) { if (memcmp(&pDetails->Type, &CM_CSP_WIFI_TYPE, sizeof(CM_CONNECTION_TYPE)) == 0) { if (wcscmp(pDetails->szName,ssid) == 0) { int i = 0; while (pDetails->State==CMCS_DISCONNECTED_UNAVAILABLE && i<10) { Sleep(100); i++; GetConnectionDetailsByHandle(hConnection, &pDetails); } if (pDetails->State==CMCS_DISCONNECTED) { result = CmAcquireConnection(hConnection); } i = 0; } } } } CmCloseSession(myhandle); return(r_value); }




    Monday, February 1, 2016 7:54 AM
  • Hello IoTGirl,

    Thank you for your help. You made my day

    Thursday, February 4, 2016 1:33 PM
  • Hi Romano,

    You are very welcome and I am very happy to help!

    Sincerely,

    IoTGirl

    Thursday, February 4, 2016 8:12 PM
    Moderator