Categories

Archives

Meta

Tags

domino

Web 本站

 

December 2008
S M T W T F S
« Nov   Feb »
 123456
78910111213
14151617181920
21222324252627
28293031  

Recent Posts

Recent Comments

GetUserDirectoryExample.LSS

‘LS.UserDirectoryEx.BE.6:

Option Public
Option Declare

%REM ************************** Purpose **************************
This little script library contains a function and some C API calls that allow you to
determine the user’s personal directory in Windows – where their things like the Document and
Settings directories are, etc. Very handy.

There are three Win32 C API calls in Declarations that are called in the getUserDirectoryPath function.
To use the function you simply pass in an empty string var on the function, and it returns the user’s
profile directory in that var. The function itself returns a 0 if there are no errors, and a non-0 if there
are errors.

The StripTerminator function is used to removed the null terminators and trailing spaces from the string buffer
used by the C API call.

%END REM

Const TOKEN_QUERY = (&H8)
Declare Function GetUserProfileDirectory Lib "userenv.dll" Alias "GetUserProfileDirectoryA" (Byval hToken As Long, Byval lpProfileDir As String, lpcchSize As Long) As Boolean
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function OpenProcessToken Lib "advapi32" (Byval ProcessHandle As Long, Byval DesiredAccess As Long, TokenHandle As Long) As Long

Function getUserDirectoryPath(userDir As String) As Long
 On Error Goto errHandler
 Dim htoken As Long
 
 REM this little bit is to make sure we’re on a Win platform – if not, throw an error
 Dim s As New NotesSession
 If Lcase(Left(s.Platform, 3)) <> "win" Then Error 1001, "WINDOWS ONLY – this function is for windows only, sorry."
 
 userDir = String(255, 0) ‘ build a string buffer to pass to the C API function
 OpenProcessToken GetCurrentProcess, TOKEN_QUERY, hToken ‘ get handle token – C API call
 Call GetUserProfileDirectory(hToken, userDir, 255) ‘ get profile directory – C API call
 userDir = StripTerminator(userDir) ‘ function to strip extra spaces and null terminator
 
getOut:
 Exit Function
errHandler:
 On Error Goto 0
 userDir = ""
 getUserDirectoryPath = Err ‘ return the error number on the function
 Error Err, Error$ & " [in " & Lsi_info(2) & "]"
 Resume getOut        
End Function
Function StripTerminator(sInput As String) As String
 On Error Goto errHandler
 Dim ZeroPos As Long
 ZeroPos = Instr(1, sInput, Chr$(0))
 If ZeroPos > 0 Then
  StripTerminator = Left$(sInput, ZeroPos – 1)
 Else
  StripTerminator = sInput
 End If
getOut:
 Exit Function
errHandler:
 On Error Goto 0
 Error Err, Error$ & " [in " & Lsi_info(2) & "]"
 Resume getOut        
End Function

 

No related posts

Write a comment





*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word