Link to home
Start Free TrialLog in
Avatar of Amro Osama
Amro OsamaFlag for Egypt

asked on

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
Avatar of Amro Osama
Amro Osama
Flag of Egypt image

ASKER

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 ...
Use:

LenB(NewPerson) to return the size in bytes of the structure
@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
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
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
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you post the Type here?
>>> 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 .


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