Approach User Support homepage Approach User Support
Answers to Frequently Asked Questions about Lotus Approach
Examples LotusScripts
Example databases
Links to other Lotus Approach support services

[Return to contents]

Example LotusScript: Find the current network ID No.1

Last updated: before December, 1998

This script was submitted to the Approach Users Mailing List by Steve Carpenter.

To get the username you need to access the Windows 95/98 registry, using functions not part of LotusScript but part of the
Windows API (Application Programming Interface). LotusScript lets you use external functions if you declare them first and
follow the calling conventions. Put the declares in the "Declarations" section of your document under "Globals", then add the
function to your scripts.

' Registry Declarations:
Declare Public Function RegOpenKeyExA Lib "advapi32" Alias "RegOpenKeyExA"
(Byval HKEY As Long,Byval lpszSubKey As String,Byval dwreserved As
Integer,Byval samDesired As Long, keyresult As Long) As Long
Declare Public Function RegQueryValueExA Lib "advapi32" Alias
"RegQueryValueExA" (Byval HKEY As Long,Byval lpszValueName As String,Byval
dwreserved As Integer, lpdwtype As Long, Byval lpData As String, readbytes
As Long) As Long
Declare Public Function RegCloseKey Lib "advapi32" Alias "RegCloseKey"
(Byval HKEY As Long) As Long

Function GetUserName()
        ' Returns user name from Windows 9x registry
        ' 10/22/98 sc  (based mainly on a Lotus sample script)
        
        Dim happkey As Long
        Dim HKEY_LOCAL_MACHINE As Long
        Dim KEY_READ As Long
        Dim HKEY_CURRENT_USER As Long
        Dim ValueType As Long
        Dim ReturnedKeyContents As String * 255
        Dim readbytes As Long
        ReturnedKeycontents$=String$(255,Chr$(32))
        Dim Username As String
        
        HKEY_LOCAL_MACHINE= &H80000002
        HKEY_CURRENT_USER= &H80000001
        
        KEY_QUERY_VALUE=1
        KEY_ENUMERATE_SUBKEYS=8
        KEY_NOTIFY=16
        KEY_READ=KEY_QUERY_VALUE Or KEY_ENUMERATE_SUBKEYS Or KEY_NOTIFY
        
        ' this is the key to read
        KeyName$="Network\Logon"
        
        ' this is the value to look up in the key
        ValueName$="UserName"
        lstat=RegOpenKeyExA(HKEY_LOCAL_MACHINE,KeyName$,0,KEY_READ,happkey)
        ReadBytes=255
        lstat=RegQueryValueExA(happkey,ValueName$,0,valueType, ReturnedKeyContents$,ReadBytes)
        regclosekey(happkey)
        
        UserName=Left$(ReturnedKeyContents$,ReadBytes-1)
        UserName=Trim$(UserName)
        If Len(UserName) = 0 Then
                GetUsername = "UnknownUser"
        Else
                GetUsername = UserName
        End If
End Function

[Return to contents]

© Copyright, JohnBrown, Trademarks, Disclaimer, Acknowledgements.