Moving from VB6 - API Long now Integer?

J

Jerry West

I am moving from VB6 to VB .NET so please bear with me. Should I change all
of my API declarations in VB6 that declared Longs to Integers? For example
in VB6:

Declare Function GetClientRect Lib "user32" (ByVal hWnd as Long, ByVal
lpRect as RECT) As Long

Should this now read:

Declare Function GetClientRect Lib "user32" (ByVal hWnd as Integer, ByVal
lpRect as RECT) As Integer

Thanks,

JW
 
A

Armin Zingler

Jerry West said:
I am moving from VB6 to VB .NET so please bear with me. Should I
change all of my API declarations in VB6 that declared Longs to
Integers? For example in VB6:

Yes, the type of 32-bit values was "Long" in VB6 and is "Integer" (or Int32,
which is exactly the same) in VB.net. "Long" in VB.Net is 64 bits.
Declare Function GetClientRect Lib "user32" (ByVal hWnd as Long,
ByVal lpRect as RECT) As Long

Should this now read:

Declare Function GetClientRect Lib "user32" (ByVal hWnd as Integer,
ByVal lpRect as RECT) As Integer

Declare Function GetClientRect Lib "user32" (ByVal hWnd as IntPtr, ByRef
lpRect as RECT) As Boolean

- Handles should be declared as InPtr
- BOOL values (see documentation) can be declared as Boolean.
- In VB6, if you don't specify ByVal/ByRef, the default was ByRef. In
VB.Net, it is ByVal. So, if you take the declaration from the old API viewer
which did not include "ByRef", and you paste it in VB.Net, the declaration
will be completed by "ByVal", which is wrong in this case.

If you use the upgrade wizard (open the VB6 project in VB.Net), it
automatically changes Integer to Long, and missing ByVal/ByRef is correctly
completed by "ByRef".


Armin
 
J

Jerry West

So should anything declared as Integer in VB6 be declared as a Short on
..NET?

JW
 
H

Herfried K. Wagner [MVP]

Jerry West said:
So should anything declared as Integer in VB6 be declared as a Short on
.NET?

Not necessarily anything. It really depends on the types used in the C
function prototype.
 
A

Armin Zingler

Jerry West said:
So should anything declared as Integer in VB6 be declared as a Short
on .NET?

For API declarations this is correct. 16 bit integers are called Short now.

But in general, if you need to declare an integer variable and if it's size
is not predetermined externally, you should use the Integer (=Int32) data
type because it's considered being the native Integer data type size, just
like it was common practice to use Long in VB6 for the same reason.
(Though, I don't know if this is still a common rule with 64-bit processors
and 64-it OSes now. Anybody knows?)


Armin
 

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