469,623 Members | 997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Fixed Length Strigs

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
Nov 20 '05 #1
3 1329
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)

"Ken Kast" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
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

Nov 20 '05 #2
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
Nov 20 '05 #3
On 2003-12-08, Ken Kast <an*******@discussions.microsoft.com> wrote:
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)
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Adrian Parker | last post: by
2 posts views Thread by coz | last post: by
5 posts views Thread by David Garamond | last post: by
2 posts views Thread by Frank Swarbrick | last post: by
13 posts views Thread by Neil Cerutti | last post: by
2 posts views Thread by Edwin.Madari | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.