?
Solved

Converting code to VB.NET

Posted on 2007-03-23
4
Medium Priority
?
2,144 Views
Last Modified: 2013-12-17
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
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22454308.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

0
Comment
Question by:Corey2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 18779545
Two things I see:
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?
0
 
LVL 13

Accepted Solution

by:
Corey2 earned 0 total points
ID: 18780416
I figured out what went wrong I was passing the byte array as byref and it needs to be byval.  The following works.

    Declare Function SendARP Lib "iphlpapi.dll" Alias "SendARP" (ByVal DestIP As Int32, ByVal SrcIP As Int32, ByVal pMacAddr() As Byte, ByRef PhyAddrLen As Int32) As Int32

    Public Shared Function GetMac(ByVal ForIP As String) As String
        Try
            Dim addr As Net.IPAddress = Net.IPAddress.Parse(ForIP)
            Dim addrMe As Net.IPAddress = Net.IPAddress.Parse("10.3.1.247")
            Dim mac(5) As Byte
            Dim IP() As Byte
            Dim IPInt As Int32

            IP = addr.GetAddressBytes()
            IPInt = BitConverter.ToInt32(IP, 0)

            Dim len As Int32 = mac.Length
            SendARP(BitConverter.ToInt32(addr.GetAddressBytes(), 0), 0, mac, len)

            Dim macAddress As String = BitConverter.ToString(mac, 0, mac.Length)
            Return macAddress

        Catch ex As Exception

        End Try
        Return ""
    End Function
0

Featured Post

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question