Error when change to 64 ( username ) (1 Viewer)

bhelmy

Registered User.
Local time
Today, 05:57
Joined
Dec 6, 2015
Messages
62
i have the below code it work in win 32 when change to win 64 , i have error

Code:
Option Compare DatabasePrivate Declare Function apiGetUserName Lib "advapi32.dll" Alias _"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As LongFunction fOSUserName() As String' Returns the network login name    Dim lngLen As Long, lngX As Long    Dim strUserName As String    strUserName = String$(254, 0)    lngLen = 255    lngX = apiGetUserName(strUserName, lngLen)    If lngX  0 Then        fOSUserName = Left$(strUserName, lngLen - 1)    Else        fOSUserName = "";    End If
End Function



Help me Please
 
Last edited by a moderator:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 03:57
Joined
Jul 9, 2003
Messages
16,282
I put code tags around it, but its not formated properly to begin with.

Please repost the code within code Tags... This will improve your chances of getting your question answered!
 

WannaKatana

New member
Local time
Today, 02:57
Joined
Oct 1, 2017
Messages
11
Private Declare PtrSafe Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpBuffer As String, nSize As LongPtr) As Long

Try this

Sent from my SM-N910V using Tapatalk
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 21:57
Joined
Feb 28, 2001
Messages
27,193
Concur with WannaKatana - switching from 32-bit to 64-bit Access, you have to fix up the pointers because of the change to address sizes. Which is the purpose of the "PtrSafe" declaration.
 

isladogs

MVP / VIP
Local time
Today, 03:57
Joined
Jan 14, 2017
Messages
18,239
Also agree with the code that WannaKatana has written
You need both PtrSafe and LongPtr for use with APIs in 64-bit Access

However disagree with the Doc's final comment
The PtrSafe is effectively a 'marker' so Access knows its safe for use in VBA7 or Win64.
It actually does nothing itself. See this link
http://codekabinett.com/rdumps.php?Lang=2&targetDoc=windows-api-declaration-vba-64-bit

Note that the modified declaration won't work in 32 -bit Access
If you need a database to work in both 'bitnesses', you need to use conditional compilation
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:57
Joined
May 7, 2009
Messages
19,247
These have same functionality:

Code:
1.
Environ("Username")

2.
Public Function sCurrentUserName() As String
  sCurrentUserName = CreateObject("wscript.network").username
End Function

3.
#If Win64 Then
Private Declare PtrSafe Function GetUserName _
    Lib "advapi32.dll" Alias "GetUserNameA" _
( _
        ByVal lpBuffer As String, _
        nSize As Long _
) As Long
#Else
Private Declare Function GetUserName _
    Lib "advapi32.dll" Alias "GetUserNameA" _
( _
        ByVal lpBuffer As String, _
        nSize As Long _
) As Long
#End If
Private Const MAXLEN = 255
Function GetLoginName() As String
    Dim strUserName As String
    Dim lngSize As Long
    strUserName = Space(MAXLEN) & Chr(0)
    lngSize = MAXLEN + 1
    If GetUserName(strUserName, lngSize) <> 0 Then
        GetLoginName = Left(strUserName, lngSize - 1)
    Else
        GetLoginName = ""
    End If
End Function
 

Users who are viewing this thread

Top Bottom