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]