473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1487
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " ...
2
by: coz | last post by:
I created a wrapper class for a dll written in C. Will the following code prevent the "ENTIRE ARRAY" from being moved, not just the first array element? The Wrapper class will be instantiated in...
5
by: David Garamond | last post by:
The MySQL manual recommends that we create a "fixed-length row" if possible, for speed (especially scanning speed). A fixed-length row is a row which is comprised of only fixed-length fields. A...
2
by: Frank Swarbrick | last post by:
I'm just learning about embedded SQL, so be gentle... My basic question is, if I use a fixed length host variable for a column defined as VARCHAR, will trailing spaces be removed (or not) upon...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
13
by: Neil Cerutti | last post by:
Many of the file formats I have to work with are so-called fixed-format records, where every line in the file is a record, and every field in a record takes up a specific amount of space. For...
1
by: kendrick82 | last post by:
Hi, I would like to seek some advise and assistance regarding the following matter as I am new to VB.Net. I'll appreciate any helps render. I am developing a VB application using VB.Net 2003 to...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
2
by: Edwin.Madari | last post by:
#your thought is right. ======================================================= def sizes2fields(sizes): d = begin = 0 for i in sizes: if begin: end = begin + i else: end = i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.