title: How to use “Native Wifi API” Windows API functions with ruby
body: <p>My Setup: Windows 7, Ruby 1.9.3</p>
  <p>I want to manage the wireless NIC to access different Wireless Routers, here's what I have tried:</p>
  <h2>Method 1: <code>netsh  wlan</code></h2>
  <p>Managing the WLAN adapter through the <code>netsh</code> command tools.</p>
  <p>So I setup my Wireless Network in Windows and exported it using</p>
  <pre><code>netsh wlan export profile name=&quot;wuhongliang&quot; folder = &quot;d:\&quot; interface=&quot;Wireless&quot; key=clear
  </code></pre>
  <p>Which worked, I can then add the profile and connect by doing:</p>
  <pre><code>&gt; netsh wlan add profile ^
  filename=&quot;d:\wireless-wuhongliang.xml&quot; ^
  interface=&quot;wireless&quot; Profile
  
  wuhongliang is added on interface Wireless.
  
  &gt; netsh wlan connect name=&quot;wuhongliang&quot; ^
  ssid=&quot;wuhongliang&quot;
  Connection request was completed successfully.
  </code></pre>
  <p>Which works and connects me to the WLAN using the exported profile.</p>
  <p>So looking at the XML Profile:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; ?&gt;
  &lt;WLANProfile xmlns=&quot;http://www.microsoft.com/networking/WLAN/profile/v1&quot;&gt;
    &lt;name&gt;wuhongliang&lt;/name&gt;
    &lt;SSIDConfig&gt;
      &lt;SSID&gt;
        &lt;hex&gt;7775686F6E676C69616E67&lt;/hex&gt;
        &lt;name&gt;wuhongliang&lt;/name&gt;
      &lt;/SSID&gt;
    &lt;/SSIDConfig&gt;
    &lt;connectionType&gt;ESS&lt;/connectionType&gt;
    &lt;connectionMode&gt;auto&lt;/connectionMode&gt;
    &lt;MSM&gt;
      &lt;security&gt;
        &lt;authEncryption&gt;
          &lt;authentication&gt;WPA2PSK&lt;/authentication&gt;
          &lt;encryption&gt;AES&lt;/encryption&gt;
          &lt;useOneX&gt;false&lt;/useOneX&gt;
        &lt;/authEncryption&gt;
        &lt;sharedKey&gt;
          &lt;keyType&gt;passPhrase&lt;/keyType&gt;
          &lt;protected&gt;true&lt;/protected&gt;
          &lt;keyMaterial&gt;[Long Encrypted Key]&lt;/keyMaterial&gt;
        &lt;/sharedKey&gt;
      &lt;/security&gt;
    &lt;/MSM&gt;
  &lt;/WLANProfile&gt;
  </code></pre>
  <p>The <code>keyMaterial</code> is obviously the encrypted network password. So if I change the router password then the profile breaks because I don't have the new encrypted <code>keyMaterial</code>.</p>
  <p>Since I don't know how to generate the <code>keyMaterial</code> this won't work for me.</p>
  <h2>Method 2: using <code>wlanapi.dll</code></h2>
  <p>I found <a href="http://www.codeproject.com/Articles/72105/Manage-WiFi-with-Native-API-WIFI-on-Windows-XP-SP2" rel="nofollow noreferrer">this article on codeproject.com</a> and it looked promising. But I have no experience in C#,C,C++.</p>
  <p>I tried using the <code>dll</code> with <code>Ruby DL</code> and <code>Win32API</code> but I don't how to set the parameters or use those libraries in general.</p>
  <p>This is the Ruby code I used call the <code>WlanEnumInterfaces</code> API (but I'm pretty sure it's incorrect):</p>
  <pre><code>c_handle = 1
  c_reserved = 0
  c_interfacelist=&quot; &quot;*10000 
  dllname = &quot;wlanapi.dll&quot; 
  pro=Win32API.new(dllname, &quot;WlanEnumInterfaces&quot;, ['p', 'l', 'p'], &quot;v&quot;) 
  p pro.call(handle, c_reserved, c_interfacelist) 
  p c_interfacelist.unpack(&quot;l*&quot;)
  </code></pre>