title: SendARP returning wrong MAC address
body: <p>I am trying to get the MAC address of another device using the SendARP function and this code:</p>
  
  <pre><code>[DllImport("iphlpapi.dll", ExactSpelling = true)]
  public static extern int SendARP(System.Net.IPAddress DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);
  
  private void btnMAC_Click(object sender, EventArgs e)
  {
      string mac = "";
      IPHostEntry hostEntry = Dns.GetHostEntry(IPaddress);
  
      byte[] macAddr = new byte[6];
      uint macAddrLen = (uint)macAddr.Length;
  
      int dwRetVal = SendARP(hostEntry.AddressList[0], 0, macAddr, ref macAddrLen);
  
      if (dwRetVal == 0)
      {
          StringBuilder macAddressString = new StringBuilder();
          for (int i = 0; i &lt; macAddr.Length; i++)
          {
              if (macAddressString.Length &gt; 0)
                  macAddressString.Append(":");
  
              macAddressString.AppendFormat("{0:x2}", macAddr[i]);
          }
  
          mac = macAddressString.ToString();
      }
      else
      {
          lblMACaddress.Text = "Invalid";
      }
  }
  </code></pre>
  
  <p>The MAC address it is returning is completely wrong. I referenced this question, <a href="https://stackoverflow.com/questions/15277659/c-sendarp-returns-wrong-mac-address">C++ SendARP returns wrong mac address?</a>
  but the answer to that question really seemed to be about the return stack being cached when getting out of the function, but I'm doing everything within my function.</p>
  
  <p>I must be missing some basic concepts here that I'm not understanding from other references.</p>
  
  <p><strong>---EDIT---</strong>
  Changed my function to be like this due to possible wrong parameter type:</p>
  
  <pre><code>        string ip = txtIP1.Text + "." + txtIP2.Text + "." + txtIP3.Text + "." + txtIP4.Text;
          IPAddress dst = IPAddress.Parse(ip);
          int intAddress = BitConverter.ToInt32(dst.GetAddressBytes(), 0);
  
          byte[] macAddr = new byte[6];
          uint macAddrLen = (uint)macAddr.Length;
  
          if (SendARP(intAddress, 0, macAddr, ref macAddrLen) != 0)
          {
              lblMACaddress.Text = "SendARP failed.";
              return;
          }
  
          string[] str = new string[(int)macAddrLen];
  
          for (int i = 0; i &lt; macAddrLen; i++)
              str[i] = macAddr[i].ToString("x2");
  
          string mac = string.Join(":", str);
  </code></pre>
  
  <p>Now the issue is when I use my own computer's IP I get the correct MAC but when I use the IP of a different computer on my LAN I get the SendARP failed exception thrown. Any ideas why this is happening?</p>