Convert this fixed string VB6 code in VB.NET and win ...

G

Guest

.... my eternal gratitude!!! :p

Here is the problem. A sample of my original VB6 code :

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Type INFO
name(1 To 3) As String * 41
address As String * 41
End Typ
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

I used to be able to get the length of any element in my type with the
following code:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as string = Len(INFO.address)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

The value of temp would then be 41


With my new VB.NET code, I try to use the following code, but whatever I
try, the value of temp is always 0
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Structure INFO
<VBFixedString(41)> public name(1 To 28) As String
<VBFixedString(41)> public address As String
End Structure
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as integer = Microsoft.VisualBasic.Len(INFO.address)
or
dim temp as integer =
System.Runtime.InteropServices.Marshal.SizeOf(INFO.address)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

In the latter case, the return value is a null and my application crashes.


Any input on this would be extremely appreciated.

Thank you.
 
K

Ken Tucker [MVP]

Hi,

From the note about the vbfixedlength string. Keep in mind that
this attribute does not change the actual length of the string itself.

http://msdn.microsoft.com/library/d...us/vblr7/html/valrfvbfixedstringattribute.asp

Ken
-----------------------
.... my eternal gratitude!!! :p

Here is the problem. A sample of my original VB6 code :

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Type INFO
name(1 To 3) As String * 41
address As String * 41
End Type
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

I used to be able to get the length of any element in my type with the
following code:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as string = Len(INFO.address)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

The value of temp would then be 41


With my new VB.NET code, I try to use the following code, but whatever I
try, the value of temp is always 0.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Structure INFO
<VBFixedString(41)> public name(1 To 28) As String
<VBFixedString(41)> public address As String
End Structure
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as integer = Microsoft.VisualBasic.Len(INFO.address)
or
dim temp as integer =
System.Runtime.InteropServices.Marshal.SizeOf(INFO.address)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

In the latter case, the return value is a null and my application crashes.


Any input on this would be extremely appreciated.

Thank you.
 
A

Armin Zingler

Eric said:
... my eternal gratitude!!! :p

Here is the problem. A sample of my original VB6 code :

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Type INFO
name(1 To 3) As String * 41
address As String * 41
End Type
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

I used to be able to get the length of any element in my type with
the following code:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as string = Len(INFO.address)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

The value of temp would then be 41


With my new VB.NET code, I try to use the following code, but
whatever I try, the value of temp is always 0.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Structure INFO
<VBFixedString(41)> public name(1 To 28) As String
<VBFixedString(41)> public address As String
End Structure
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim INFO as INFO
dim temp as integer = Microsoft.VisualBasic.Len(INFO.address)
or
dim temp as integer =
System.Runtime.InteropServices.Marshal.SizeOf(INFO.address)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

In the latter case, the return value is a null and my application
crashes.


Any input on this would be extremely appreciated.

Thank you.



It might be possible to get the attribute using Reflection, but why do you
need this? The structure/string would be written to a file correctly as you
apply the vbfixedstring attribute. Where else do you need the length? If you
want to pass it to API functions, you'd have to add the MarshalAs-Attribute
on the 'address' element anyway:

<MarshalAs(UnmanagedType.ByValTStr, SizeConst := 41)> _
address As String * 41

For the first Element 'name', it's currently not supported to marshal an
array of fixed length strings. You'd have to, for example, replace it by an
array of chars (sizeconst = 123). Using VB file functions and referring to
the first VB6 declaration, this declaration should work:


Private Structure INFO
<VBFixedArray(2), VBFixedString(41)> _
Public name() As String
<VBFixedString(41)> _
Public address As String
End Structure


The Len function returns 164 as expected.


Armin
 
G

Guest

Thank you for your reply Armin, it is a lot clearer now.

However i still experience one ultimate problem. What if I were to want the
size of a structure containing another structure.

ie:

Public Structure TEST
PUblic Integ as Integer
End Structure

Public Structure INFO
Public Test as TEST
Public Str as Integer
End Structure

When I try Len(INFO) it give me the size of an Integer, ignoring the Test
element. Is there a special secret attribute for this situation?

Thnk you
 
A

Armin Zingler

Eric said:
Thank you for your reply Armin, it is a lot clearer now.

However i still experience one ultimate problem. What if I were to
want the size of a structure containing another structure.

ie:

Public Structure TEST
PUblic Integ as Integer
End Structure

Public Structure INFO
Public Test as TEST
Public Str as Integer
End Structure

When I try Len(INFO) it give me the size of an Integer, ignoring the
Test element. Is there a special secret attribute for this
situation?

Using your declarations above:

dim i as info
msgbox len(i)

I get "8" which is what I expect.

Armin
 
G

Guest

I made a mistake on my last post, the problem is that the structure is
ignored if it is a fixed array.

ie:

Public Structure TEST
Public Integ As Integer
End Structure

Public Structure INFO
<VBFixedArray(4)> Public Test() As Test
Public Str As Integer
End Structure

The result of Len(INFO) is 4, instead of 24.

Thank you
 
A

Armin Zingler

Eric said:
I made a mistake on my last post, the problem is that the structure is

ignored if it is a fixed array.

ie:

Public Structure TEST
Public Integ As Integer
End Structure

Public Structure INFO
<VBFixedArray(4)> Public Test() As Test
Public Str As Integer
End Structure

The result of Len(INFO) is 4, instead of 24.


Right, now I also get 4. Writing the structure to a file creates a 22 byte
sized file, so it's only the len function not returning the correct value.
But as you say it must be 24 bytes. This is probably because it's not
possible(?) to write a fixed array contained in a structure. The problem is
that you can specify "ArrayIsDynamic = False" if you write an array using
FilePut, but you can not specify this flag if you write a structure
containing an array. That's probably the reason why Len also returns the
"wrong" value - my guess only. There's a long explanation in the docs on the
Fileput function.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top