Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Win32 API structures with fixed length arrays in VB.NET

59 views
Skip to first unread message

Alexandre Brousseau

unread,
Feb 25, 2002, 8:53:36 PM2/25/02
to
Hello,

I'm trying to use some API functions with VB.NET and I've hit a wall on
this issue. Back in VB6 you would declare a type, in which you would include
a fixed length string (var as String * 50) and it would work fine, provided
you called the widestring version........

Types are gone, and they have been replaced with strucutres. For some
reason, I can't declare a fixed length member in a structure. The doc says
all arrays are now dynamic, so I'm a bit confused as to how I can work with
all this.

My current goal is to create and fill a LOGFONT structure using common
dialog FontDialog and send this LOGFONT to a C++ program that is plugged to
my VB.NET DLL...

Here's what I got for now :

Structure LOGFONT
Public lfHeight As Long
Public lfWidth As Long
Public lfEscapement As Long
Public lfOrientation As Long
Public lfWeight As Long
Public lfItalic As Byte
Public lfUnderline As Byte
Public lfStrikeOut As Byte
Public lfCharSet As Byte
Public lfOutPrecision As Byte
Public lfClipPrecision As Byte
Public lfQuality As Byte
Public lfPitchAndFamily As Byte
Public lfFaceName As String
End Structure

Of course the lfFaceName is causing a problem cause the
FontDialog.Font.ToLogFont function is receiving a pointer to a string,
rather than the string itself. I've tried all combinaisons, and looked
everywhere and I still can't figure out how I can first declare a LOGFONT
structure, and then fill it using the common dialog...

Any ideas?

Thanks alot!

BTW: Using .NET RC1 under Windows 2000 sp2...

Alex.


Peter Aragon

unread,
Feb 26, 2002, 3:35:24 AM2/26/02
to
Take a look at the following help-snippet from MSDN:

Structure may require marshalling attributes to be passed as an argument in
this Declare statementSee Also
Applying Attributes
In Visual Basic 6.0, user-defined types could be passed as an argument in a
Declare statement for a Windows API.

In Visual Basic .NET, a structure (user-defined type) passed as an argument
in a Declare statement may require additional marshalling attributes in
order to be passed correctly to the external function or subroutine. In
particular, arrays and fixed-length strings may not function as expected
without these attributes.

The following example shows a structure before and after upgrade:

' Visual Basic 6.0 code
Type MyType
s As String * 100
End Type

' After upgrade to Visual Basic .NET
Structure MyType
<VBFixedString(100),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=100)> Public s As String
End Structure
What to do next

Add an Imports statement to reference the InteropServices namespace:
Imports System.Runtime.InteropServices
Modify the structure and the string declaration to include marshalling
attributes, as in the following example:
' Modified code
<StructLayout( LayoutKind.Sequential, CharSet:=CharSet.ANSI)>Structure
MyType
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=100)> Public s As String
End Structure


Good luck,
Peter Aragon

"Alexandre Brousseau" <bro...@hotmail.com> wrote in message
news:eUMS#gmvBHA.2136@tkmsftngp04...

0 new messages