This project is read-only.

Every Time " The network connection file is corrupted" error

Mar 22, 2012 at 3:03 AM

Hey guys , i've just started experimentig with the managed wifi API , and it works delightfully well till i don't want to update the profile for connection.Because at that point i always get a Win32Exception , namely "The network connection file is corrupted" and i just can't figure it out how to make this work , please help me.
here is my code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NativeWifi;

namespace wifi
    class Program
                static string GetStringForSSID(Wlan.Dot11Ssid ssid)
            return Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);

        static void Main(string[] args)
            int NetIncrementator = 0;
            string ssid = "";
            string password = "xyz";
             WlanClient client = new WlanClient();
            foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
                // Lists all networks with WEP security
                Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
                //////////////////////////////////////////////////////////////////////////////////////////////////////SSID and selecting
                foreach (Wlan.WlanAvailableNetwork network in networks)
                    if (network.dot11DefaultCipherAlgorithm == Wlan.Dot11CipherAlgorithm.CCMP)
                        if ("Janus Computers" == GetStringForSSID(network.dot11Ssid))
                            Console.WriteLine("Found WPA network with SSID {0}.", GetStringForSSID(network.dot11Ssid));
                            ssid = GetStringForSSID(network.dot11Ssid);

                            if(GetStringForSSID(network.dot11Ssid) != ssid)
                            ssid = GetStringForSSID(network.dot11Ssid);
                /////////////////////////////////////////////////////////////////////////////////////////////////////////MAC Address
                Wlan.WlanBssEntry[] wlanBssEntries = wlanIface.GetNetworkBssList();
                Wlan.WlanBssEntry wlanBssEntry = wlanBssEntries[NetIncrementator];
                    byte[] macAddr = wlanBssEntry.dot11Bssid;
                    var macAddrLen = (uint)macAddr.Length;
                    var str = new string[(int)macAddrLen];
                    for (int i = 0; i < macAddrLen; i++)
                        str[i] = macAddr[i].ToString("x2");
                    string mac1 = string.Join("", str);
                    //////////////////////////////////////////////////////////////////////////////////////////////////////profile settings
                    Wlan.WlanProfileInfo[] profileInfos = wlanIface.GetProfiles();
                    Wlan.WlanProfileInfo profileInfo = profileInfos[NetIncrementator];
                    string name = profileInfo.profileName; // this is typically the network's SSID
                    string xml = wlanIface.GetProfileXml(profileInfo.profileName);
                    //////////////////////////////////////////////////////////////////////////////////////////////////////Trying to connect
                    string profileName = ssid ; // this is also the SSID
                    string key = password;
                    string profilexml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"\"><name>{0}</name><SSIDConfig><SSID><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><autoswitch>false</autoswitch><MSM><security><authEncryption><authentication>WPA2PSK</authentication><encryption>AES</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>PassPhrase</keyType><protected>false</protected><keyMaterial>{1}</keyMaterial></sharedKey></security></MSM></WLANProfile>", profileName, key);
                    wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profilexml, true);
                    wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, ssid);

Mar 23, 2012 at 10:45 PM

I spent all day looking at the code wondering why I was getting the same error... turns out I was sending incorrect data in the mac address the entire time... Looks like you are doing something along the lines of what I tried to do with the mac address... I figured out it was really the network name converted to mac after searching up the interop code. Let me know if this works for you!

                    WlanClient.WlanInterface wlanIface = ....
                    var networkName = "ZuechBNetwork";
                    var macAddress = ConvertToHex(networkName);

                    // Connects to a known network with WEP security
                    string profileName = networkName; // this is also the SSID
                    string mac = macAddress;
                    string key = password;
                    string profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"\"><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", profileName, mac, key);
                    wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
                    wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);

                    MessageBox.Show("Attempted Connection to: " + networkName);

and the method to add to it...

public string ConvertToHex(string asciiString) { string hex = ""; foreach (char c in asciiString) { int tmp = c; hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString())); } return hex; }