You are not logged in.

A1880

Intermediate

  • "A1880" is male
  • "A1880" started this thread

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

1

Monday, July 27th 2009, 1:19pm

Len for Variables

Hi,
in VB6 the Len() function returns the length of a string and the number of bytes used for a variable.
That is useful if you want to estimate memory consumption or have to pass a structure of dynamic size to an external API.

The following yields "0 14" in VB6:

Source code

1
2
3
4
5
6
7
8
9
10
11
Private Type myType
    a As Integer
    s As String
    d As Double
End Type

Private Sub Command1_Click()
    Dim mt As myType
        
    MsgBox Len(myType) & " " & Len(mt)
End Sub


Jabaco does not compile this code. It expects only string arguments and thus complaints that conversion to String is not supported.

Basically, Len() acts similar to "sizeof()" in C/C++.
In Java, it takes some fiddling around to actually get the size of an object.
Jabaco's compiler might be able to know the size of some variables at compile time.

Greetings!

A1880

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

2

Monday, July 27th 2009, 2:38pm

sizeof = LenB

Hi,

first: due to VB6, when working with UD-Types and API-functions, never use Len, but use LenB instead.

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
Private Type myType
    a As Integer
    s As String
    d As Double
End Type
Private Sub Command1_Click()
    Dim mt As myType
     
    'MsgBox Len(myType) & " " & Len(mt)
    MsgBox Len(mt) & " " & LenB(mt)
    
End Sub


As you may know, the reason for that is: padding bytes

but you got to keep calm, normally all UD-Types concerning the win-API have a 4-byte alignment.

by the way: additional tipp: VB is able to deal with 1,2 and 4-byte-alignment, so packed records are also possible in VB, just use a typelibrary for your udtypes. In VB-modules only 4-byte aligned udtype-records will be produced by VB itself. Google search: "ActiveVB UDTypeAlignment.zip"



second: it's only for completeness reasons: there was a similar thread to this questions started by StefanSchnell :
Size of a structure
he also posted a solution for that kind of problems, togehter with his VarPtr.dll



third: All of this is not the answer to the JNA-questions. But we got to ask Manuel how the native-keyword is meant to be.

@Manuel, ho is the new native-keyword in conjunction with the new class-property ment to be used?
Is it maybe the way or even half the way to JNA?


regards
OlimilO

Rate this thread
WoltLab Burning Board