Results 1 to 4 of 4
  1. #1
    Join Date
    May 2016
    Posts
    82
    Provided Answers: 3

    Unanswered: Declaring API functions in 32 or 64 bits

    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:
    Code:
     #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
    Code:
    SetValueEx(ByVal zhKey, sValueName As String, zlType, vValue As Variant) As z
    thanks a lot for your help

  2. #2
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    472
    Provided Answers: 22
    why is SetValueEx not inside the #IF?

  3. #3
    Join Date
    May 2016
    Posts
    82
    Provided Answers: 3
    Hi ranman256

    You don't need to set
    SetValueEx in #If because I declare

    #If Win64 Then
    DefLngPtr Z
    #Else
    DefLng Z
    #End If

  4. #4
    Join Date
    May 2016
    Posts
    82
    Provided Answers: 3
    All regKey API return a long value so SetValueEx return also a long and i can declare this function as follows

    SetValueEx(ByVal zhKey, sValueName As String, zlType, vValue As Variant) As Long

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •