Corey Scheich
asked on
Converting code to VB.NET
Trying to convert the following code to VB.NET In my attempt to do this on my own I must have missed something. My code is below also.
Also reference my origional question that brought me to this code
https://www.experts-exchange.com/questions/22454308/MAC-ID-Physical-Address-of-server-from-the-client.html
Code to convert***************
[DllImport("iphlpapi.dll") ]
public static extern int SendARP( int DestIP, int SrcIP, [Out] byte[]
pMacAddr, ref int PhyAddrLen );
IPAddress addr = IPAddress.Parse("192.168.0 .1");
byte[] mac = new byte[6];
int len = mac.Length;
SendARP( (int)addr.Address, 0, mac, ref len );
string macAddress = BitConverter.ToString( mac, 0, len );
My conversion attempt******************* **********
Public Class MACRetriever
Declare Function SendARP Lib "iphlpapi.dll" Alias "SendARP" (ByVal DestIP As Int32, ByVal SrcIP As Int32, ByRef pMacAddr() As Byte, ByRef PhyAddrLen As Int32) As Int32
Public Shared Function GetMac() As String
Dim addr As Net.IPAddress = Net.IPAddress.Parse("10.3. 1.245")
Dim addrMe As Net.IPAddress = Net.IPAddress.Parse("10.3. 1.247")
Dim mac(6) As Byte
Dim Mac2(6) As Byte
Dim b As Byte
For Each b In mac
b = 1
Next
Dim len As Int32 = mac.Length
Try
SendARP(addr.Address, addrMe.Address, mac, len)
Dim macAddress As String = BitConverter.ToString(Mac2 , 0, len)
Return macAddress
Catch ex As Exception
End Try
Return "No Luck"
End Function
End Class
Also reference my origional question that brought me to this code
https://www.experts-exchange.com/questions/22454308/MAC-ID-Physical-Address-of-server-from-the-client.html
Code to convert***************
[DllImport("iphlpapi.dll")
public static extern int SendARP( int DestIP, int SrcIP, [Out] byte[]
pMacAddr, ref int PhyAddrLen );
IPAddress addr = IPAddress.Parse("192.168.0
byte[] mac = new byte[6];
int len = mac.Length;
SendARP( (int)addr.Address, 0, mac, ref len );
string macAddress = BitConverter.ToString( mac, 0, len );
My conversion attempt*******************
Public Class MACRetriever
Declare Function SendARP Lib "iphlpapi.dll" Alias "SendARP" (ByVal DestIP As Int32, ByVal SrcIP As Int32, ByRef pMacAddr() As Byte, ByRef PhyAddrLen As Int32) As Int32
Public Shared Function GetMac() As String
Dim addr As Net.IPAddress = Net.IPAddress.Parse("10.3.
Dim addrMe As Net.IPAddress = Net.IPAddress.Parse("10.3.
Dim mac(6) As Byte
Dim Mac2(6) As Byte
Dim b As Byte
For Each b In mac
b = 1
Next
Dim len As Int32 = mac.Length
Try
SendARP(addr.Address, addrMe.Address, mac, len)
Dim macAddress As String = BitConverter.ToString(Mac2
Return macAddress
Catch ex As Exception
End Try
Return "No Luck"
End Function
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
1)
For Each b In mac
b = 1
Next
This doesn't really do anything to the source array (the byte is passed by value). What you want is:
For i = 0 to mac.Length
mac(i) = 1
Next
2)
You're never setting Mac2 to anything. Was your intent to copy mac into Mac2?