By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,186 Members | 1,530 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,186 IT Pros & Developers. It's quick & easy.

Fixed Length Strigs

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.