Convert this C structure to a VB type

Posted on 2005-03-25
Medium Priority
Last Modified: 2010-08-05
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.
Question by:DrDamnit
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
  • 2
LVL 32

Expert Comment

ID: 13633434
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


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


'4 bytes
Private Type IPAddr
  S_B1 as Byte
  S_B2 as Byte
  S_B3 as Byte
  S_B4 as Byte
End Type
LVL 32

Expert Comment

ID: 13633508
But, you can always use CopyMemory to simulate a Union when you need to access the variables as different data types.


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)
    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

Expert Comment

ID: 13633512
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


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.


Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 32

Author Comment

ID: 13633549
LVL 32

Expert Comment

ID: 13633603
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.
LVL 32

Accepted Solution

Erick37 earned 2000 total points
ID: 13633699
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


Expert Comment

ID: 13633704
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.


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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

771 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