title: WlanSetProfile from wlanapi.dll in Windows XP fails
body: <p>I am creating a program which will be able to connect automatically to a wireless network. For doing that, I am using a library called ManagedWifi, which uses the library "wlanapi.dll". When I try to use the code on Windows Vista, everything is fine, but when I use it on Windows XP it fails. I have checked that it has Service Pack 3 installed, and "wlanapi.dll" is located in Windows/system32. The problem is when ManagedWifi calls this method:</p>
  
  <pre><code>[DllImport("wlanapi.dll")]
          public static extern int WlanSetProfile(
              [In] IntPtr clientHandle,
              [In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
              [In] WlanProfileFlags flags,
              [In, MarshalAs(UnmanagedType.LPWStr)] string profileXml,
              [In, Optional, MarshalAs(UnmanagedType.LPWStr)] string allUserProfileSecurity,
              [In] bool overwrite,
              [In] IntPtr pReserved,
              [Out] out WlanReasonCode reasonCode);
  </code></pre>
  
  <p>You can view a description for this method <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms706795%28v=vs.85%29.aspx" rel="nofollow">here</a>. The error I am receiving is the following: 1206 (ERROR_BAD_PROFILE). As you can see <a href="http://support.microsoft.com/kb/958071/en-us?fr=1" rel="nofollow">in this page</a> it is a known error, and the only thing I have to do is to download and install the hotfix. Well, I have done that and the error is still there. The profile is correct, because I get it from a call to <a href="http://msdn.microsoft.com/en-us/library/ms706738%28v=VS.85%29.aspx" rel="nofollow">WlanGetProfile</a></p>
  
  <p>I just want to connect to a network using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms706613%28v=vs.85%29.aspx" rel="nofollow">WlanConnect</a>, but as you can see in the function's page: <strong><em>Windows XP with SP3 and Wireless LAN API for Windows XP with SP2</strong>:  You can only use WlanConnect to connect to networks on the preferred network list. To add a network to the preferred network list, call WlanSetProfile.</em></p>
  
  <p>I am really desperate, I've been fighting to this error some days, and I need to go through it...</p>
  
  <p>And, if I try to call WlanConnect method without calling SetProfile, the result is always the same: it doesn't connect.</p>
  
  <p>I will appreciate any idea you can provide with.</p>