Re: retrieving user environment settings from system process

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bob Butler (tiredofit_at_nospam.com)
Date: 03/25/04


Date: Thu, 25 Mar 2004 10:06:59 -0800


"Martin Egerter" <megerter@t-online.de> wrote in message
news:%23lDsm3nEEHA.3748@TK2MSFTNGP11.phx.gbl
> Hi everybody,
>
> I'm trying to write a little prog, that is going to run as a service
> in the system context. However, this service is supposed to retrieve
> the value of a certain user environment variable of all currently
> logged on users.
>
> In C++ this can be done by first calling LsaEnumerateLogonSessions
> and then pass the returned SessionID's to LsaGetLogonSessionData.
> This function returns a structure that holds the data I need.
>
> So far so good. The bad news is, that I didn't find any source on the
> web that would tell me how to declare these functions in VB.

Option Explicit
Option Compare Binary
Option Base 0
Private Enum LogonTypes
  ltInteractive = 2
  ltNetwork
  ltBatch
  ltService
  ltProxy
  ltUnlock
End Enum
Private Type LSA_UNICODE_STRING
  Length As Integer
  MaximumLength As Integer
  Buffer As Long
End Type
Private Type SECURITY_LOGON_SESSION_DATA
  Size As Long
  LogonId As Currency
  UserName As LSA_UNICODE_STRING
  LogonDomain As LSA_UNICODE_STRING
  AuthenticationPackage As LSA_UNICODE_STRING
  LogonType As LogonTypes
  Session As Long
  Sid As Long
  LogonTime As Currency
  LogonServer As LSA_UNICODE_STRING
  DnsDomainName As LSA_UNICODE_STRING
  Upn As LSA_UNICODE_STRING
End Type
Private Declare Function LsaFreeReturnBuffer Lib "secur32.dll" _
  (ByVal BuffPtr As Long) As Long
Private Declare Function LsaEnumerateLogonSessions Lib "secur32.dll" _
  (ByRef LogonSessionCount As Long, ByRef LogonSessionList As Long) As Long
Private Declare Function LsaGetLogonSessionData Lib "secur32.dll" _
  (ByRef LogonId As Currency, ByRef ppLogonSessionData As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  (ByRef dst As Any, ByRef src As Any, ByVal nbytes As Long)

Private Sub Main()
Dim lResult As Long
Dim lCount As Long
Dim lSessionList As Long
Dim lSessions() As Currency
Dim lData As Long ' address of pointer
Dim uData As SECURITY_LOGON_SESSION_DATA
Dim s As Long
lResult = LsaEnumerateLogonSessions(lCount, lSessionList)
If lResult = 0 Then
  If lCount > 0 Then
    ReDim lSessions(1 To lCount)
    CopyMemory lSessions(1), ByVal lSessionList, lCount * 8
    For s = 1 To lCount
      lResult = LsaGetLogonSessionData(lSessions(s), lData)
      If lResult = 0 Then
        CopyMemory uData, ByVal lData, LenB(uData)
        Debug.Print uData.LogonType,
GetLSAString(uData.AuthenticationPackage), _
              GetLSAString(uData.LogonDomain) & "\" &
GetLSAString(uData.UserName)
        lResult = LsaFreeReturnBuffer(lData)
      Else
        Debug.Print "Unable to get session " & s & ":" & lResult & "," &
Err.LastDllError
      End If
    Next
  End If
  lResult = LsaFreeReturnBuffer(lSessionList)
Else
  Debug.Print "Unable to enumerate sessions:" & lResult & "," &
Err.LastDllError
End If
End Sub

Private Function GetLSAString(ByRef LSAString As LSA_UNICODE_STRING)
Dim b() As Byte
If LSAString.Length > 0 Then
  ReDim b(1 To LSAString.Length)
  CopyMemory b(1), ByVal LSAString.Buffer, LSAString.Length
  GetLSAString = b
End If
End Function

-- 
Reply to the group so all can participate
VB.Net... just say "No"


Relevant Pages

  • Re: Impersonation using Microsoft Visual C# .NET and Windows 2000
    ... Dim fCapabilities As Integer ... Private Structure SecHandle ... "RtlMoveMemory" (ByRef Destination As SecBuffer, ... Private Declare Function NT4QuerySecurityPackageInfo Lib ...
    (microsoft.public.dotnet.security)
  • HELP! API upgrade problems
    ... Private Const mcCaseInsensitive As Long = &H40 ... (ByRef pSection As Long, ByVal pAccess As Long, ByRef pObjAttribs As ... Private Sub GetSerialParallel() ... Dim vMemoryAs Byte ...
    (microsoft.public.dotnet.languages.vb)
  • NtOpenSection - how to use in .NET??
    ... Private Const mcCaseInsensitive As Long = &H40 ... (ByRef pSection As Long, ByVal pAccess As Long, ByRef pObjAttribs As ... Private Sub GetSerialParallel() ... Dim vMemoryAs Byte ...
    (microsoft.public.dotnet.framework.interop)
  • Re: API upgrade problem - VB6 to VB.NET
    ... Private Const mcCaseInsensitive As Long = &H40 ... (ByRef pSection As Long, ByVal pAccess As Long, ByRef pObjAttribs As ... : Private Sub GetSerialParallel() ... Dim vMemoryAs Byte ...
    (microsoft.public.vb.winapi)
  • API upgrade problem - VB6 to VB.NET
    ... Private Const mcCaseInsensitive As Long = &H40 ... (ByRef pSection As Long, ByVal pAccess As Long, ByRef pObjAttribs As ... Private Sub GetSerialParallel() ... Dim vMemoryAs Byte ...
    (microsoft.public.vb.winapi)