Declaring API functions in 32 or 64 bits

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • informerFR
    New Member
    • Jun 2016
    • 14

    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
  • PhilOfWalton
    Recognized Expert Top Contributor
    • Mar 2016
    • 1430

    #2
    I may be totally wrong, but I am using Access 2010 32 bit on Windows 10 (64 bit), and am not going to be able to help further.

    Firstly you refer to a 32 bit & 64 bit environments. Are we talking Windows or Access.

    Secondly, I have pinched code from all sorts of sources, an for example, in some modules I have
    Code:
    Declare PtrSafe Function GetDesktopWindow Lib "user32" () As LongPtr
    and in other modules
    Code:
    Declare Function GetDesktopWindow Lib "user32" () As Long
    My impression is that Access isn't fussy.

    Phil

    Comment

    • informerFR
      New Member
      • Jun 2016
      • 14

      #3
      Hi PhilOfWalton,

      Thanks for your answer.

      My post is about DLLs which depend on windows environment not Access.

      Comment

      • jforbes
        Recognized Expert Top Contributor
        • Aug 2014
        • 1107

        #4
        First, if you are using Access, why are you storing information into the Registry when you have a perfectly functional database available to you? I'm not being smart, it's just that that is what Databases are for.

        If for some reason you can't use the Database, it looks like the complier doesn't understand the z DataType. That isn't a surprise as Access doesn't seem to want to use any other DataTypes other than it's own except in super special occasions.

        You might want to try something like this, that way you are returning out of your function with an Access VBA DataType:
        Code:
        Public Function SetValueEx(ByVal zhKey, sValueName As String, zlType, vValue As Variant) [iCODE]As Long[/iCODE]
         
             Dim zlValue
             Dim sValue As String
         
             Select Case zlType
                 Case REG_SZ
                     sValue = vValue & Chr$(0)
                     SetValueEx = [iCODE]Clng([/iCODE]RegSetValueExString(zhKey, sValueName, 0&, zlType, sValue, Len(sValue))[iCODE])[/iCODE]
                 Case REG_DWORD
                     zlValue = vValue
                     SetValueEx = [iCODE]Clng([/iCODE]RegSetValueExLong(zhKey, sValueName, 0&, zlType, zlValue, 4)[iCODE])[/iCODE]
         
             End Select
         
        End Function

        Comment

        • informerFR
          New Member
          • Jun 2016
          • 14

          #5
          Hi jforbes

          Thanks for your answer.

          I read that all registry API retrun long value so My problem is solved

          Comment

          Working...