Adapting API functions to Office 64 (1 Viewer)

spikepl

Eledittingent Beliped
Local time
Today, 23:13
Joined
Nov 3, 2010
Messages
6,142
I have following code which works in Office 32:

Code:
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, _
  ByVal hdc As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _
  ByVal nIndex As Long) As Long

Const HWND_DESKTOP As Long = 0
Const LOGPIXELSX As Long = 88
Const LOGPIXELSY As Long = 90

'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As Long
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
  ReleaseDC HWND_DESKTOP, lngDC
End Function

'--------------------------------------------------
Function TwipsPerPixelY() As Single
'--------------------------------------------------
'Returns the height of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As Long
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
  ReleaseDC HWND_DESKTOP, lngDC
End Function
I need to make this available also for office 64 but my accesss to testing is very limited and also I'm not entirely sure what needs changing , other than adding PTRSAFE.

Some hints are here:

https://msdn.microsoft.com/en-us/library/office/ee691831(v=office.14).aspx

Many of the Long-variables need presumably changing into LongPtr

If you have any knowledge of /confidence in this, then please share.

TIA.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:13
Joined
Feb 19, 2013
Messages
16,668
I develop in 32 but on occasion deploy to 64 (although I need to compile on clients machine in 64 for accde)

The only thing I have ever had to do is add PtrSafe to the API declarations.

I have not had to change longs to longptrs, although I declare thing like

Const LOGPIXELSX As Long = 88

as simply

Const LOGPIXELSX = 88

so cannot confirm you might need to convert these (but you could try removing the 'As Long')
 

spikepl

Eledittingent Beliped
Local time
Today, 23:13
Joined
Nov 3, 2010
Messages
6,142
I made the mistake of reading the article I'm linking to. There it appears that handles and pointers need LongPtr.

The API stuff I've used so far did not have those, which is perhaps why I got away with PtrSafe only. Or perhaps not - this is just soooo far out of my area of interest :-(
 

jdraw

Super Moderator
Staff member
Local time
Today, 17:13
Joined
Jan 23, 2006
Messages
15,394
spike,

I have NOT had to do any of this but here is a link (with some embedded links) that may help.

Good luck.
 
Last edited:

spikepl

Eledittingent Beliped
Local time
Today, 23:13
Joined
Nov 3, 2010
Messages
6,142
Thanks jdraw. My issue is that I have little code to change, as shown in my post, but I have extremely limited possibilities for testing.
 

spikepl

Eledittingent Beliped
Local time
Today, 23:13
Joined
Nov 3, 2010
Messages
6,142
So here is the code after the transformation:

Code:
Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, _
  ByVal hdc As LongPtr) As Long
Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hdc As LongPtr, _
  ByVal nIndex As Long) As Long

Const HWND_DESKTOP As LongPtr = 0
Const LOGPIXELSX As Long = 88
Const LOGPIXELSY As Long = 90

'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As LongPtr
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
  ReleaseDC HWND_DESKTOP, lngDC
End Function

'--------------------------------------------------
Function TwipsPerPixelY() As Single
'--------------------------------------------------
'Returns the height of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As LongPtr
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
  ReleaseDC HWND_DESKTOP, lngDC
End Function
IBeing unable to test now, I'll make changes if this code proves faulty.

Note that putting PtrSafe was not enough. I guess the code would likely run in many cases without any other changes (if the various handles could be held within Long, but presumably would fail in all the other cases.) Since all the routines' 64 versions are provided in the link check them if you have to migrate to 64.
 
Last edited:

Users who are viewing this thread

Top Bottom