Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2003
    Posts
    268

    Unanswered: Novell API to get User ID

    Does any one know how one might go about getting the userID from a Novell API call?

  2. #2
    Join Date
    Apr 2004
    Location
    outside the rim
    Posts
    1,011

    Using APIs to get User Names

    It's not an easy one, by try this.

    First, create a module and paste all of the following code into it.

    Second, use the function afnUserID (which returns a string) anywhere you need the user ID. This function will look for the Novell UserID in the registry. If it can't find it, it will try to find the Windows Login.

    In a control or a query you can use the expression =anfUserID() and in code, simply strValue = afnUserID

    This one took me a weekend to track down and piece together (all the code on the internet is for C++)

    Code:
    Option Compare Database
    Option Explicit
    
    Public Const REG_SZ As Long = 1
    Public Const REG_DWORD As Long = 4
    
    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_USERS = &H80000003
    
    Public Const ERROR_NONE = 0
    Public Const ERROR_BADDB = 1
    Public Const ERROR_BADKEY = 2
    Public Const ERROR_CANTOPEN = 3
    Public Const ERROR_CANTREAD = 4
    Public Const ERROR_CANTWRITE = 5
    Public Const ERROR_OUTOFMEMORY = 6
    Public Const ERROR_ARENA_TRASHED = 7
    Public Const ERROR_ACCESS_DENIED = 8
    Public Const ERROR_INVALID_PARAMETERS = 87
    Public Const ERROR_NO_MORE_ITEMS = 259
    
    Public Const KEY_QUERY_VALUE = &H1
    Public Const KEY_SET_VALUE = &H2
    Public Const KEY_ALL_ACCESS = &H3F
    
    Public Const REG_OPTION_NON_VOLATILE = 0
    
    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
    
    Public Function SetValueEx(ByVal hKey As Long, sValueName As String, _
        lType As Long, vValue As Variant) As Long
        Dim lValue As Long
        Dim sValue As String
        Select Case lType
            Case REG_SZ
                sValue = vValue & Chr$(0)
                SetValueEx = RegSetValueExString(hKey, sValueName, 0&, _
                                               lType, sValue, Len(sValue))
            Case REG_DWORD
                lValue = vValue
                SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, _
                lType, lValue, 4)
        End Select
    End Function
    
    Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _
        String, vValue As Variant) As Long
        Dim cch As Long
        Dim lrc As Long
        Dim lType As Long
        Dim lValue As Long
        Dim sValue As String
    
        On Error GoTo QueryValueExError
    
        ' Determine the size and type of data to be read
        lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
        If lrc <> ERROR_NONE Then Error 5
    
        Select Case lType
            ' For strings
            Case REG_SZ:
                sValue = String(cch, 0)
    
                lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _
                      sValue, cch)
                If lrc = ERROR_NONE Then
                    vValue = Left$(sValue, cch - 1)
                Else
                    vValue = Empty
                End If
            ' For DWORDS
            Case REG_DWORD:
                lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, _
                      lValue, cch)
                If lrc = ERROR_NONE Then vValue = lValue
            Case Else
                'all other data types not supported
                lrc = -1
        End Select
    
    QueryValueExExit:
        QueryValueEx = lrc
        Exit Function
    
    QueryValueExError:
        Resume QueryValueExExit
    End Function
    
    Public Function afnWINUserID() As String
    
        Dim strText As String
        Dim varVal As Variant   'Function Return
        Dim sKey As String      'Key Name
        Dim sValue As String    'Value Name
        Dim lRetVal As Long     'result of the API functions
        Dim hKey As Long        'handle of opened key
        Dim vValue As Variant   'setting of queried value
    
        sKey = "SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\EXPLORER"
        sValue = "LOGON USER NAME"
        strText = ""
        
        lRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKey, 0, KEY_QUERY_VALUE, hKey)
        If lRetVal = 2 Then ' Maybe it's Windows 98
            sKey = "Network\Logon"
            lRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKey, 0, KEY_QUERY_VALUE, hKey)
            If lRetVal <> 2 Then
                sValue = "username"
                lRetVal = QueryValueEx(hKey, sValue, vValue)
                RegCloseKey (hKey)
            Else
                vValue = "Key not found!"
            End If
        Else
            lRetVal = QueryValueEx(hKey, sValue, vValue)
            RegCloseKey (hKey)
        End If
        If Len(vValue & "") = 0 Then strText = "Not Identified!" Else strText = vValue
        
        afnWINUserID = strText
    End Function
    
    Public Function afnNWUserID() As String
    
        Dim strText As String
        Dim varVal As Variant   'Function Return
        Dim sKey As String      'Key Name
        Dim sValue As String    'Value Name
        Dim lRetVal As Long     'result of the API functions
        Dim hKey As Long        'handle of opened key
        Dim vValue As Variant   'setting of queried value
    
        sKey = "Volatile Environment"
        sValue = "NWUSERNAME"
        strText = ""
        
        lRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, sKey, 0, KEY_QUERY_VALUE, hKey)
        If lRetVal = 2 Then
            vValue = "Key not found!"
        Else
            lRetVal = QueryValueEx(hKey, sValue, vValue)
            RegCloseKey (hKey)
        End If
        If Len(vValue & "") = 0 Then strText = "Not Identified!" Else strText = vValue
        
        afnNWUserID = strText
    End Function
    
    Public Function afnUserID() As String
        ' This looks first for the Netware Username.  If that's not found, it looks for the Windows Username.
        
        Dim strText As String
        
        strText = afnNWUserID
        If strText = "Not Identified!" Or strText = "Key not found!" Then
            strText = afnWINUserID
            If strText = "Not Identified!" Or strText = "Key not found!" Then strText = "unknown"
        End If
        
        afnUserID = strText
    End Function
    Last edited by tcace; 07-16-04 at 12:47.

Posting Permissions

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