Solved Update code for use on 64-bit system (1 Viewer)

Jupie23

Registered User.
Local time
Today, 07:26
Joined
Nov 9, 2017
Messages
90
Hello,

A user is getting a compile error that the code needs to be updated for use on a 64-bit system after getting a new computer. I don't know anything about this and am just trying to help out, but I appear to have a 64-bit system and don't get any errors. I am not well versed in coding but I tried to update after Googling, but it still doesn't work. It now gives a compile error: ByRef argument type mismatch. Can someone please tell me what needs to be updated in this code? And will changing this make it not work for other people who are not having an issue? Thank you!

Code:
Option Compare Database

Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As LongPtr) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As LongPtr, 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 = vbNullString
    End If
End Function
 

Jupie23

Registered User.
Local time
Today, 07:26
Joined
Nov 9, 2017
Messages
90
I came across this function and it seems to be working! Thank you.

Code:
Public Function GetUserName() As String
    GetUserName = CreateObject("WScript.Network").UserName
End Function
 

isladogs

MVP / VIP
Local time
Today, 13:26
Joined
Jan 14, 2017
Messages
18,213
I was just going to recommend using that code instead.
However, for info the error in your code was that nSize should be Long not LongPtr
See my article
 

Jupie23

Registered User.
Local time
Today, 07:26
Joined
Nov 9, 2017
Messages
90
I was just going to recommend using that code instead.
However, for info the error in your code was that nSize should be Long not LongPtr
See my article
Thank you!
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:26
Joined
Oct 29, 2018
Messages
21,467
I came across this function and it seems to be working! Thank you.

Code:
Public Function GetUserName() As String
    GetUserName = CreateObject("WScript.Network").UserName
End Function
Glad to hear you got it sorted out. That was the code I was referring to in the link I posted.
 

Users who are viewing this thread

Top Bottom