Structure for API call

J

Jack Russell

I do not need this now but it did make me wonder

In VB 6 if I define the following structure

Public Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type

It creates a structure which is 148? bytes long

However in vb.net

Public Type OSVERSIONINFO
dwOSVersionInfoSize As integer
dwMajorVersion As integer
dwMinorVersion As integer
dwBuildNumber As integer
dwPlatformId As integer
szCSDVersion As String' ' Maintenance string for PSS usage
End Type

blah.szcsdversion=space(128)

creates a structure which is 24 bytes long. How does one create the
structure needed for the API call (or can't you)?
 
C

Crouchie1998

This is the correct format according to Microsoft Development UK:

<StructLayout(LayoutKind.Sequential)> Public Structure OsVersionInfoEx
Public dwOSVersionInfoSize As Integer
Public dwMajorVersion As Integer
Public dwMinorVersion As Integer
Public dwBuildNumber As Integer
Public dwPlatformId As Integer
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> Public
szCSDVersion As Char()
Public wServicePackMajor As Short
Public wServicePackMinor As Short
Public wSuiteMask As Short
Public wProductType As Byte
Public wReserved As Byte
End Structure

Crouchie1998
BA (HONS) MCP MCSE
 
T

Tom Shelton

This is the correct format according to Microsoft Development UK:

I sould change this:
<StructLayout(LayoutKind.Sequential)> Public Structure OsVersionInfoEx
To:


Public dwOSVersionInfoSize As Integer
Public dwMajorVersion As Integer
Public dwMinorVersion As Integer
Public dwBuildNumber As Integer
Public dwPlatformId As Integer

And this:
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> Public
szCSDVersion As Char()
To:

Public wServicePackMajor As Short
Public wServicePackMinor As Short
Public wSuiteMask As Short
Public wProductType As Byte
Public wReserved As Byte
End Structure

Makes it a little easier, IMHO.
 
C

Crouchie1998

But you are going against a Microsoft programmer if you change it. I don't
mean to be rude, but I would trust them over you.

Crouchie1998
BA (HONS) MCP MCSE
 
H

Herfried K. Wagner [MVP]

Crouchie1998 said:
But you are going against a Microsoft programmer if you change it. I don't
mean to be rude, but I would trust them over you.

I'd check which solution is the better one and use the better solution.

| And this:
| > <MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> Public
| > szCSDVersion As Char()
|
| To:
| <MarshalAs (UnmanagedType.ByValTStr, SizeConst:=128)> _
| Public szCSDVersion As String

An array of 'TCHAR' is an array of Unicode characters on Unicode systems and
a null-terminated a array of ANSI characters on ANSI systems. That's
exactly what 'ByValTStr' is designed for, and as a benefit will allow you to
treat the member as a string. Although 'ByValArray' will work on both
Unicode and ANSI systems too, I suggest to use a 'ByValTStr' in this case
because it's the more "natural" choice.

Just my 2 Euro cents...
 
T

Tom Shelton

I'd check which solution is the better one and use the better solution.

| And this:
| > <MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> Public
| > szCSDVersion As Char()
|
| To:
| <MarshalAs (UnmanagedType.ByValTStr, SizeConst:=128)> _
| Public szCSDVersion As String

An array of 'TCHAR' is an array of Unicode characters on Unicode systems and
a null-terminated a array of ANSI characters on ANSI systems. That's
exactly what 'ByValTStr' is designed for, and as a benefit will allow you to
treat the member as a string. Although 'ByValArray' will work on both
Unicode and ANSI systems too, I suggest to use a 'ByValTStr' in this case
because it's the more "natural" choice.

Just my 2 Euro cents...

I agree... :)
 
C

Crouchie1998

But you're still going against what the Microsoft coders recommend though.

But who to believe? A person who has all the Microsoft qualifications &
trainining from the Microsoft Development team or a person who has answered
a year of newsgroup posts to only gain a MVP?

That's my 2p worth (We use £/p in the UK not cents)

Crouchie1998
BA (HONS) MCP MCSE
 
T

Tom Shelton

But you're still going against what the Microsoft coders recommend though.

Microsoft example code is not always the best.
But who to believe? A person who has all the Microsoft qualifications &
trainining from the Microsoft Development team or a person who has answered
a year of newsgroup posts to only gain a MVP?

You seem to be a bit testy. If you feel I was trying to say that your
answer was wrong, I'm sorry - because I wasn't. I was just trying to
show what I feel was an improved version.

The fact is that the member of the structure in question is a text
field. It is much more natural to access this member as a string then
an array of characters... What would you prefer?:

Console.WriteLine (osVersion.szCSDVersion)

Or

Console.WriteLine (New String (osVersion.szCSDVersion))

Both methods work. Both methods produce the same results. But, one is
a little more natural then the other... Wouldn't you agree?

And by the way, I've been on the news groups for much more then a year.
 

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

Top