none
Declaring API functions in 32 or 64 bits RRS feed

  • General discussion

  • Hi 

    My Access project must run on 32 bits & 64 bits and use registry API. So I turned to How To Use the Registry API to Save and Retrieve Setting for inspiration. And for typing my variables considering the environment, I use DefType command. 

    My code in a module is as follows:

     #If Win64 Then
            DefLngPtr Z
        #Else
            DefLng Z
        #End If
        
        #If Win64 Then      
            
            Private Declare PtrSafe Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As LongPtr) As Long
            Private Declare PtrSafe Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As LongPtr, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As LongPtr, lpdwDisposition As Long) As LongPtr
            Private Declare PtrSafe Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As LongPtr, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As LongPtr
            Private Declare PtrSafe Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal lpReserved As LongPtr, lpType As Long, ByVal lpData As String, lpcbData As Long) As LongPtr
            Private Declare PtrSafe Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal lpReserved As LongPtr, lpType As Long, lpData As Long, lpcbData As Long) As LongPtr
            Private Declare PtrSafe Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal lpReserved As LongPtr, lpType As Long, ByVal lpData As Long, lpcbData As Long) As LongPtr
            Private Declare PtrSafe Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal Reserved As LongPtr, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As LongPtr
            Private Declare PtrSafe Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal Reserved As LongPtr, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As LongPtr
       
     #Else
        
            Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
            Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
            Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
            Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
            Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
            Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
            Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
            Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
        #End If

    Public Function SetValueEx(ByVal zhKey, sValueName As String, zlType, vValue As Variant) As z
           
        Dim zlValue
        Dim sValue As String
        
        Select Case zlType
            Case REG_SZ
                sValue = vValue & Chr$(0)
                SetValueEx = RegSetValueExString(zhKey, sValueName, 0&, zlType, sValue, Len(sValue))
            Case REG_DWORD
                zlValue = vValue
                SetValueEx = RegSetValueExLong(zhKey, sValueName, 0&, zlType, zlValue, 4)
                
            End Select
            
       End Function

    But when I compile the project, this compilation error message is displayed due to value returned by the SetValueEx

    SetValueEx(ByVal zhKey, sValueName As String, zlType, vValue As Variant) As z
    thanks a lot for your help
    Tuesday, June 7, 2016 6:54 AM

All replies

  • Hi 

    My Access project must run on 32 bits & 64 bits and uses registry API. So I turned to How To Use the Registry API to Save and Retrieve Setting for inspiration. And for typing my variables considering the environment, I use DefType command. 

    My code in a module is as follows:

     #If Win64 Then
            DefLngPtr Z
        #Else
            DefLng Z
        #End If
        
        #If Win64 Then      
            
            Private Declare PtrSafe Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As LongPtr) As Long
            Private Declare PtrSafe Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As LongPtr, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As LongPtr, lpdwDisposition As Long) As LongPtr
            Private Declare PtrSafe Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As LongPtr, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As LongPtr
            Private Declare PtrSafe Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal lpReserved As LongPtr, lpType As Long, ByVal lpData As String, lpcbData As Long) As LongPtr
            Private Declare PtrSafe Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal lpReserved As LongPtr, lpType As Long, lpData As Long, lpcbData As Long) As LongPtr
            Private Declare PtrSafe Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal lpReserved As LongPtr, lpType As Long, ByVal lpData As Long, lpcbData As Long) As LongPtr
            Private Declare PtrSafe Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal Reserved As LongPtr, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As LongPtr
            Private Declare PtrSafe Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal Reserved As LongPtr, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As LongPtr
       
     #Else
        
            Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
            Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
            Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
            Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
            Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
            Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
            Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
            Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
        #End If

    Public Function SetValueEx(ByVal zhKey, sValueName As String, zlType, vValue As Variant) As z
           
        Dim zlValue
        Dim sValue As String
        
        Select Case zlType
            Case REG_SZ
                sValue = vValue & Chr$(0)
                SetValueEx = RegSetValueExString(zhKey, sValueName, 0&, zlType, sValue, Len(sValue))
            Case REG_DWORD
                zlValue = vValue
                SetValueEx = RegSetValueExLong(zhKey, sValueName, 0&, zlType, zlValue, 4)
                
            End Select
            
       End Function

    But when I compile the project, this compilation error message: Type defined by user not defined is displayed due to value returned by the SetValueEx

    SetValueEx(ByVal zhKey, sValueName As String, zlType, vValue As Variant) As z
    thanks a lot for your help


    Tuesday, June 7, 2016 6:55 AM
  • Hi technet,

    This forum is used to discuss about Access automation like Access object model, your issue is more related with Registry API, I will move this thread to the more related fourm.

    Reference: http://social.msdn.microsoft.com/Forums/en-US/home?forum=isvvba

    Thanks for your understanding.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, June 8, 2016 2:34 AM
    Moderator
  • Try this:

    #If Win64 Then
        Public Function SetValueEx(ByVal zhKey, sValueName As String, zlType, vValue As Variant) As LongPtr
    #Else
        Public Function SetValueEx(ByVal zhKey, sValueName As String, zlType, vValue As Variant) As Long
    #End If
        Dim zlValue
        Dim sValue As String
        
        Select Case zlType
            Case REG_SZ
                sValue = vValue & Chr$(0)
                SetValueEx = RegSetValueExString(zhKey, sValueName, 0&, zlType, sValue, Len(sValue))
            Case REG_DWORD
                zlValue = vValue
                SetValueEx = RegSetValueExLong(zhKey, sValueName, 0&, zlType, zlValue, 4)
        End Select
    End Function
    


    http://www.ambienteoffice.com.br - http://www.clarian.com.br

    Wednesday, June 8, 2016 10:11 AM