UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Ptrsafe For Pixels To Twips, Access 2016    
 
   
Dinodeserter
post Aug 15 2019, 08:24 AM
Post#1



Posts: 334
Joined: 7-December 12



I have some code that was in a module for defining location on a screen. It basically converts between twips and pixels, but IT just updated my machine and I apparently have 64bit instead of 32 and I get a PtrSafe error. What kind of handling has anyone used that worked well with this? I've seen too many websites to count and I get a type mismatch when I attempt to use LongLong in some of the code. I'll post an example of before and after.
CODE
Option Compare Database
Option Explicit
#If Win64 Then
Private Declare PtrSafe Function apiGetDC Lib "user32" Alias "GetDC" _
    (ByVal hWnd As LongLong) As LongLong
    #Else
    Private Declare Function apiGetDC Lib "user32" Alias "GetDC" _
    (ByVal hWnd As Long) As Long
    #End If
#If Win64 Then
Private Declare PtrSafe Function apiReleaseDC Lib "user32" Alias "ReleaseDC" _
    (ByVal hWnd As Long, ByVal hdc As LongLong) As LongLong
    #Else
    Private Declare Function apiReleaseDC Lib "user32" Alias "ReleaseDC" _
    (ByVal hWnd As Long, ByVal hdc As Long) As Long
    #End If
#If Win64 Then
Private Declare PtrSafe Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" _
    (ByVal hdc As Long, ByVal nIndex As LongLong) As LongLong
#Else
Private Declare Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" _
    (ByVal hdc As Long, ByVal nIndex As Long) As Long
    #End If
Public Enum ScreenDirection
    ScrDirVertical = 88
    ScrDirHorizontal = 90
End Enum

Function TwipsToPixels(lngTwips As LongLong, lngDirection As ScreenDirection) As LongLong

    On Error GoTo E_Handle
    Dim lngDeviceHandle As LongLong
    Dim lngPixelsPerInch As LongLong
    
    lngDeviceHandle = apiGetDC(Application.hWndAccessApp())
    lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, lngDirection)
    lngDeviceHandle = apiReleaseDC(0, lngDeviceHandle)
    TwipsToPixels = lngTwips / 1440 * lngPixelsPerInch
    
fExit:
    On Error GoTo 0
    Exit Function
    
E_Handle:
    MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume fExit
    
End Function

Function PixelsToTwips(lngPixels As LongLong, lngDirection As ScreenDirection) As LongLong

    On Error GoTo E_Handle
    Dim lngDeviceHandle As LongLong
    Dim lngPixelsPerInch As LongLong
    
    lngDeviceHandle = apiGetDC(Application.hWndAccessApp())
    lngPixelsPerInch = apiGetDeviceCaps(lngDeviceHandle, lngDirection)
    lngDeviceHandle = apiReleaseDC(0, lngDeviceHandle)
    PixelsToTwips = lngPixels * 1440 / lngPixelsPerInch
    
fExit:
    On Error GoTo 0
    Exit Function
    
E_Handle:
    MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume fExit
    
End Function

Sorry I don't have a before shot. I edited it and forgot to save a copy of the previous.

--------------------
Thanks,
Chris
Go to the top of the page
 
ADezii
post Aug 15 2019, 09:03 AM
Post#2



Posts: 2,619
Joined: 4-February 07
From: USA, Florida, Delray Beach


To the best of my knowledge, these are the 64-bit Declarations:
CODE
Declare PtrSafe Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As LongPtr) As LongPtr

Declare PtrSafe Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As LongPtr, ByVal hdc As LongPtr) As Long

Declare PtrSafe Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As LongPtr, ByVal nIndex As Long) As Long
Go to the top of the page
 
theDBguy
post Aug 15 2019, 10:35 AM
Post#3


Access Wiki and Forums Moderator
Posts: 76,290
Joined: 19-June 07
From: SunnySandyEggo


Hi Chris. Here's a link for future reference. Hope it helps... Cheers!

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
isladogs
post Aug 15 2019, 11:01 AM
Post#4


UtterAccess VIP
Posts: 1,602
Joined: 4-June 18
From: Somerset, UK


Answering on my phone so I can't supply the code now but you can find what you need in the modMetrics module of my example app here accurately move forms and controls

BTW for conditional compilation I would use
#If VBA7 Then
'declarations with PtrSafe and LongPtr (not LongLong)
#Else
….
#End If

If all users have A2010 or later, you can omit the conditions and just use the code from the VBA7 part
Hope that helps

--------------------
Go to the top of the page
 
jleach
post Aug 15 2019, 12:33 PM
Post#5


UtterAccess Editor
Posts: 10,079
Joined: 7-December 09
From: St Augustine, FL


As Colin says, use LongPtr instead of LongLong (LongLong = 64 bit integer, which is only available in 64 bit systems; LongPtr = 32bit integer on 32bit systems or 64 on 64: it's a psuedotype that converts based on the application architecture).

If you need another source look under the Code Archive for an entry of mine called GUI Functions, which has all of these in there (for 32 and 64 bit).

Cheers




--------------------
Go to the top of the page
 
Dinodeserter
post Aug 15 2019, 07:13 PM
Post#6



Posts: 334
Joined: 7-December 12



Thanks guys! This was super helpful. I haven't run into this before and I haven't been on here in a long time so I'm super rusty. Since I'm working on a new project you'll probably see me around again!

--------------------
Thanks,
Chris
Go to the top of the page
 
isladogs
post Aug 16 2019, 04:04 AM
Post#7


UtterAccess VIP
Posts: 1,602
Joined: 4-June 18
From: Somerset, UK


We were all pleased to help.

--------------------
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    15th September 2019 - 08:55 AM