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

Defined a Fixed Length String in a Structure

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


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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.