473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Structure for API call

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)?
Nov 21 '05 #1
8 1660
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
Nov 21 '05 #2
On 2005-05-26, Crouchie1998 <cr**********@spamcop.net> wrote:
This is the correct format according to Microsoft Development UK:

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

<StructLayout (LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure OsVersionInfoEx
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:
<MarshalAs (UnmanagedType.ByValTStr, SizeConst:=128)> _
Public szCSDVersion As String
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.
--
Tom Shelton [MVP]
Nov 21 '05 #3
Thanks!
Crouchie1998 wrote:
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

Nov 21 '05 #4
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
Nov 21 '05 #5
"Crouchie1998" <cr**********@spamcop.net> schrieb:
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...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6
In article <OJ**************@TK2MSFTNGP10.phx.gbl>, Herfried K. Wagner [MVP] wrote:
"Crouchie1998" <cr**********@spamcop.net> schrieb:
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...


I agree... :)

--
Tom Shelton [MVP]
Nov 21 '05 #7
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
Nov 21 '05 #8
On 2005-05-27, Crouchie1998 <cr**********@spamcop.net> wrote:
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.

--
Tom Shelton [MVP]
Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
4
by: Scott Lemen | last post by:
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...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
1
by: Falko Wagner | last post by:
Hi there, I am currently translating a VB 6.0 application to .NET and have the following problem: The data structure I need to pass to a DLL function call has a structure variable inside its...
2
by: David | last post by:
Hi all, using 1.1 I am creating a dynamic menu structure for my site, however, I may refer to the structure many times within a page. This would be fine if I was doing it all in the...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
0
by: rocckky | last post by:
Hi, i am new to .NET framework so not that knowledgable about managed and unmanaged codes. The problem i am facing is i need to call a function say fun_test(struct test * A) which is present in...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.