Fixed Length Strigs

K

Ken Kast

I'm using VBFixedString to create fixed length strings.
But I find that the string will merrily allow a string of
greater length to be assigned to it. Is this the way
things are supposed to work?

I've got a more basic question. The reason I'm doing all
this is so I can pass a string buffer to an unmanaged
method in a dll. It's assuming 1 byte/char. Does that
mean that my fixed length should be half the size the
buffer is supposed to be?

Thanks.

Ken
 
B

Brian

You can use the StringBuilder class to send a fixed string to a dll. Use the
(capacity +1) when you pass it to your dll.

Dim sb as New StringBuilder(255)
 
A

Alexandre Moura

Two things - I'm not sure (It's been a long time since I've played with it)
but I believe the vbfixedstring applies to the vb runtime, not marshalling
to unmanaged code - you probably should be using the MarshalAs attribute
under system.runtime.interopservices:
<System.runtime.interopservices.marshalas(Runtime.InteropServices.UnmanagedT
ype.ByValTStr, sizeconst:=5)> public s As String
note that this will only affect the marshalling - while inside .net there's
no restriction on string size - that said, you should instantiate it with a
string with the size you intend to marshall - while extra characters will
be ignored by the marshaller, missing characters will cause errors.

Second, what you need to do is to make sure the string is being marshalled
as an ansi string instead of an unicode string - you may need to specify
the structlayoutattribute on the structure that has this string (you may
need to use a structure, yes - not sure if it will work on a class) - It's
possible if you're using declare that specifying Ansi will do the trick:
Declare Ansi Sub mysub Lib "nolib.dll" (ByVal arg1 As class1)
but I haven't tried it. Check the help for the marshalas attribute for more
info.

Good luck
Alex
 
T

Tom Shelton

I'm using VBFixedString to create fixed length strings.
But I find that the string will merrily allow a string of
greater length to be assigned to it. Is this the way
things are supposed to work?

I've got a more basic question. The reason I'm doing all
this is so I can pass a string buffer to an unmanaged
method in a dll. It's assuming 1 byte/char. Does that
mean that my fixed length should be half the size the
buffer is supposed to be?

Thanks.

Ken

If it is a one byte char to an unmanaged api, then you probably want to
do some thing like:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Structure Struct
Public TheChar As Char
End Struct

Declare Ansi Function MyFunc Lib "MyLib.Dll" _
(ByRef TheStruct As Struct) As Integer

For fixed lenght strings you'll want to use
MarshalAs(UnmanagedType.ByValTStr, SizeConst:=YourSize)
 

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