Click here to Skip to main content
15,890,282 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: I need some hints about sqlcommand Pin
dptalt25-Nov-05 2:12
dptalt25-Nov-05 2:12 
GeneralRe: I need some hints about sqlcommand Pin
y_mmohd25-Nov-05 4:08
y_mmohd25-Nov-05 4:08 
GeneralRe: I need some hints about sqlcommand Pin
Colin Angus Mackay25-Nov-05 4:27
Colin Angus Mackay25-Nov-05 4:27 
AnswerRe: I need some hints about sqlcommand Pin
y_mmohd25-Nov-05 20:18
y_mmohd25-Nov-05 20:18 
QuestionHow to play voice recorded as byte format? Pin
WhineyGrace24-Nov-05 20:38
WhineyGrace24-Nov-05 20:38 
QuestionMAC address retrieval Pin
abhinish24-Nov-05 18:54
abhinish24-Nov-05 18:54 
AnswerRe: MAC address retrieval Pin
Briga24-Nov-05 22:15
Briga24-Nov-05 22:15 
AnswerRe: MAC address retrieval Pin
Dave Kreskowiak25-Nov-05 6:36
mveDave Kreskowiak25-Nov-05 6:36 
OK. Well, I tried your code and found a problem with the data types your using to pass the Source and Destination IP's to SendARP. Your using Integer, which are signed 32-bit numbers. The sign limits the numbers range from -2147483648 to 2147483647. A signed Integer reserves the first bit for the sign, leaving the remaining 31 bits for the number.

Why is this important? Because when you convert an IpAddress (essentially 4 8-bit numbers) to an Integer (32-bits long), part of the address of the first byte ends up in the sign bit of a signed Integer. When the IpAddress object returns its integer version of the address it holds, it's returning a signed Long, or 64-bit number. This number can exceed the max value a SIGNED Integer can hold! But, it won't exceed the max value an UNSIGNED Integer can, which is 4294967295. When you convert that value to a signed Integer, the sign bit is retained, reguardless of the value that was in that bit when the number was converted to a 32-bit value. This little conversion gotcha changes the address your trying to resolve the MAC for! When you convert the 64-bit signed integer down to a 32-bit unsigned value, the sign bit is dropped since it isn't used in the new data type.

After changing the parameters you're using in your SendARP call, it works great!
[might help if I pasted in the code correctly...]
Imports System.Net
 
Public Class IpHelpers
    Declare Auto Function SendARP Lib "iphlpapi" ( _
                        ByVal DestIp As UInteger, _
                        ByVal SrcIp As UInteger, _
                        ByVal MacBytes() As Byte, _
                        ByVal PhysAddrLength As Integer _
                        ) As Integer
 
    Public Shared Function GetMacAsStringFromIp(ByVal TargetIp As IPAddress) As String
        Dim rc As Integer
        Dim MacBytes(6) As Byte
        Dim Length As Integer = MacBytes.Length
 
        Try
            Dim Ip32 As UInteger = CType(TargetIp.Address, UInteger)
            rc = SendARP(Ip32, 0, MacBytes, Length)
        Catch ex As Exception
            ' This is usefull for catching any .NET Exceptions thrown because of coding problems.
            MsgBox(ex.Message)
        End Try
 
        If rc <> 0 Then
            ' SendARP will fail with error 31 if the Target IPAddress doesn't respond.
            ' This can happen if the IP really doesn't exist, isn't on the local subnet
            ' or if the conversion of the IPAddress to a number that SendARP expects
            ' isn't done properly.
            Throw New ArgumentException(String.Format("Win32 Error {0} returned by SendARP!", rc))
        End If
       
        Return String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}", _
                        MacBytes(0), MacBytes(1), MacBytes(2), _
                        MacBytes(3), MacBytes(4), MacBytes(5))
    End Function
End Class



RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome


-- modified at 12:58 Friday 25th November, 2005
GeneralRe: MAC address retrieval Pin
abhinish25-Nov-05 18:32
abhinish25-Nov-05 18:32 
GeneralRe: MAC address retrieval Pin
Dave Kreskowiak27-Nov-05 15:24
mveDave Kreskowiak27-Nov-05 15:24 
GeneralRe: MAC address retrieval Pin
abhinish28-Nov-05 18:49
abhinish28-Nov-05 18:49 
AnswerRe: MAC address retrieval Pin
GregRH16-Dec-05 13:01
GregRH16-Dec-05 13:01 
GeneralRe: MAC address retrieval Pin
abhinish16-Dec-05 18:06
abhinish16-Dec-05 18:06 
QuestionHand Cursor in .NET Pin
marclile24-Nov-05 17:26
marclile24-Nov-05 17:26 
AnswerRe: Hand Cursor in .NET Pin
Christian Graus24-Nov-05 17:53
protectorChristian Graus24-Nov-05 17:53 
AnswerRe: Hand Cursor in .NET Pin
progload25-Nov-05 14:11
progload25-Nov-05 14:11 
Questionregister accessing from VB6 Pin
vani_bel24-Nov-05 17:01
vani_bel24-Nov-05 17:01 
AnswerRe: register accessing from VB6 Pin
Dave Kreskowiak25-Nov-05 4:16
mveDave Kreskowiak25-Nov-05 4:16 
QuestionForce Panel to scroll Pin
spelltwister24-Nov-05 15:44
spelltwister24-Nov-05 15:44 
QuestionCoding web camera Pin
Amr M. K.24-Nov-05 6:43
Amr M. K.24-Nov-05 6:43 
AnswerRe: Coding web camera Pin
Briga24-Nov-05 7:17
Briga24-Nov-05 7:17 
QuestionEnforcing Referrential Integrity in VB.NET Pin
R. A. Abbasi24-Nov-05 4:56
R. A. Abbasi24-Nov-05 4:56 
AnswerRe: Enforcing Referrential Integrity in VB.NET Pin
Steve Pullan24-Nov-05 18:31
Steve Pullan24-Nov-05 18:31 
GeneralRe: Enforcing Referrential Integrity in VB.NET Pin
R. A. Abbasi24-Nov-05 18:37
R. A. Abbasi24-Nov-05 18:37 
GeneralRe: Enforcing Referrential Integrity in VB.NET Pin
Steve Pullan24-Nov-05 18:51
Steve Pullan24-Nov-05 18:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.