Solved

Return Netware login with VBA

Posted on 2004-10-06
2
365 Views
Last Modified: 2012-06-21
Is there a way to return the users' netware login through VBA?  I found the Environ() command that returns the windows login, but I need the netware login.  Thank you!
0
Comment
Question by:drache21
2 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 50 total points
ID: 12239133
Use this function / API call...

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function fn_get_user_name() As String

'Returns the network ID of the person that logged into the machine that is running this Access application:
'This is not the same as the Access security user.

'Visual Basic Source Code Library, The Waite group, Chapter 12.5

On Error GoTo err_fn_get_user_name

Dim str_user_name As String, lng_buffer_size As Long, lng_ret_code As Long, lng_null_character_position As Long

str_user_name = Space(80)
lng_buffer_size = Len(str_user_name)

lng_ret_code = GetUserName(str_user_name, lng_buffer_size)

lng_null_character_position = InStr(str_user_name, Chr(0))
If lng_null_character_position > 0 Then
    str_user_name = Left(str_user_name, lng_null_character_position - 1)
Else
    str_user_name = " "
End If

fn_get_user_name = UCase(str_user_name)

exit_fn_get_user_name:
    Exit Function
   
err_fn_get_user_name:
    fn_get_user_name = "Not able to get user name."
    Resume exit_fn_get_user_name
   
End Function

0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 50 total points
ID: 12241035
For NT and beyond, the quick and dirty method is:

  strNetWareLoginName = Environ("NWUSERNAME")

A better/safer method it to use the API to the NDS/eDirectory.
Here is code to copy and paste:

<code>

Option Compare Database
Option Explicit

Private Declare Function NWCallsInit Lib "calwin32" ( _
  ByRef reserved1 As Byte, _
  ByRef reserved2 As Byte) _
  As Long
 
Private Declare Function NWDSCreateContextHandle Lib "netwin32" ( _
  ByRef newHandle As Long) _
  As Long
 
Private Declare Function NWDSWhoAmI Lib "netwin32" ( _
  ByVal context As Long, _
  ByVal objectName As String) _
  As Long
 
Private Declare Function NWDSFreeContext Lib "netwin32" ( _
  ByVal context As Long) _
  As Long
'

Public Function GetNetwareUserName() As String

' Retrieve name of NetWare user currently logged in.
' 2002-03-04. Cactus Data ApS, CPH.

  ' Length of buffer for login name.
  Const clngLenBuffer As Long = 255
  ' Header to strip from retrieved user info.
  Const cstrHeader    As String = "CN="
 
  Dim lngCCode        As Long
  Dim lngNdsHandle    As Long
  Dim lngUserInfo     As Long
  Dim strUserName     As String
 
  On Error GoTo Err_GetNetwareUserName
 
  ' Dimension buffer for user name.
  strUserName = Space(clngLenBuffer)
 
  ' Initialize NetWare client.
  lngCCode = NWCallsInit(0, 0)
  If lngCCode = 0 Then
    ' Obtain handle to NDS/eDirectory.
    lngCCode = NWDSCreateContextHandle(lngNdsHandle)
    If lngCCode = 0 Then
      ' Retrieve current user name.
      lngCCode = NWDSWhoAmI(lngNdsHandle, strUserName)
      If lngCCode = 0 Then
        ' Trim string from buffer.
        lngUserInfo = InStr(strUserName, vbNullChar) - 1
        If lngUserInfo > 0 Then
          strUserName = Left(strUserName, lngUserInfo)
          strUserName = Mid(strUserName, 1 + Len(cstrHeader))
        End If
      End If
      ' Release handle to NDS/eDirectory.
      lngCCode = NWDSFreeContext(lngNdsHandle)
    End If
  End If

  GetNetwareUserName = strUserName

Exit_GetNetwareUserName:
  Exit Function

Err_GetNetwareUserName:
  MsgBox "Error " & Err.Number & ": " & Err.description, _
    vbCritical, _
    Err.Source
  Resume Exit_GetNetwareUserName
 
End Function

</code>

/gustav
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now