VBA issues with references to 32 bit .dlls running under Windows 10 64 bit

NewOrderFac33

Well-known Member
Joined
Sep 26, 2011
Messages
1,275
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Good afternoon,

We are going through a Win7/Office 2010 -> Win10/Office 2016 upgrade and some code that previously worked now no longer does.
In particular, I have a function to trap the ID of the currently logged on user thus:
Code:
-----------------------------------------------------------------------------------------------
Example 1 (Trapping Windows UserName)
-----------------------------------------------------------------------------------------------
Public Declare Function GETUSERNAME Lib "advapi32" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Called by:
Function GetWinUserName() As String
    Dim szBuffer As String * 100
    Dim lBufferLen As Long
    lBufferLen = 100
    If CBool(GETUSERNAME(szBuffer, lBufferLen)) Then
        GetWinUserName = Left$(szBuffer, lBufferLen - 1)
    Else
        GetWinUserName = CStr(Empty)
    End If
End Function

Called by:
Sub ShowWinUserName()
    MsgBox (GetWinUserName)
End Sub

-----------------------------------------------------------------------------------------------
Example 2 (Trapping Screen resolution)
-----------------------------------------------------------------------------------------------
Public Declare Function GETSYSTEMMETRICS Lib "user32.dll" (ByVal nIndex As Long) As Long '

Called by:
Sub SetScreenResolution()
    Dim x As Long
    Dim y As Long
    x = GETSYSTEMMETRICS(SM_CXSCREEN)
    y = GETSYSTEMMETRICS(SM_CYSCREEN)
    MsgBox ("Screen resolution = " & x & " X " & y)
End Sub

-----------------------------------------------------------------------------------------------
Example 3 (Trapping Computer Name)
-----------------------------------------------------------------------------------------------
'Private Declare Function GETCOMPUTERNAME Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Called by:
'Public Function NameOfComputer()
'    Dim ComputerName As String
'    Dim ComputerNameLen As Long
'    Dim Result As Long
'    ComputerNameLen = 256
'    ComputerName = Space(ComputerNameLen)
'    Result = GETCOMPUTERNAME(ComputerName, ComputerNameLen)
'    If Result <> 0 Then
'        NameOfComputer = Left(ComputerName, ComputerNameLen)
'    Else
'        NameOfComputer = "Unknown"
'    End If
'End Function

Called by:
Sub ShowComputerName()
    MsgBox (NameOfComputer)
End Sub

 -----------------------------------------------------------------------------------------------
Example 4 (Trapping TEMP path variable)
-----------------------------------------------------------------------------------------------
'Private Declare Function GETTEMPPATH Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH As Long = 260

Called by:
Function TempPath() As String
    TempPath = String$(MAX_PATH, Chr$(0))
    GETTEMPPATH MAX_PATH, TempPath
    TempPath = Replace(TempPath, Chr$(0), "")
End Function

Called by:
Sub ShowTempPath()
    MsgBox (TempPath)
End Sub
-----------------------------------------------------------------------------------------------

When I open the workbooks containing these functions, the Public/Private Declare lines are highlighted in red. and all related functions and Procedures have to be commented out.
I'm bright enough to figure out that they're 32bit dll-related issues (kernel32.dll, user32.dll & advapi32.dll) but I don't know what the 64bit equivalents of my examples are!
Can anyone help, please?

As always, thanks in advance

Pete
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I worked out ComputerName, UserName and FULL username with the following:
Code:
Function GetUserName(strLDAP)
    
    Dim objUser
    Dim strName
    Dim arrLDAP
    Dim intIdx
    
    On Error Resume Next
    strName = ""
    Set objUser = GetObject("LDAP://" & strLDAP)
    
    If Err.Number = 0 Then
      strName = objUser.Get("givenName") & Chr(32) & objUser.Get("sn")
    End If
    
    If Err.Number <> 0 Then
      arrLDAP = Split(strLDAP, ",")
      For intIdx = 0 To UBound(arrLDAP)
        If UCase(Left(arrLDAP(intIdx), 3)) = "CN=" Then
          strName = Trim(Mid(arrLDAP(intIdx), 4))
        End If
      Next
    End If
    
    Set objUser = Nothing
    
    GetUserName = strName
  
End Function
  
Sub MyDetails()
    
    Dim objInfo
    Dim strLDAP
    Dim MyFullName
    
    Dim MyUserName As String
    Dim MyUserDomain As String
    Dim MyComputerName As String
    Dim MyUserProfile As String
    Dim MyFullDetails As String
      
    Set objInfo = CreateObject("ADSystemInfo")
    strLDAP = objInfo.UserName
    Set objInfo = Nothing
    
    MyFullName = GetUserName(strLDAP)
    MyUserName = Environ("UserName")
    MyUserDomain = Environ("UserDomain")
    MyComputerName = Environ("ComputerName")
    MyUserProfile = Environ("UserProfile")
    
    MyFullDetails = "Username: " & MyUserName & Chr(10) & _
        "Full Name: " & MyFullName & Chr(10) & _
            "Domain: " & MyUserDomain & Chr(10) & _
                "ComputerName: " & MyComputerName & Chr(10) & _
                    "Profile: " & MyUserProfile
    
    MsgBox (MyFullDetails)
    
End Sub
The screen resolution one isn't being used anywhere at the moment, so not madly urgent.
Thanks if you took an interest.
Pete
 
Upvote 0

Forum statistics

Threads
1,215,784
Messages
6,126,878
Members
449,347
Latest member
Macro_learner

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top