Defined a Fixed Length String in a Structure

  • Thread starter Thread starter Scott Lemen
  • Start date Start date
S

Scott Lemen

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
 
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,

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
 
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 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top