IPAddress.Address is negative

  • Thread starter Thread starter Milosz - [playseven.com]
  • Start date Start date
M

Milosz - [playseven.com]

Hello,

I perform a ping using the IcmpSendEcho Function from iphlpapi:
<DllImport("iphlpapi")> _

Private Shared Function IcmpSendEcho(ByVal IcmpHandle As IntPtr, ByVal
DestinationAddress As System.UInt32, ByVal RequestData() As Byte, ByVal
RequestSize As Integer, ByVal RequestOptions As IntPtr, ByVal ReplyBuffer()
As Byte, ByVal ReplySize As Int32, ByVal Timeout As Int32) As System.UInt32


This Function requires a Destination Adress as an unsigned Int32. I get it
from the IPAddress.Address Object.

Public Structure PingOptions
Dim IPAdress As IPAddress
Dim TimeOUT As Integer
End Structure
dim opt as PingOptions

opt.IPAdress = IPAddress.Parse("192.168.1.128") ' Adresses bigger then
A.B.C.127, have a negative adress value !!!

ipaddr = System.UInt32.Parse(opt.IPAdress.Address) ' and couse exception
here

Dim ret As System.UInt32

'but i need an unsigned int here in the second parameter
ret = IcmpSendEcho(h, opt.ipaddr, RequestData, CInt(RequestData.Length),
IntPtr.Zero, reply._Data, reply._Data.Length, opt.TimeOUT)

The Problem is that for IPAdresses, where the last Byte is bigger then 127.
the generated Long Value for Adress is negative and the Parse Method to the
uint32 couses an exception.

Or should i declare the DestinationAdress as Long in the IcmpSendEcho and
put in IPAdress.Address value directly.
My assumption is that the Address value is created in the correct bitformat
but becouse the CF is declaring it as a long it is interpreted negative.

Or how else to get the correct Long Value from IPAdress.Address ?

THX in advance


--
-> Milosz Weckowski
www.playseven.com

mailto:[email protected]
ICQ Number: 84867613

Get the enhanced Progressbar and a fine Colorpicker for the Compact Framwork
for free:
http://www.playseven.com/11620/p7_Controls.html
 
With unsigned numbers there is no such thing as negative. It simply means
that your LSB is on. If you use unchecked you can stuff a "negative" signed
number into an unsigned.

-Chris
 
I'm sorry. I do not understand.
IPAdress.Adress is signed becouse it's a simple Int32, and IcmpSendEcho
wants to have an unsigned int.

What is an LSB and how to uncheck it?

Do you mean i have to pass the long value 'as it is' to IcmpSendEcho
Function ?

regards

Milosz
 
Do you mean LSB opp. MSB ?
If yes, i still don't know what to do ..

regards
Milosz
 
Thats fine, but thats C#... and I'm using VB.net.
However even if using C# with unchecked Option, if i would pass the
truncated Adress Value to the function, would it ping the right host ?
I mean the IPAdress.Adress onbejct is possible to return an negative value.
It must be possible to translate it in an adequate unsigned int for the
P/Invoke function.

Or, how to simulate the checked behavior in VB:net use math.abs() or
something else ?

regards

Milosz



Kevin Hutchison said:
Try looking at this --
http://msdn.microsoft.com/library/d...sref/html/vclrfcheckeduncheckedstatements.asp




Milosz - said:
I'm sorry. I do not understand.
IPAdress.Adress is signed becouse it's a simple Int32, and IcmpSendEcho
wants to have an unsigned int.

What is an LSB and how to uncheck it?

Do you mean i have to pass the long value 'as it is' to IcmpSendEcho
Function ?

regards

Milosz





"Chris Tacke, eMVP" <ctacke[at]Open_NET_CF[dot]org> schrieb im Newsbeitrag
With unsigned numbers there is no such thing as negative. It simply means
that your LSB is on. If you use unchecked you can stuff a "negative" signed
number into an unsigned.

-Chris

Hello,

I perform a ping using the IcmpSendEcho Function from iphlpapi:
<DllImport("iphlpapi")> _

Private Shared Function IcmpSendEcho(ByVal IcmpHandle As IntPtr, ByVal
DestinationAddress As System.UInt32, ByVal RequestData() As Byte, ByVal
RequestSize As Integer, ByVal RequestOptions As IntPtr, ByVal
ReplyBuffer()
As Byte, ByVal ReplySize As Int32, ByVal Timeout As Int32) As
System.UInt32


This Function requires a Destination Adress as an unsigned Int32. I
get
it
from the IPAddress.Address Object.

Public Structure PingOptions
Dim IPAdress As IPAddress
Dim TimeOUT As Integer
End Structure
dim opt as PingOptions

opt.IPAdress = IPAddress.Parse("192.168.1.128") ' Adresses bigger then
A.B.C.127, have a negative adress value !!!

ipaddr = System.UInt32.Parse(opt.IPAdress.Address) ' and couse exception
here

Dim ret As System.UInt32

'but i need an unsigned int here in the second parameter
ret = IcmpSendEcho(h, opt.ipaddr, RequestData, CInt(RequestData.Length),
IntPtr.Zero, reply._Data, reply._Data.Length, opt.TimeOUT)

The Problem is that for IPAdresses, where the last Byte is bigger then
127.
the generated Long Value for Adress is negative and the Parse Method to
the
uint32 couses an exception.

Or should i declare the DestinationAdress as Long in the
IcmpSendEcho
and
put in IPAdress.Address value directly.
My assumption is that the Address value is created in the correct
bitformat
but becouse the CF is declaring it as a long it is interpreted negative.

Or how else to get the correct Long Value from IPAdress.Address ?

THX in advance


--
-> Milosz Weckowski
www.playseven.com

mailto:[email protected]
ICQ Number: 84867613

Get the enhanced Progressbar and a fine Colorpicker for the Compact
Framwork
for free:
http://www.playseven.com/11620/p7_Controls.html
 
The number is simply a 4-byte value representing the four IP address
components. That is, if your target IP address is 172.16.0.1, the 4-byte
value is going to be something like:

0xac100001

You'll note that, in this case, this number would appear to be negative
because the most-significant bit *is* set. In fact, this value would appear
to be -1408237567, if my math is right.

Sounds like your problem is the declaration of the P/Invoke or the inability
to convert from this signed integer value to an unsigned one. In C#, I'd do
this:

unsigned int ui = unchecked((unsigned int)-1408237567);

to perform this conversion and avoid an out-of-range error. Is there an
equivalent way to do this from VB.NET? If not, you may want to change your
P/Invoke declaration to accept a signed, rather than unsigned, integer.

Paul T.

Milosz - said:
Thats fine, but thats C#... and I'm using VB.net.
However even if using C# with unchecked Option, if i would pass the
truncated Adress Value to the function, would it ping the right host ?
I mean the IPAdress.Adress onbejct is possible to return an negative value.
It must be possible to translate it in an adequate unsigned int for the
P/Invoke function.

Or, how to simulate the checked behavior in VB:net use math.abs() or
something else ?

regards

Milosz



Kevin Hutchison said:
Try looking at this --
http://msdn.microsoft.com/library/d...sref/html/vclrfcheckeduncheckedstatements.asp
Milosz - said:
I'm sorry. I do not understand.
IPAdress.Adress is signed becouse it's a simple Int32, and IcmpSendEcho
wants to have an unsigned int.

What is an LSB and how to uncheck it?

Do you mean i have to pass the long value 'as it is' to IcmpSendEcho
Function ?

regards

Milosz





"Chris Tacke, eMVP" <ctacke[at]Open_NET_CF[dot]org> schrieb im Newsbeitrag
With unsigned numbers there is no such thing as negative. It simply means
that your LSB is on. If you use unchecked you can stuff a "negative"
signed
number into an unsigned.

-Chris

Hello,

I perform a ping using the IcmpSendEcho Function from iphlpapi:
<DllImport("iphlpapi")> _

Private Shared Function IcmpSendEcho(ByVal IcmpHandle As IntPtr, ByVal
DestinationAddress As System.UInt32, ByVal RequestData() As Byte, ByVal
RequestSize As Integer, ByVal RequestOptions As IntPtr, ByVal
ReplyBuffer()
As Byte, ByVal ReplySize As Int32, ByVal Timeout As Int32) As
System.UInt32


This Function requires a Destination Adress as an unsigned Int32.
I
get
it
from the IPAddress.Address Object.

Public Structure PingOptions
Dim IPAdress As IPAddress
Dim TimeOUT As Integer
End Structure
dim opt as PingOptions

opt.IPAdress = IPAddress.Parse("192.168.1.128") ' Adresses bigger then
A.B.C.127, have a negative adress value !!!

ipaddr = System.UInt32.Parse(opt.IPAdress.Address) ' and couse exception
here

Dim ret As System.UInt32

'but i need an unsigned int here in the second parameter
ret = IcmpSendEcho(h, opt.ipaddr, RequestData, CInt(RequestData.Length),
IntPtr.Zero, reply._Data, reply._Data.Length, opt.TimeOUT)

The Problem is that for IPAdresses, where the last Byte is bigger then
127.
the generated Long Value for Adress is negative and the Parse
Method
to
the
uint32 couses an exception.

Or should i declare the DestinationAdress as Long in the IcmpSendEcho
and
put in IPAdress.Address value directly.
My assumption is that the Address value is created in the correct
bitformat
but becouse the CF is declaring it as a long it is interpreted negative.

Or how else to get the correct Long Value from IPAdress.Address ?

THX in advance


--
-> Milosz Weckowski
www.playseven.com

mailto:[email protected]
ICQ Number: 84867613

Get the enhanced Progressbar and a fine Colorpicker for the Compact
Framwork
for free:
http://www.playseven.com/11620/p7_Controls.html
 
Something like this, for example:

Dim ui32 As UInt32 = System.Convert.ToUInt32(-1408237567)



Paul T.



Paul G. Tobey said:
The number is simply a 4-byte value representing the four IP address
components. That is, if your target IP address is 172.16.0.1, the 4-byte
value is going to be something like:

0xac100001

You'll note that, in this case, this number would appear to be negative
because the most-significant bit *is* set. In fact, this value would appear
to be -1408237567, if my math is right.

Sounds like your problem is the declaration of the P/Invoke or the inability
to convert from this signed integer value to an unsigned one. In C#, I'd do
this:

unsigned int ui = unchecked((unsigned int)-1408237567);

to perform this conversion and avoid an out-of-range error. Is there an
equivalent way to do this from VB.NET? If not, you may want to change your
P/Invoke declaration to accept a signed, rather than unsigned, integer.

Paul T.

Milosz - said:
Thats fine, but thats C#... and I'm using VB.net.
However even if using C# with unchecked Option, if i would pass the
truncated Adress Value to the function, would it ping the right host ?
I mean the IPAdress.Adress onbejct is possible to return an negative value.
It must be possible to translate it in an adequate unsigned int for the
P/Invoke function.

Or, how to simulate the checked behavior in VB:net use math.abs() or
something else ?

regards

Milosz
http://msdn.microsoft.com/library/d...sref/html/vclrfcheckeduncheckedstatements.asp
I'm sorry. I do not understand.
IPAdress.Adress is signed becouse it's a simple Int32, and IcmpSendEcho
wants to have an unsigned int.

What is an LSB and how to uncheck it?

Do you mean i have to pass the long value 'as it is' to IcmpSendEcho
Function ?

regards

Milosz





"Chris Tacke, eMVP" <ctacke[at]Open_NET_CF[dot]org> schrieb im Newsbeitrag
With unsigned numbers there is no such thing as negative. It simply
means
that your LSB is on. If you use unchecked you can stuff a "negative"
signed
number into an unsigned.

-Chris

Hello,

I perform a ping using the IcmpSendEcho Function from iphlpapi:
<DllImport("iphlpapi")> _

Private Shared Function IcmpSendEcho(ByVal IcmpHandle As IntPtr, ByVal
DestinationAddress As System.UInt32, ByVal RequestData() As Byte,
ByVal
RequestSize As Integer, ByVal RequestOptions As IntPtr, ByVal
ReplyBuffer()
As Byte, ByVal ReplySize As Int32, ByVal Timeout As Int32) As
System.UInt32


This Function requires a Destination Adress as an unsigned
Int32.
I bigger
then bigger
then
 
Milosz,
Or should i declare the DestinationAdress as Long in the IcmpSendEcho and
put in IPAdress.Address value directly.

No, but declaring it as Integer should work, regardless if the callee
interprest the argument as signed or not.



Mattias
 
yes, that worked !

THX a lot !


Mattias Sjögren said:
Milosz,


No, but declaring it as Integer should work, regardless if the callee
interprest the argument as signed or not.



Mattias
 
Back
Top