Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1313
  • Last Modified:

C --->Sizeof (Structure) ... the Equivalent in VB for Sizeof(MyType)???

Hello there ...
In C you can get the sizeof a Structure ( A Type in Visual Basic)

How can I get the Sizeof a Type in Visual Basic ??
OHDev
0
OHDev2004
Asked:
OHDev2004
  • 7
  • 4
  • 2
1 Solution
 
OHDev2004Author Commented:
Im Calling A Function from a C DLL ... so one of the Parameters needs to get the size of a Structure (In our case a Type in VB)

So let's say I have the Following Type:

Public Type MyType1
 Name as string
 Age As Integer
 Email as string
End Type


then when i call the Function it should be called like:

Dim Ret as Long
Dim NewPerson as MyType1
Dim ID as Integer,Buffer as Long

ID=5
Buffer = [ Here i should get the size of the Structure    'in C it was SizeOf(MyType1)]
Ret= MyFunction(MyType1,Buffersize,ID)

PS:Sorry for the Low Points :(
Thank you ...
0
 
Erick37Commented:
Use:

LenB(NewPerson) to return the size in bytes of the structure
0
 
vb_elmarCommented:
@OHDev2004

first write customer no.5's  name to Structure:
===============================================
BufferLength = writeMyCustomer(Name,Age,eMail,id)

then print his Data (Name,Age,eMail,id) to Form1:
======================================================
Call Print_DATA_of_user(5)

Try this code:


'Form1:
'=======
Private Type MyType1
      Name As String * 19
      Age As Integer
      eMail As String * 19
      id As Integer 'user identity number as integer
End Type
Dim Ret As Integer '   2 Bytes
Dim NewPerson(0 To 100) As MyType1, NewPersonData As MyType1
Private Sub Form_Activate(): AutoRedraw = -1: DoEvents
Move 0, 0: FontSize = 16
'NewPerson.Age = 23 '2 Byte Integer
'NewPerson.eMail = "user@xyz.com'String *19  ---> 123456789@123456789"
'NewPerson.ID = 999 '   Integer
'NewPerson.Name = "Bill Gates" ' String *19  ---> 123456789 123456789

'ID = 5
Ret = writeMyCustomer("Bill Gates", 23, "user@xyz.com", 5)
Print "Buffer Length  :  "; Ret: Print

Call Print_DATA_of_user(5)
End Sub
Private Sub Print_DATA_of_user(ByVal id As Integer)
Print "Id : "; id
Print "Age : "; NewPerson(id).Age
Print "Mail : "; NewPerson(id).eMail
Print "Name : "; NewPerson(id).Name
End Sub
Private Function writeMyCustomer(ByVal custName As String, _
                                 ByVal Age2 As Integer, _
                                 ByVal eMail2 As String, _
                                 ByVal id2 As Integer _
                                 ) As Integer
NewPerson(id2).Age = Age2
NewPerson(id2).eMail = eMail2
NewPerson(id2).Name = custName
writeMyCustomer = Len(NewPersonData)
End Function
0
Independent Software Vendors: 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!

 
OHDev2004Author Commented:
Thank you two for Commenting ...

I want to get the size of the Type it self  not of the Variable ...
Like
LenB(MyType1)

Not
LenB(NewPerson)
Thank you :)
OHDev
0
 
OHDev2004Author Commented:
and by the way ...
when i test ... I have a complicated Type ...
and when I get it using Len or LenB i get a 28 which is Impossible ... because it's A Large Type and it even has a Member Called Heap which is declared as Heap(1024) As Byte ...

So the Returned Size is not valid ...
OHDev
0
 
Erick37Commented:
Hi OHDev:

VB does not have a direct equivalent of SizeOf, instead you must use Len() or LenB() on a variable of the type you are interested in.

So if you declared the type as:

Private Type MyType1
    Name As String * 80
    Age As Integer
    Email As String * 60
End Type

Then Len(NewPerson) = 142 '80 characters + 2 bytes Integer + 60 characters
and LenB(NewPerson) = 282 '160 bytes in an 80 char string + 2 bytes Integer + 120 bytes in a 60 char string
0
 
Erick37Commented:
Can you post the Type here?
0
 
vb_elmarCommented:
>>> I want to get the size of the Type it self  LenB(MyType1) Not LenB(NewPerson)

OHDev, that's not possible in VB. You must declare a type X as "MyType1",
ant then you can ask, how long X is .


0
 
OHDev2004Author Commented:
Public Enum ConnectionID_Device_t
 STATIC_ID = 0
 DYNAMIC_ID = 1
End Enum

Public Type ConnectionID_t
 CallID As Long
 DeviceID As String * 64
 DevIDType As ConnectionID_Device_t
End Type


Public Type CSTAMakeCallConfEvent_t
 newCall As ConnectionID_t
End Type
 
Public Type CSTAConfirmationEvent
 invokeID As Long
 makeCall As CSTAMakeCallConfEvent_t
End Type
 
Public Type ACSEventHeader_t
 acsHandle As Long
 eventClass As Integer
 eventType As Integer
End Type
 
Public Type CSTAEvent_t
 eventHeader As ACSEventHeader_t
 cstaConfirmation As CSTAConfirmationEvent
 heap(1024) As Byte
End Type


Then I declare a Variable as Dim MyVariable as CSTAEvent_t

OHDev
0
 
OHDev2004Author Commented:
By the way it returns 1170 ...


I have a Small Question ...

I've Converted the Following from C:
typedef char            DeviceID_t[64];
typedef struct ConnectionID_t {
    long            callID;
    DeviceID_t      deviceID;
    ConnectionID_Device_t devIDType;
} ConnectionID_t;

is this :

Public Type ConnectionID_t
 CallID As Long
 DeviceID As String * 64
 DevIDType As ConnectionID_Device_t
End Type

considering the DeviceID part did i convert it right ??
OHDev
0
 
OHDev2004Author Commented:
Ok :) I fixed the Bug :)

i changed it to  DeviceID (64) as Byte

and All was Cool :)

Since Erick37's Answer was the good one because I needed the Binary Length so He will get the Points :)
Thank you too very Much :)
Again Erick ur a Life saver :P
OHDev
0
 
Erick37Commented:
Hi OHDev,

Thanks for the points, glad it worked.

One note regarding arrays:

In C
DeviceID_t[64];
means 64 elements, 0 to 63

In VB
DeviceID(64) as Byte
means 65 elements, 0 to 64

So to duplicate the structure exactly you might want to declare DeviceID(63) so that it has 64 elements as does the C prototype.
0
 
OHDev2004Author Commented:
:) Thank you very Much for Pointing me to the right :) ...
But Im glad it worked anyway :)

Working with Old C DLLs is really really Discusting :(....LOL
thank you again ...
OHDev
0

Featured Post

Technology Partners: 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!

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