473,320 Members | 1,926 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,320 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 23740
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.