char[4] data type in c for storing IPs - VB.NET equivalence

E

emitojleyes

Hi everyone:
i read from the documentation of a dll that there is a struct that uses
char[4] for storing an IP address. How can it be? and how come i can get to
representate the same value in a string VB.NET data type, knowing that this
is its equivalence (for char4)?
Next is the data type definition


'''unsigned char[4]

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=4)> _

Public IP As String



Thanks very much for your help!
 
D

David Anton

'unsigned char' is Byte in VB.
As an example, the equivalent to the following C++:
unsigned char test[4];
is:
Dim test(3) As Byte
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
E

emitojleyes

Hi david, and thanks very muuch for your reply.
What i worte in vb.net, actually i didn't do it. I used an application
avaiable in
http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx,
that apparently helped me a lot, because, before this problem, i was getting
an error and couldn't find the solution.
Now, you say the data type must be byte... so, what value should i write for
the following ip address, for example: 192.168.010.008?
I tried replacing string for byte in
<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=4)>, but i got an error...
How can i continue? Thanks again david.
--
Emilio Javier Leyes
Técnico Universitario en Informática
Sistema de Emergencias 911
Salta, Argentina


David Anton said:
'unsigned char' is Byte in VB.
As an example, the equivalent to the following C++:
unsigned char test[4];
is:
Dim test(3) As Byte
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI


emitojleyes said:
Hi everyone:
i read from the documentation of a dll that there is a struct that uses
char[4] for storing an IP address. How can it be? and how come i can get to
representate the same value in a string VB.NET data type, knowing that this
is its equivalence (for char4)?
Next is the data type definition


'''unsigned char[4]
Public IP As String



Thanks very much for your help!
--
Emilio Javier Leyes
Técnico Universitario en Informática
Sistema de Emergencias 911
Salta, Argentina
 
G

Göran Andersson

emitojleyes said:
Hi david, and thanks very muuch for your reply.
What i worte in vb.net, actually i didn't do it. I used an application
avaiable in
http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx,
that apparently helped me a lot, because, before this problem, i was getting
an error and couldn't find the solution.
Now, you say the data type must be byte... so, what value should i write for
the following ip address, for example: 192.168.010.008?

New Byte() { 192, 168, 10, 8 }
I tried replacing string for byte in:
<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=4)>, but i got an error...
How can i continue? Thanks again david.

You want a byte array with the size 4, so it should be something like:

<MarshalAsAttribute(UnmanagedType.LPArray, SizeConst:=4,
ArraySubType:=UnmanagedType.U1>
 
H

Herfried K. Wagner [MVP]

Göran Andersson said:
New Byte() { 192, 168, 10, 8 }


You want a byte array with the size 4, so it should be something like:

<MarshalAsAttribute(UnmanagedType.LPArray, SizeConst:=4,
ArraySubType:=UnmanagedType.U1>

Maybe 'ByValArray' is more suitable as 'LPArray' is not used to store the
array elements inside the structure.
 
E

emitojleyes

Thanks very much for the suggestions!
I'll be trying it tomorrow, and let you know, so, PLEASE don't lose track of
this thread.
Thanks again!
 
B

Bill McCarthy

As an alternative, I would suggest using an Int32 as that gives you the four
bytes. You can also use a struct or do explicit layout so as you have a
union of an Int32 and four bytes, e.g:


Structure IPAddress
Public a As Byte
Public b As Byte
Public c As Byte
Public d As Byte
End Structure


Or:



<StructLayout(LayoutKind.Explicit)> _
Structure IPAddress
<FieldOffset(0)> Public a As Byte
<FieldOffset(1)> Public b As Byte
<FieldOffset(2)> Public c As Byte
<FieldOffset(3)> Public d As Byte

<FieldOffset(0)> Public value As Int32

Public Overrides Function ToString() As String
Return String.Format("{0}.{1}.{2}.{3}", a, b, c, d)
End Function
End Structure
 
E

emitojleyes

Hi everyone:
The problem is still there... i cannot solve it.
Goran, i have used the data type you suggest me:
<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=4)> _
Public IP As Byte()
and assigned the values:
.IP = New Byte() {192, 168, 10, 8}
But it didn't work.
And still didn't try what bill told me to do, because i don't understand
much how to do it...
As this issue is beating me, i have taken extremes measures. Below are the
links to download the dlls flies, the advanced spectifications document, and
the user manual, i will really appreciate you look the technical specs doc,
becaouse maybe i'm having a mistake in other issue that you could notice
better than me.

Also i tried as Herfried told me:

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=4, ArraySubType:=System.Runtime.InteropServices.UnmanagedType.U1)>
_
Public IP As Byte()
but didn't work either.
Advanced Specifications document:
http://www.fundesnoa.org.ar/wison/AdvancedDoor_Spec_02.pdf

WisClient library DLL:
http://www.fundesnoa.org.ar/wison/WisClient.dll

WisServer library DLL:
http://www.fundesnoa.org.ar/wison/WisServer.dll

User Manual:
http://www.fundesnoa.org.ar/wison/DR168_Eng.pdf

Thanks very much in advance, for any comments about this issue.
Best regards,
 
B

Bill McCarthy

Hi emitojleyes,

Define the structure as I showed (either one), e.g:


<StructLayout(LayoutKind.Explicit)> _
Structure IPAddress
<FieldOffset(0)> Public a As Byte
<FieldOffset(1)> Public b As Byte
<FieldOffset(2)> Public c As Byte
<FieldOffset(3)> Public d As Byte

<FieldOffset(0)> Public value As Int32

Public Overrides Function ToString() As String
Return String.Format("{0}.{1}.{2}.{3}", a, b, c, d)
End Function
End Structure


Then declare the field or parameter as Byval ip As IPAddress
 
E

emitojleyes

I've tried what you Bill told me, but still doesn't work...
i think it's not a big problem to slve this issue, but i am a real beginner
at this, and don't know where else to continue looking for the solution.
That's why i put the links for downloading the documentation of this device.
I hope you take a look at them and understand the problem, and realise it's
not big deal... or is it?
Next, i'll put the source code of the windows form where it is the button
that TESTS the device. All the data definition was made automatically by this
program i mentioned before, with the exception of the last attemps i made,
modifying the IP member of systemdata, following Bill's advice.
I am really thankful for all the help i got from this forum, and hope to get
to solve it; without your help... it'd be really impossible for me.

---------------
Public Class Form1



<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential,
CharSet:=System.Runtime.InteropServices.CharSet.[Ansi])> _
Public Structure SYSTEMDATA

'''unsigned char
Public Version As Byte


<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)> _
Structure IPAddress
<System.Runtime.InteropServices.FieldOffset(0)> Public a As Byte
<System.Runtime.InteropServices.FieldOffset(1)> Public b As Byte
<System.Runtime.InteropServices.FieldOffset(2)> Public c As Byte
<System.Runtime.InteropServices.FieldOffset(3)> Public d As Byte
<System.Runtime.InteropServices.FieldOffset(0)> Public value As
Int32

Public Overrides Function ToString() As String
Return String.Format("{0}.{1}.{2}.{3}", a, b, c, d)
End Function
End Structure

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Struct,
SizeConst:=4, ArraySubType:=System.Runtime.InteropServices.UnmanagedType.U1)>
_
Public IP As IPAddress

'''unsigned char[4]

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=4)> _
Public NetMask As String

'''unsigned char[4]

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=4)> _
Public Gateway As String

'''unsigned char
Public BaudRate As Byte

'''unsigned char[3]

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=3)> _
Public DeviceID As Byte()

'''unsigned short
Public UserNo As UShort

'''unsigned int
Public LogNo As UInteger

'''unsigned char
Public AuthenMode As Byte

'''unsigned char
Public UartMode As Byte

'''unsigned char
Public VoiceEN As Byte

'''unsigned char[12]

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=12)> _
Public ServerIP As String

'''unsigned char
Public IDLength As Byte

'''unsigned char[20]
'''

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=20)> _
Public Reserved As String

'''DOORDATA->_DOORDATA
Public DoorData As DOORDATA

'''unsigned char[16]

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=16)> _
Public DeviceName As String

'''unsigned char
Public HasDevice As Byte

'''unsigned char
Public ProtocolPswd As Byte

'''unsigned char[7]

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=7)> _
Public Reserved1 As String
End Structure


<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential,
CharSet:=System.Runtime.InteropServices.CharSet.[Ansi])> _
Public Structure DOORDATA

'''unsigned short
Public DoorOpenSec As UShort

'''unsigned short
Public DoorOverSec As UShort

'''unsigned short
Public AlarmTime As UShort

'''unsigned char
Public AlarmEnable As Byte

'''unsigned char[9]

<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=9)> _
Public Reserved As String
End Structure

Partial Public Class NativeMethods

'''Return Type: int
'''pwd: unsigned char*
'''CurSystemData: SYSTEMDATA->_SYSTEMDATA
<System.Runtime.InteropServices.DllImportAttribute("WisClient.dll",
EntryPoint:="Wis_Diagnose")> _
Public Shared Function Wis_Diagnose(ByVal pwd As System.IntPtr,
ByVal CurSystemData As SYSTEMDATA) As Integer
End Function
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim systemdata1 As New Form1.SYSTEMDATA
Dim doordata1 As New Form1.DOORDATA
Dim a As System.IntPtr
With systemdata1
.ProtocolPswd = "00000000"
.IP.a = 192
.IP.b = 168
.IP.c = 10
.IP.d = 8


.NetMask = "255.255.255.0"
.ServerIP = "192.168.10.4"
.Reserved = "00000000"
.Reserved1 = "00000000"
.DeviceID = New Byte() {0, 0, 0}
.Gateway = "192.168.10.1"
.IDLength = 3
.HasDevice = 3
.DoorData.Reserved = "00000000"

End With

a = "00000000"
MsgBox(NativeMethods.Wis_Diagnose(0, systemdata1))

End Sub
End Class
---------------



For those who want to see the documentation and download the DLL i cant make
work, the links are below.

Best regards,
Emilio Javier Leyes
Técnico Universitario en Informática
Sistema de Emergencias 911
Salta, Argentina



Advanced Specifications document:
http://www.fundesnoa.org.ar/wison/AdvancedDoor_Spec_02.pdf

WisClient library DLL:
http://www.fundesnoa.org.ar/wison/WisClient.dll

WisServer library DLL:
http://www.fundesnoa.org.ar/wison/WisServer.dll

User Manual:
http://www.fundesnoa.org.ar/wison/DR168_Eng.pdf
 
B

Bill McCarthy

Hi Emilio,

This is roughly what I meant. You'll need to check which are IP addresses and which aren't:

Imports System.Runtime.InteropServices





Public Class Form1





<StructLayout(LayoutKind.Explicit)> _

Structure IPAddress

<FieldOffset(0)> Public a As Byte

<FieldOffset(1)> Public b As Byte

<FieldOffset(2)> Public c As Byte

<FieldOffset(3)> Public d As Byte

<FieldOffset(0)> Public value As Int32



Public Overrides Function ToString() As String

Return String.Format("{0}.{1}.{2}.{3}", a, b, c, d)

End Function

End Structure







<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.[Ansi])> _

Public Structure SYSTEMDATA



'''unsigned char

Public Version As Byte



'''unsigned char[4]

Public IP As IPAddress



'''unsigned char[4]

Public NetMask As IPAddress



'''unsigned char[4]

Public Gateway As IPAddress



'''unsigned char

Public BaudRate As Byte



'''unsigned char[3]

Public DeviceID As Int32



'''unsigned short

Public UserNo As UShort



'''unsigned int

Public LogNo As UInteger



'''unsigned char

Public AuthenMode As Byte



'''unsigned char

Public UartMode As Byte



'''unsigned char

Public VoiceEN As Byte



'''unsigned char[12]

<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=12)> _

Public ServerIP As String



'''unsigned char

Public IDLength As Byte



'''unsigned char[20]

<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=20)> _

Public Reserved As String



'''DOORDATA->_DOORDATA

Public DoorData As DOORDATA



'''unsigned char[16]

<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=16)> _

Public DeviceName As String



'''unsigned char

Public HasDevice As Byte



'''unsigned char

Public ProtocolPswd As Byte



'''unsigned char[7]

<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=7)> _

Public Reserved1 As String

End Structure





<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.[Ansi])> _

Public Structure DOORDATA



'''unsigned short

Public DoorOpenSec As UShort



'''unsigned short

Public DoorOverSec As UShort



'''unsigned short

Public AlarmTime As UShort



'''unsigned char

Public AlarmEnable As Byte



'''unsigned char[9]



<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=9)> _

Public Reserved As String

End Structure
 
E

emitojleyes

Thanks very much for keeping up poasting in this thread; after this issue, if
i solve it, i'll dedicate to interop... hehe...
Yes, the data structure definition has another variables that store IP
address; so i'll define them as my new data structure IpAddress, as you
indicated me.
But, if you an see in page 8 of AdvancedDoor_Spec_02.pdf, in "transmitted
data", it only asks for DeviceId and "Reserved" (the password). So, in tHe
wis_diagnose function, the pwd parameter must be this "reserved" value. And i
only give the DeviceID and the IpAddress in the SystemData structure; i don't
think i have to enter values for all the variables (netmask, ipserver,
baudrate, voiceEn, etc.). That's how i am trying so far, and doesn't work...
Do u think i must assign values to all the systemdata members? No that i
think it, the big question is... the error is in the IP Address? Damn!
Thanks VERY MUCH for your help

--
Emilio Javier Leyes
Técnico Universitario en Informática
Sistema de Emergencias 911
Salta, Argentina


Bill McCarthy said:
Hi Emilio,

This is roughly what I meant. You'll need to check which are IP addresses
and which aren't:

Imports System.Runtime.InteropServices





Public Class Form1





<StructLayout(LayoutKind.Explicit)> _

Structure IPAddress

<FieldOffset(0)> Public a As Byte

<FieldOffset(1)> Public b As Byte

<FieldOffset(2)> Public c As Byte

<FieldOffset(3)> Public d As Byte

<FieldOffset(0)> Public value As Int32



Public Overrides Function ToString() As String

Return String.Format("{0}.{1}.{2}.{3}", a, b, c, d)

End Function

End Structure







<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.[Ansi])>
_

Public Structure SYSTEMDATA



'''unsigned char

Public Version As Byte



'''unsigned char[4]

Public IP As IPAddress



'''unsigned char[4]

Public NetMask As IPAddress



'''unsigned char[4]

Public Gateway As IPAddress



'''unsigned char

Public BaudRate As Byte



'''unsigned char[3]

Public DeviceID As Int32



'''unsigned short

Public UserNo As UShort



'''unsigned int

Public LogNo As UInteger



'''unsigned char

Public AuthenMode As Byte



'''unsigned char

Public UartMode As Byte



'''unsigned char

Public VoiceEN As Byte



'''unsigned char[12]

<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=12)> _

Public ServerIP As String



'''unsigned char

Public IDLength As Byte



'''unsigned char[20]

<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=20)> _

Public Reserved As String



'''DOORDATA->_DOORDATA

Public DoorData As DOORDATA



'''unsigned char[16]

<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=16)> _

Public DeviceName As String



'''unsigned char

Public HasDevice As Byte



'''unsigned char

Public ProtocolPswd As Byte



'''unsigned char[7]

<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=7)> _

Public Reserved1 As String

End Structure





<StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.[Ansi])>
_

Public Structure DOORDATA



'''unsigned short

Public DoorOpenSec As UShort



'''unsigned short

Public DoorOverSec As UShort



'''unsigned short

Public AlarmTime As UShort



'''unsigned char

Public AlarmEnable As Byte



'''unsigned char[9]



<MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=9)> _

Public Reserved As String

End Structure
 
B

Bill McCarthy

Hi emitojleyes,
But, if you an see in page 8 of AdvancedDoor_Spec_02.pdf, in "transmitted
data", it only asks for DeviceId and "Reserved" (the password)

That url you posted doesn't work from here, so I can't see the document. If
you have the C++ definition, please post it here. (or perhaps a different
URL)
 
E

emitojleyes

Hi
I think i made it! (putting the manual avaiable in the web, not making work
the device...)
The url is http://www.fundesnoa.org.ar/wison/advanceddoor_spec_02.html
There is all the document content; the taiwanese people who sells this
device told me that with this document is enough, and that many people could
do it... I hope all this issue is due to my ignorance, so i learn and the
problem is solved....
Thanks very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top