OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Blogs > shelbym

Rating: 13 votes, 5.00 average.

Macros and CorelDRAW X6 (64-Bit)

Posted 25-03-2012 at 18:27 by shelbym

Introduction

The good news in most macros from previous versions of CorelDRAW should work great. The one exception to this are macros that use WinAPI calls. All previous macros that use WinAPI calls will need to be updated for CorelDRAW X6 (64-bit). This is only for the 64-bit version, if you are using the 32-bit version of CorelDRAW, even on a 64-bit version of Windows everything should run as expected.

Why the Change

VBA has used the Long data type to represent pointer values in WinAPI calls. The Long data type is a 32-bit Integer, on a 64-bit platform the pointers are 64-bit values. As they say, you cannot put a 64-bit value in a 32-bit bucket, it will not fit. VBA needs to pass 64-bit values to the parameters and return values, not 32-bit as in the past. If VBA where passed a 64-bit value it would be truncated to a 32-bit value when making the WinAPI call and most likely lead to a crash of the host application, in our case CorelDRAW.

The Solution

Microsoft has added support for 64-bit by creating the new data type, LongPtr in VBA 7. The LongPtr data type will work correctly on both 32 and 64-bit systems as it uses a 32-bit pointer size for 32-bit platforms and a 64-bit pointer size for 64-bit platforms, pretty smart huh.

VBA needs to know that you have reviewed the WinAPI calls and made the correct changes to the new LongPtr data type, so Microsoft added the keyword PtrSafe to VBA to add after the Declare statement. This way VBA know you have reviewed the call and made it safe. Without this keyword any function / subroutine with Declare will fail to compile.
Examples

Here is an example of the changes that will need to be made.
‘API definition the old way
Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long

‘API definition the new way
Private Declare PtrSafe Function GetDC Lib "USER32" (ByVal hWnd As LongPtr) As LongPtr
I have bolded and underlined the changes to make them easy to see. We have changed the Long data type to LongPtr, and have said that WinAPI call is now safe by using the PtrSafe keyword.

Let’s look at another example:
‘API definition the old way
Declare Function GetKeyState Lib "USER32" (ByVal vKey As Long) As Integer

‘API definition the new way
Declare PtrSafe Function GetKeyState Lib "USER32" (ByVal vKey As Long) As Integer
Notice anything different? Here we only used the keyword PtrSafe but did not change the Long data type to LongPtr. This is because we only need to use the LongPtr data type if the function arguments or return values that are memory addresses, if they represent data you should still use the Long data type.

Here is an example that uses both data types:
‘API definition the old way
Public Declare Function SendMessageA Lib "user32" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

‘API definition the new way
Public Declare PtrSafe Function SendMessageA Lib "user32" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPtr
If you would like some examples of common WinAPI calls and the new syntax you can use the following links:

Declaring API Functions In 64 Bit Office
Office 2010 Help Files: Win32API_PtrSafe with 64-bit Support

Supporting Older Version

This all works great for CorelDRAW X6 and VB7, but does not work with older versions, this is because older versions of VBA to not understand the new PtrSafe keyword and LongPtr data type. If you want your macro to support older version we need to add an IF Else Statement. Ah, yes, one Macro to rule them all.

Here is an example:
#If VBA7 Then
Private Declare PtrSafe Function GetDC Lib "USER32" (ByVal hWnd As LongPtr) As LongPtr
#Else
Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long
#End If
Summary

Again, I want to point out that this change only affects the few macros that use WinAPI calls, and with a few simple changes you will be up and running on any version of CorelDRAW. If you have questions or need further examples please feel free to post on the forums.
Posted in VBA
Views 19890 Comments 1
« Prev     Main     Next »
Total Comments 1

Comments

  1. Old Comment

    Still struggling...

    Thanks for the useful post. I've been trying to fix the wOxxOm tools package using the various bits of info I've found about 64-bit, but i'm not very good with VBA (understatement!)

    I've put in all the PtrSafe statements but whenever I run and of the Macros in the package I get a compile error and the word StrPtr is highlighted in the following line under "Function VGDllString$(ByVal nID&)"

    i = LoadStringW(hVGdll, nID, StrPtr(VGDllString), MAXBUF)

    I would be forever grateful if you have any ideas on how this could be fixed. I'm even happy to buy a new version of the Macros if they can be fixed as I am really struggling without them. Thanks.
    Posted 03-04-2012 at 11:23 by scarrott
 
Total Trackbacks 0

Trackbacks


All times are GMT -5. The time now is 05:07.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com