Rabu, 27 Agustus 2014

RE: [MS_AccessPros] Conditional API declaration

 

Hi Crystal

I changed the subject :)

The main point is that code for 64-bit VBA must include the PtrSafe qualifier in the Declare statement.  This is not required for 32-bit VBA, but its absence will prevent compilation on 64-bit.  It is, however, understood by both bit-ness of VBA7, so should be included in all Declares for VBA7 onward. (VBA7 is currently the latest version).

However, it is not understood on versions pre-VBA7 (pre-Office 2007) and will cause a compiler error.  This is why we need to include the #Else block with the old format of the declaration, so that the code will work in Access 2003 and earlier.  I guess that as pre-2007 versions of Access disappear, the need for the conditional Declare will also largely disappear, but I don't see that happening soon.  (Look at Tom Wickerath and Boeing, for example!)

By the way, PtrSafe doesn't actually do anything!  It's just a way to make sure that the programmer has read the Terms & Conditions and at least made an attempt to ensure the code is 64-bit compatible :-)

The other main difference is in variables which are pointers (i.e. addresses of locations in memory).  32-bit code addresses memory with 32-bit pointers, which can be stored in a Long variable, but 64-bit code uses 64-bit pointers which obviously don't fit.  VBA has therefore introduced a new data type, LongPtr, which is a Long in a 32-bit code base, and a new 64-bit integer data type LongLong in 64-bit code.  Using Long instead of LongPtr will work fine in 32-bit, and will compile OK in 64-bit but will crash and burn at runtime because the API will be reading and writing 64-bit values where only 32 bits have been allocated for storage.

A very few API functions have versions that actually use 64-bit integers (not pointers) and for these the arguments need to be declared as LongLong.  VBA does not understand LongLong in 32 bit, so there is another compiler constant Win64 which is True if the code is running in 64-bit mode.  I have seen code where programmers have used #If Win64 instead of #If VBA7 for the two types of declaration.  While this will work OK, it is not technically correct, because VBA7 is designed to understand both types of pointer.  Note that Win64 indicates the bit-ness of the office application code, not the bit-ness of the Windows operating system it is running on (as is stated in some documentation).

Hope this clarifies things a little :-)

Cheers,
Graham

From: MS_Access_Professionals@yahoogroups.com [mailto:MS_Access_Professionals@yahoogroups.com]
Sent: Thursday, 28 August 2014 00:12
To: MS_Access_Professionals@yahoogroups.com
Subject: Re: [MS_AccessPros] widths and heights

 

 

Hi Graham,  right away, I see this:

#If VBA7 Then
   ' use LongPtr
#Else
   'As Long
#End if

what are other changes that you make?

thanks!

 

 

Warm Regards,

Crystal

 

Contacts database in Access

Contacts_070604 ... being updated ~ hopefully post something new in a few months

__._,_.___

Posted by: "Graham Mandeno" <graham@mandeno.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (1)

.

__,_._,___

Tidak ada komentar:

Posting Komentar