473,388 Members | 1,073 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,388 software developers and data experts.

Defined a Fixed Length String in a Structure

Hi,

Some Win APIs expect a structure with a fixed length string. How is it
defined in VB .Net 2003?
When I try to use the FixedLengthString class I get an "Array bounds
cannot appear in type specifiers" error.

Thank you,

Scott
Imports Microsoft.VisualBasic.Compatibility.VB6

Module Module1

Public Structure OSVERSIONINFO
Dim dwOSVersionInfoSize As Long
Dim dwMajorVersion As Long
Dim dwMinorVersion As Long
Dim dwBuildNumber As Long
Dim dwPlatformId As Long
Dim szCSDVersion As FixedLengthString(128)

'^^^----- Array bounds cannot appear in type specifiers error
End Structure

End Module

Nov 21 '05 #1
4 23758
On Fri, 10 Sep 2004 21:17:38 -0700, Scott Lemen wrote:
Hi,

Some Win APIs expect a structure with a fixed length string. How is it
defined in VB .Net 2003?
When I try to use the FixedLengthString class I get an "Array bounds
cannot appear in type specifiers" error.

Thank you,

Scott
Imports Microsoft.VisualBasic.Compatibility.VB6

Module Module1

Public Structure OSVERSIONINFO
Dim dwOSVersionInfoSize As Long
Dim dwMajorVersion As Long
Dim dwMinorVersion As Long
Dim dwBuildNumber As Long
Dim dwPlatformId As Long
Dim szCSDVersion As FixedLengthString(128)

'^^^----- Array bounds cannot appear in type specifiers error
End Structure

End Module


Imports System.Runtime.InteropServices

<StructLayout (LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure OSVERSIONINFO
Public dwOSVersionInfoSize As Integer
Public dwMajorVersion As Integer
Public dwMinorVersion As Integer
Public dwBuildNumber As Integer
Public dwPlatformId As Integer

<MarshalAs (UnmanagedType.ByValTStr, SizeConst:=128)> _
Public szCSDVersion As String
End Structure

Public Declare Auto Functiong GetVersionEx Lib "kernel32" _
(ByRef lpVersionInfo As OSVERSIONINFO) As Boolean

A couple of things... In VB.NET data sizes have changed. So, when
converting API calls keep in mind the following:

VB6 VB.NET DataSize
Byte Byte 8-bit
Integer Short 16-bit
Long Integer 32-bit
Currency Long 64-bit

Also, notice the lack of an alias... Instead, use Auto when declaring
functions of the A/W type. Let the runtime choose the appropriate method.
Unlike VB6 - VB.NET can handle Unicode calls, so you save your self a lot
of overhead for string conversions if you make the W call on NT based
platforms... Think about it. If you use the A version of a call on NT
based systems, you end up with a sequence of conversions sort of like:

Unicode -> Ansi -> Unicode -> Ansi -> Unicode

The reason for this is because the A functions on NT, just convert the Ansi
string to unicode and call the W function :) So, you have VB.NET convert a
wide character string to ansi and passing it to the dll function, which
then immediately converts it back to unicode and calls the W function. On
the way back, the Ansi function converts it back to ansi, and then returns
it to the VB.NET marshaller that converts it back into unicode...

Anyway, you may want to read the P/Invoke sections of the documentation -
the cover many important topics such as default marshalling behavior,
marshalling attributes, and type conversions. Pay attention to the part
about strings. VB.NET's marshaller will let you pass strings as mutable
buffers to external api's, but it causes a great deal of overhead. So, a
good rule of thumb for passing buffers that need to be filled by the call,
is to use System.Text.StringBuilder instead (this isn't possilbe in
structures though). I personally, think it is a good habbit to get into -
even ignoring the performance/memory usage benifits - simply because if you
ever have to any interop using C#, your going to have to do it this way if
you want to receive your data. The C# marshaller is not quite as friendly
in this respect :)

--
Tom Shelton [MVP]
Nov 21 '05 #2
Tom,

In the dotnet general group is a long nice discussion which probably
interest you.

It is about C# compiling and now it becomes at the point of Mono.

Cor
Nov 21 '05 #3
On Sat, 11 Sep 2004 09:06:48 +0200, Cor Ligthert wrote:
Tom,

In the dotnet general group is a long nice discussion which probably
interest you.

It is about C# compiling and now it becomes at the point of Mono.

Cor


I just read through it.. Thanks for pointing it out. I need to get my
Gentoo Linux reinstalled, so I can try out Mono 1.0 in it's native
environment! Unfortuantely, I've been so busy with a couple of "extra"
projects on the side, that I have had very little time to play with mono
for the last few months (let alone hang out in the news groups)...
Fortunately, those are completed - so, I am reinstalling Gentoo Linux this
week (Yeah!), and you can expect to see a lot more of me around these parts
as well (for now anyway)...

--
Tom Shelton [MVP]
Nov 21 '05 #4
Tom and the VB .Net Group,

All I can say is WOW! I never would have figured out that technique for
passing fixed length strings to Windows APIs. Thank you for taking the time
to respond with such a complete way.

Thank you again,

Scott

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:v8*****************************@40tude.net...
On Fri, 10 Sep 2004 21:17:38 -0700, Scott Lemen wrote:
Hi,

Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003?
When I try to use the FixedLengthString class I get an "Array bounds
cannot appear in type specifiers" error.

Thank you,

Scott
Imports Microsoft.VisualBasic.Compatibility.VB6

Module Module1

Public Structure OSVERSIONINFO
Dim dwOSVersionInfoSize As Long
Dim dwMajorVersion As Long
Dim dwMinorVersion As Long
Dim dwBuildNumber As Long
Dim dwPlatformId As Long
Dim szCSDVersion As FixedLengthString(128)

'^^^----- Array bounds cannot appear in type specifiers error
End Structure

End Module
Imports System.Runtime.InteropServices

<StructLayout (LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure OSVERSIONINFO
Public dwOSVersionInfoSize As Integer
Public dwMajorVersion As Integer
Public dwMinorVersion As Integer
Public dwBuildNumber As Integer
Public dwPlatformId As Integer

<MarshalAs (UnmanagedType.ByValTStr, SizeConst:=128)> _
Public szCSDVersion As String
End Structure

Public Declare Auto Functiong GetVersionEx Lib "kernel32" _
(ByRef lpVersionInfo As OSVERSIONINFO) As Boolean

A couple of things... In VB.NET data sizes have changed. So, when
converting API calls keep in mind the following:

VB6 VB.NET DataSize
Byte Byte 8-bit
Integer Short 16-bit
Long Integer 32-bit
Currency Long 64-bit

Also, notice the lack of an alias... Instead, use Auto when declaring
functions of the A/W type. Let the runtime choose the appropriate method.
Unlike VB6 - VB.NET can handle Unicode calls, so you save your self a lot
of overhead for string conversions if you make the W call on NT based
platforms... Think about it. If you use the A version of a call on NT
based systems, you end up with a sequence of conversions sort of like:

Unicode -> Ansi -> Unicode -> Ansi -> Unicode

The reason for this is because the A functions on NT, just convert the

Ansi string to unicode and call the W function :) So, you have VB.NET convert a wide character string to ansi and passing it to the dll function, which
then immediately converts it back to unicode and calls the W function. On
the way back, the Ansi function converts it back to ansi, and then returns
it to the VB.NET marshaller that converts it back into unicode...

Anyway, you may want to read the P/Invoke sections of the documentation -
the cover many important topics such as default marshalling behavior,
marshalling attributes, and type conversions. Pay attention to the part
about strings. VB.NET's marshaller will let you pass strings as mutable
buffers to external api's, but it causes a great deal of overhead. So, a
good rule of thumb for passing buffers that need to be filled by the call,
is to use System.Text.StringBuilder instead (this isn't possilbe in
structures though). I personally, think it is a good habbit to get into -
even ignoring the performance/memory usage benifits - simply because if you ever have to any interop using C#, your going to have to do it this way if
you want to receive your data. The C# marshaller is not quite as friendly
in this respect :)

--
Tom Shelton [MVP]

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
3
by: ecov | last post by:
Is there any easy way to read into a dataset a file that was created from a BCP command. The file is in a fixed length format. I would also like to be able to write out a dataset to the same type...
1
by: active | last post by:
Private Structure TNavRec <VBFixedString(100),System.Runtime.InteropServices.MarshalAs(System.Runtime. InteropServices.UnmanagedType.ByValTStr,SizeConst:=100)> Public strItemName As String ...
3
by: Ken Kast | last post by:
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?...
6
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
0
by: Ken Varn | last post by:
I have a managed C++ assembly in which I need to interact with some 'C' APIs that take fixed size 'C' data blocks. I need to wrap these data blocks into a managed object. It seems like a lot of...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.