• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

Convert this C structure to a VB type

Looking for someone who can convert this C structure to a VB type:

typedef struct {
  union {
    struct {
      u_char s_b1,s_b2,s_b3,s_b4;
    } S_un_b;
    struct {
      u_short s_w1,s_w2;
    } S_un_w;
    u_long S_addr;
  } S_un;
} IPAddr;

For the points: provide a working, converted Type.
0
DrDamnit
Asked:
DrDamnit
  • 4
  • 2
1 Solution
 
Erick37Commented:
VB does not support Unions, so you will have to choose which data type you would like your IP Address in:

'One Long type (4 bytes)
Private Type IPAddr
  S_Addr as Long
End Type

=or=

'Two Integer types (4 bytes)
Private Type IPAddr
  S_W1 as Integer
  S_W2 as Integer
End Type

=or=

'4 bytes
Private Type IPAddr
  S_B1 as Byte
  S_B2 as Byte
  S_B3 as Byte
  S_B4 as Byte
End Type
0
 
Erick37Commented:
But, you can always use CopyMemory to simulate a Union when you need to access the variables as different data types.

Example:

Option Explicit

'One Long type (4 bytes)
Private Type IPAddr1
  S_Addr As Long
End Type

'Two Integer types (4 bytes)
Private Type IPAddr2
  S_W1 As Integer
  S_W2 As Integer
End Type

'4 bytes
Private Type IPAddr4
  S_B1 As Byte
  S_B2 As Byte
  S_B3 As Byte
  S_B4 As Byte
End Type

Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (dest As Any, Source As Any, ByVal bytes As Long)

Private Sub Command1_Click()
   
    'Our "Union"
    Dim ip1 As IPAddr1
    Dim ip2 As IPAddr2
    Dim ip4 As IPAddr4
   
    'assign to the 4 byte version
    ip4.S_B1 = 192
    ip4.S_B2 = 168
    ip4.S_B3 = 1
    ip4.S_B4 = 1
   
    'copy it over to the other 2 types
    Call CopyMemory(ip1, ip4, 4)
    Call CopyMemory(ip2, ip4, 4)
   
    'display
    Debug.Print Hex(ip1.S_Addr)
    Debug.Print Hex(ip2.S_W2), Hex(ip2.S_W1)
    Debug.Print Hex(ip4.S_B4), Hex(ip4.S_B3), Hex(ip4.S_B2), Hex(ip4.S_B1)


End Sub
0
 
anthonywjones66Commented:
VB doesn't support unions nor does it support unsigned longs or words however given:-

Type tIPBytes
{
  b1 byte
  b2 byte
  b3 byte
  b4 byte
}

Private m_lngIP
Private m_tIPBytes

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

An IP address coded into m_tIPBytes can be mapped into m_lngIP with:-

CopyMemory m_lngIP, m_tIPBytes, 4

and vice versa.

I'm guessing in reality mapping an IP address to 2 signed integers is not very useful to you so I haven't shown them.  However the techinique is clear.

BTW,  Do you intend to compare IP ranges as long values?  If so this techinque is flawed since VB longs are signed.  To make the long values comparable you will need:-

CopyMemory m_lngIP, m_tIPBytes, 4
m_lngIP = m_lngIP Xor &h80000000

And

CopyMemory m_tIPBytes, m_lngIP, 4
m_tIPBytes.B4 = m_tIPBytes xor &h80

Of course this means the m_lngIP is no longer an exact copy of the bytes but is a more useful value in VB.

Anthony.




0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DrDamnitAuthor Commented:
0
 
Erick37Commented:
Any of the above should work - also passing in a Long should also work.  It's just looking for a pointer to a variable 4 bytes wide.

Make sure your declare statement reflects which type you choose.
0
 
Erick37Commented:
I only found one example of using this function in VB, and it uses a Long:

Public Declare Function AddIPAddress Lib "IPHlpApi" (ByVal Address As Long, ByVal IpMask As Long, ByVal IfIndex As Long, _
    NTEContext As Long, NTEInstance As Long) As Long

http://www.experts-exchange.com/Programming/Q_20650057.html
0
 
anthonywjones66Commented:
Personally I would modify the Declare of the AddIPAddress to take a ByVal Long for  Address and IPMask.  

Build the IP address and mask in instances of tIPBytes then copymemory them to longs which can then be used in Calling AddIPAddress.  Don't use the xor described above.

E.g.;

Declare Sub AddIPAddressLib "Iphlpapi.dll" (ByVal Address as Long, _
  ByVal IpMask as Long, _
  ByVal IfIndex as Long, _
  ByRef NTEContext as Long, _
  ByRef NTEInstance as Long)

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Type tIPBytes
  b1 byte
  b2 byte
  b3 byte
  b4 byte
End Type

Private Function AddAddr(ByVal vlngAdapter as Long )

Dim tIPAddr as tIPBytes
Dim tIPMask as tIPBytes

Dim lngIPAddr as Long
Dim lngIPMask as Long
Dim lngNTEContext as Long
Dim lngNTEInstance as Long

tIPAddr.b1 = 100
tIPAddr.b2 = 1
tIPAddr.b3 = 168
tIPAddr.b4 = 192

tIPMask.b1 = 0
tIPMask.b2 = 255
tIPMask.b3 = 255
tIPMask.b4 = 255

CopyMemory lngIPAddr, tIPAddr, 4
CopyMemoty lngIPMask, tIPMask, 4

AddIPAddress lngIPAddr, lngIPMask, vlngAdapter , lngNTEContext, lngNTEInstance

End Function
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now