Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with NetUserAdd, NetUserGetInfo, and NetUserSetInfo

459 views
Skip to first unread message

Enzo Maini

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

I would like some assistance using NetUserAdd, NetUserGetInfo, and
NetUserSetInfo. I have just begun to investigate these functions. I
understand that these are all Windows NT functions and are called from the
"netapi32.dll". All these functions are part of the LAN Manager functions.
All these function are UNICODE only. None of these functions are core
Win32 so they are not documented in the standard Win32api.txt file for VB.
I actually found the definitions in the Win32s.hlp file that comes with
Delphi 2.0.

I would like to know the following:

1) Does anyone already have these declares ( no big deal just would save me
some time)
2) Do I need some supporting function calls or will these work stand alone
3) Can someone please explain USER_INFO_0, USER_INFO_1 .... USER_INFO_N.
What are they and how can I find thier meaning. I was told to use 2, 3 and
22 but I have no idea how.
4) Since these are UNICODE only function, what do I have to watch out for
in VB.
5) Finally, is someone has used them in VB 4 Under NT 4 SP2 successfully I
would appriciate if you can describe your experience good or bad.

I apologize for the long question but the information on these funtions is
vague.

Enzo Maini
enm...@pcnet.com

Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

4/5/97

Hi all,

The following is the function I use to send a msg to a particular machine
or all running machines on a Windows NT domain.

This function could be useful if you develop a maintenance module in your
application for your network support guys.

I welcome any comments and suggestions.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)

Option Explicit

'Author: Jeff Hong YAN
'Date: 1/30/97
'Description: Send to msg to a machine on a Windows NT domain
' similar to that of the notification msg from printer spooler
'Note: Use with care to avoid a broadcasting to all workstations on the
domain
' if you do not want to

Declare Function NetMessageBufferSend Lib "netapi32.dll" _
(ByVal sNullParam As String, ByVal pszServer As String, ByVal
pszRecipient As String, _
ByVal pbBuffer As String, ByVal cbBuffer As Long) As Long

Function SendMsg(sToDomn As String, sFromWho As String, sToWho As String,
sTheMsg As String) As Boolean
Dim lReturn As Long
Dim sToMachine As String * 256
Dim sToDomain As String
Dim sFrom As String * 256
Dim sMessage As String * 256
Dim lLength As Long

SendMsg = False
lLength = 256
sToMachine = StrConv(sToWho & vbNullChar, vbUnicode)
sToDomain = StrConv(sToDomn, vbUnicode) + vbNullChar
sFrom = StrConv(sFromWho & vbNullChar, vbUnicode)
sMessage = StrConv(Left$(sTheMsg, 256) & vbNullChar, vbUnicode)

lReturn = NetMessageBufferSend(sToDomain, sToMachine, sFrom, sMessage,
lLength)

If lReturn = 0 Then
SendMsg = True
Else
Call NetErrorHandler(lReturn, "Sending message to " & sToWho & ".
The message is " & sTheMsg & ".")
End If
End Function

Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

Reposting.

Jeff Hong YAN

4/8/97
Hi all,

The following is the function I use to get the compelete infomation on any
valid global user account. Please note that on Windows NT, local groups and
local users are maintained with different API functions as global groups
and group users. (If one gets confused on this, he's got to take an NT
course first.) I will come back on local user info problem later.

I welcome any comments and suggestions.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)


Option Explicit

'===============================
'Author : Jeff Hong YAN
'Date : 12/4/97
'Description: Function to get complete user information
'Note : Assume current user has account operator permission
' Function works on WinNT workstation or server.
' (Tested on WinNT workstation / server 3.51, 4.0)
'===============================

'Type TUser0 ' Level 0
' ptrName As Long
'End Type

'Type TUser1 ' Level 1
' ptrName As Long
' ptrPassword As Long
' dwPasswordAge As Long
' dwPriv As Long
' ptrHomeDir As Long
' ptrComment As Long
' dwFlags As Long
' ptrScriptPath As Long
'End Type
'

'Type Level_2_User_Structure_Type
' ptrName As Long
' ptrPassword As Long
' dwPasswordAge As Long
' dwPriv As Long
' ptrHomeDir As Long
' ptrComment As Long
' dwFlags As Long
' ptrScriptPath As Long
' dwAuthFlags As Long
' ptrFullName As Long
' ptrComment2 As Long
' ptrParms As Long
' ptrWorkstations As Long
' dwLastLogon As Long
' dwLastLogoff As Long
' dwAccountExpires As Long
' dwMaxStorage As Long
' dwUnitsPerWeek As Long
' pbLogonHours As Long
' dwBadPasswordCount As Long
' dwNumLogons As Long
' ptrLogonServer As Long
' dwCountryCode As Long
' dwCodePage As Long
'End Type

'
' for dwPriv
'
'Const USER_PRIV_MASK = &H3
'Const USER_PRIV_GUEST = &H0
'Const USER_PRIV_USER = &H1
'Const USER_PRIV_ADMIN = &H2

'
' for dwFlags
'
'Const UF_SCRIPT = &H1
'Const UF_ACCOUNTDISABLE = &H2
'Const UF_HOMEDIR_REQUIRED = &H8
'Const UF_LOCKOUT = &H10
'Const UF_PASSWD_NOTREQD = &H20
'Const UF_PASSWD_CANT_CHANGE = &H40
'Const UF_NORMAL_ACCOUNT = &H200 ' Needs to be ORed with the
' other flags

Private Type USER_INFO_3
usri3_name As Long 'LPWSTR
usri3_password As Long 'LPWSTR
usri3_password_age As Long 'DWORD
usri3_priv As Long 'DWORD
usri3_home_dir As Long 'LPWSTR
usri3_comment As Long 'LPWSTR
usri3_flags As Long 'DWORD
usri3_script_path As Long 'LPWSTR
usri3_auth_flags As Long 'DWORD
usri3_full_name As Long 'LPWSTR
usri3_usr_comment As Long 'LPWSTR
usri3_parms As Long 'LPWSTR
usri3_workstations As Long 'LPWSTR
usri3_last_logon As Long 'DWORD
usri3_last_logoff As Long 'DWORD
usri3_acct_expires As Long 'DWORD
usri3_max_storage As Long 'DWORD
usri3_units_per_week As Long 'DWORD
usri3_logon_hours As Long 'PBYTE
usri3_bad_pw_count As Long 'DWORD
usri3_num_logons As Long 'DWORD
usri3_logon_server As Long 'LPWSTR
usri3_country_code As Long 'DWORD
usri3_code_page As Long 'DWORD
usri3_user_id As Long 'DWORD
usri3_primary_group_id As Long 'DWORD
usri3_profile As Long 'LPWSTR
usri3_home_dir_drive As Long 'LPWSTR
usri3_password_expired As Long 'DWORD
End Type

Declare Function MyPtrToStr Lib "kernel32" Alias "lstrcpyW" _
(RetVal As String, ByVal ptr As Long) As Long

Private Declare Function MyNetUserGetInfo Lib "netapi32.dll" Alias
"NetUserGetInfo" ( _
ByVal strServerName As String, ByVal strUserName As String, ByVal
dwLevel As Long, _
pBuffer As Long) As Long

Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As _
Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

Function GetUserInfo(sUserID As String, sUserFullName As String,
sUserDescription As String)

'===============================
'Author : Jeff Hong YAN
'Date : 12/4/97
'Description:
' (1) sUserID: user id you pass in
' (2) sUserFullName: string buffer to return the full name of the
' specified user id
' (3) sUserDescription: string buffer to return the description of
' specified user id
'===============================


Dim swServer As String * 255
Dim swUserID As String * 255
Dim lReturn As Long
Dim ptmpBuffer As Long
Dim ptr As Long

Dim sUser As String
Dim sByte() As Byte
ReDim sByte(255)

swUserID = StrConv(sUserID & vbNullChar, vbUnicode)
swServer = StrConv(vbNullChar, vbUnicode)

Dim tmpBuffer As USER_INFO_3

lReturn = MyNetUserGetInfo(swServer, swUserID, 3&, ptmpBuffer)

RtlMoveMemory tmpBuffer, ptmpBuffer, LenB(tmpBuffer)

ptr = tmpBuffer.usri3_full_name
If ptr <> 0 Then
RtlMoveMemory sByte(0), ptr, 256
sUser = vbNullString
sUser = sByte
sUser = sUser & vbNullChar
Else
sUser = "(Unknown)"
End If
sUserFullName = sUser

ptr = tmpBuffer.usri3_comment
If ptr <> 0 Then
RtlMoveMemory sByte(0), ptr, 256
sUser = vbNullString
sUser = sByte
sUser = sUser & vbNullChar
Else
sUser = "(Unknown)"
End If
sUserDescription = sUser

Call NetAPIBufferFree(ptmpBuffer)
End Function


Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

Reposting.

Jeff Hong YAN

4/8/97

Hi all,

The following is the function I use to get the current user id. There could
be many ways to do this job and I will be back on this later.

This function has been tested on Windows NT wksta / svr and Win95.

I welcome any comments and suggestions.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)

Option Explicit

Declare Function WNetGetUser Lib "mpr" Alias "WNetGetUserA" (ByVal lpName
As String, ByVal lpUserName As String, lpnLength As Long) As Long

Function GetUser() As String

Dim sUserNameBuff As String * 255
Dim lbuff As Long
Dim lReturnValue As Long

sUserNameBuff = Space(255)
lbuff = 255

lReturnValue = WNetGetUser(vbNullString, sUserNameBuff, lbuff)

If lReturnValue = 0 Then
GetUser = sUserNameBuff
Else
GetUser = "Not Known"
End If

End Function

Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

Reposting.

Jeff Hong YAN

Hi all,

The following is the function I use to get the current machine name. There
could be many ways to get the current machine name (from registry, calling
NetWksta* function, etc..). In a previous posting, a similar function for
Windows NT was proposed. This posting employs Kernel32 API function and it
works on Win95 / NT.

I welcome any comments and suggestions.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)

Option Explicit

Private Declare Function GetComputerName _
Lib "kernel32" Alias "GetComputerNameA" ( _
ByVal lpBuffer As String, _
nSize As Long) As Long

Public Function CurrentMachineName() As String
Dim lSize As Long
Dim sBuffer As String * 255

On Error GoTo CurrentMachineName_Err
lSize = 255
If GetComputerName(sBuffer, lSize) Then
lSize = InStr(sBuffer, Chr$(0)) - 1
CurrentMachineName = Left$(sBuffer, lSize)
Else
CurrentMachineName = "(Not Known)"
End If

CurrentMachineName_Exit:
Exit Function
CurrentMachineName_Err:
MsgBox "Program error in function CurrentMachineName. Error code = " &
Err, vbOKOnly, "Error"
Resume CurrentMachineName_Exit
Resume
End Function

Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

Reposting.

Jeff Hong YAN


4/13/97

Hi all,

The following is the function I use to enumerate global user groups using
"NetGroupEnum" API function. The way of wrapping this API function is
exactly the same as I did for the enumerating local user gropus in a
previous posting.

In later postings, I will give wrapper functions for enumerating users.

Comments and suggestions are welcome. Thank you for reading.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)


Option Explicit

Declare Function NetAPIBufferFree Lib "netapi32.dll" Alias _
"NetApiBufferFree" (ByVal ptr As Long) As Long

Declare Function CopyMem Lib "kernel32" Alias "RtlMoveMemory" ( _
hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long) As Long

Declare Function StrLen Lib "kernel32" Alias "lstrlenW" (ByVal ptr As Long)
As Long

Private Declare Function MyNetGroupEnum0 Lib "netapi32.dll" Alias
"NetGroupEnum" _
(ByVal ServerName As String, _
ByVal level As Long, _
Buffer As Long, _
ByVal PrefMaxLen As Long, _
lEntriesRead As Long, _
lTotalEntries As Long, _
lResumeHandle As Long) As Long

Function EnumerateGroups(ByVal sMName As String, ByRef sReturn() As String)
As Long
Dim lResult As Long
Dim lBufPtr As Long
Dim lEntriesRead As Long
Dim lTotalEntries As Long
Dim lResumeHandle As Long
Dim lBufLen As Long

Dim sGName As String
Dim I As Integer
Dim swMName As String

Dim lTheWord As Long
Dim bTestArray() As Byte
Dim sTest As String

lBufLen = 255 ' Buffer size
lResumeHandle = 0 ' Start with the first entry
swMName = StrConv(sMName, vbUnicode)
ReDim bTestArray(255)
ReDim sReturn(0)

Do
lResult = MyNetGroupEnum0(swMName, 0, lBufPtr, lBufLen, _
lEntriesRead, lTotalEntries, lResumeHandle)

EnumerateGroups = lResult
If lResult <> 0 And lResult <> 234 Then ' 234 means multiple
reads required
Call NetErrorHandler(lResult, "Error " & _
lResult & " enumerating group " & _
lEntriesRead & " of " & lTotalEntries)
Exit Function
End If

'Get the data into an array and then a string
lResult = CopyMem(bTestArray(0), lBufPtr, 256&)
sTest = bTestArray

For I = 0 To lEntriesRead - 1
'Munge 4 bytes into a long integer
lTheWord = CInt(bTestArray(I * 4 + 3)) * 2 ^ 8 + _
CInt(bTestArray(I * 4 + 2)) * 2 ^ 16 + _
CInt(bTestArray(I * 4 + 1)) * 2 ^ 8 + _
CInt(bTestArray(I * 4))
'Extract the substring we want
sGName = Mid(sTest, (lTheWord - lBufPtr) / 2 + 1,
StrLen(lTheWord))

If I > UBound(sReturn) Then ReDim Preserve
sReturn(UBound(sReturn) + 1)
sReturn(I) = sGName
Next I

Loop Until lEntriesRead = lTotalEntries

lResult = NetAPIBufferFree(lBufPtr) ' Don't leak memory

End Function

Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

Reposting.

Jeff Hong YAN

4/15/97

Hi all,

The following are functions I use to include or exclude an existing user
to/from a global group on a Windows NT domain.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)

Option Explicit

Declare Function NetGroupDelUser Lib "netapi32.dll" (ServerName As Byte, _
GroupName As Byte, Username As Byte) As Long

Declare Function NetGroupAddUser Lib "netapi32.dll" (ServerName As Byte, _
GroupName As Byte, Username As Byte) As Long

Function AddUserToGroup(ByVal sSName As String, ByVal sGName As String,
ByVal sUName As String) As Boolean
'Arguments: sSName is server name or empty for default server
' sGName is global group name
' sUName is an existing user id
'Note: you must have account operator's right to run

Dim SNArray() As Byte
Dim GNArray() As Byte
Dim UNArray() As Byte
Dim lResult As Long

AddUserToGroup = False
SNArray = sSName & vbNullChar 'Server machine name, null for
default server.
GNArray = sGName & vbNullChar 'Global group name
UNArray = sUName & vbNullChar 'User Name
lResult = NetGroupAddUser(SNArray(0), GNArray(0), UNArray(0))

Select Case lResult
Case 2220
'You probably mistook a local group as a global group or mistyped
group name
MsgBox "There is no such global group '" & sGName & "'.", vbOKOnly,
""
Exit Function
Case 2236
MsgBox "This user '" & sUName & "' is already in this group.",
vbOKOnly, ""
Exit Function
End Select

If lResult <> 0 Then
Call NetErrorHandler(lResult, "Add New User " & sUName)
Else
AddUserToGroup = True
End If

End Function

Function DelUserFromGroup(ByVal sSName As String, ByVal sGName As String,
ByVal sUName As String) As Boolean
'Arguments: sSName is server name or empty for default server
' sGName is global group name
' sUName is a user id presumably in this group
'Note: you must have account operator's right to run

Dim SNArray() As Byte
Dim GNArray() As Byte
Dim UNArray() As Byte
Dim lResult As Long

DelUserFromGroup = False
SNArray = sSName & vbNullChar 'Server machine name, null for
default server.
GNArray = sGName & vbNullChar 'Global group name
UNArray = sUName & vbNullChar 'User Name
lResult = NetGroupDelUser(SNArray(0), GNArray(0), UNArray(0))

Select Case lResult
Case 2220
'You probably mistook a local group as a global group or mistyped
group name.
MsgBox "There is no such global group '" & sGName & "'.", vbOKOnly,
""
Exit Function
Case 2234
'You probably tried to delete a user from domain user group.
MsgBox "The user '" & sUName & "' must be in this primary group.",
vbOKOnly, ""
Exit Function
Case 2237
'You probably mistyped the user name or you had deleted it already.
MsgBox "This user '" & sUName & "' is not in this group.",
vbOKOnly, ""
Exit Function
End Select

If lResult <> 0 Then
Call NetErrorHandler(lResult, " delele user " & sUName)
Else
DelUserFromGroup = True
End If

End Function


Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

Reposting.

Jeff Hong YAN

4/18/97

Hi all,

The following is functions I use to get (1) SIDs of users, groups, domains
when you give the user id, group name, or domain name; (2) user id, group
name, domain name when you give the SID.

This is the first VB wrapper functions of SID APIs posted in user groups,
as far as I know.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)

Option Explicit

Declare Function NetAPIBufferAllocate Lib "netapi32.dll" Alias _
"NetApiBufferAllocate" (ByVal ByteCount As Long, Ptr As Long) As Long

Private Declare Function NetAPIBufferFree Lib "netapi32.dll" Alias _
"NetApiBufferFree" (ByVal Ptr As Long) As Long



Declare Function CopyMem Lib "kernel32" Alias "RtlMoveMemory" ( _
hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long) As Long

Private Declare Function LookupAccountSid Lib "advapi32.dll" Alias
"LookupAccountSidA" _
(ByVal lpSystemName As String, _
SID As Any, _
ByVal name As String, _
cbName As Long, _
ByVal ReferencedDomainName As String, _
cbReferencedDomainName As Long, _
peUse As Integer) As Long

Public Declare Function LookupAccountName Lib "advapi32.dll" Alias
"LookupAccountNameA" _
(ByVal lpSystemName As String, _
ByVal lpAccountName As String, _
SID As Byte, _
cbSid As Long, _
ByVal ReferencedDomainName As String, _
cbReferencedDomainName As Long, _
peUse As Integer) As Long

Function GetMySID(sSName As String, sDName As String, sUname As String,
bRet() As Byte, iType As Integer) As String

'Author: Jeff Hong YAN
'Date : 4/17/97
'Arguments:
' sSName : current server name (In)
' sDName : current domain name (In)
' sUName : the account name (user id, global group name, etc..)
(In)
' bRet() : SID of the account (Out)
' iType : Type of SID (Out)
'Return Value:
' SID in a string

Dim lReturn As Long
Dim iSIDTypeBuf As Integer
Dim ptrSIDBuf As Long
Dim sRet As String

Dim lSIDBufLen As Long
Dim lDNameLen As Long

Dim i As Long
Dim sSID As String

'I pass sDName string ByVal, to the dll function, the length of the
string
'is increased by 1 byte. This is a trap that goes to GPF
lDNameLen = Len(sDName) + 1

lSIDBufLen = 0
iSIDTypeBuf = 0
ReDim bRet(0)
lReturn = LookupAccountName( _
sSName, _
sUname, _
bRet(0), _
lSIDBufLen, _
sDName, _
lDNameLen, _
iSIDTypeBuf)

Select Case Err.LastDllError
Case 1722
MsgBox "Bad server name - Jeff"
Case 1322, 1332
MsgBox "Bad user name - Jeff"
Case 122 'buffer size for sid is not large enough
lReturn = NetAPIBufferAllocate(lSIDBufLen, ptrSIDBuf)
If lReturn <> 0 Then
MsgBox "Failed in allocating space"
GoTo GetMySID_Exit
End If
sSID = Space(lSIDBufLen + 1)
sDName = Space(255)
lDNameLen = 255
ReDim bRet(lSIDBufLen)

lReturn = LookupAccountName( _
sSName, _
sUname, _
bRet(0), _
lSIDBufLen, _
sDName, _
lDNameLen, _
iSIDTypeBuf)
If lReturn <> 0 Then
For i = 1 To lSIDBufLen
sRet = sRet & bRet(i - 1)
Next i
Debug.Print "Type = " & iSIDTypeBuf & " SID = " & sRet & "
Description = " & sDName
GetMySID = sRet
iType = iSIDTypeBuf
Else
GetMySID = ""
MsgBox "Failed in obtaining the SID."
GoTo GetMySID_Exit
End If
Case Else
Call NetErrorHandler(Err.LastDllError, "get the SID of account '" &
sUname & "'.")
End Select

GetMySID_Exit:
lReturn = NetAPIBufferFree(ptrSIDBuf)
End Function

Function CheckMySID(sSName As String, sDName As String, bSID() As Byte,
iAccountType As Integer) As String

'Author: Jeff Hong YAN
'Date : 4/17/97
'Arguments:
' sSName : current server name (In)
' sDName : current domain name (In)
' bSID() : SID of the account (In)
' iAccountType : Type of SID (Out)
'Return Value:
' Account Name in a string

Dim lReturn As Long 'Hold return value of API calls
Dim sUNameBuf As String 'Buffer of account name returned by API
Dim lUNameBufLen As Long 'Length of the account name buffer
Dim lDNameLen As Long 'Length of domain name

'I pass sDName string ByVal, to the dll function, the length of the
string
'is increased by 1 byte. This is a trap that goes to GPF
lDNameLen = Len(sDName) + 1

lUNameBufLen = 255
sUNameBuf = Space(lUNameBufLen)
lReturn = LookupAccountSid( _
sSName, _
bSID(0), _
sUNameBuf, _
lUNameBufLen, _
sDName, _
lDNameLen, _
iAccountType)


If lReturn = 0 Then

Call NetErrorHandler(Err.LastDllError, "")
Else
' MsgBox "Success!!!"
CheckMySID = Left$(sUNameBuf, InStr(sUNameBuf, Chr$(0)) - 1)
End If

End Function


Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

Reposting.

Jeff Hong YAN

4/5/97

Hi all,

The following is the function I use to get the current domain controller's
machine name.

I welcome any comments and suggestions.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)


Option Explicit

'Author: Jeff Hong YAN
'Date: 12/4/96
'Description: The computer name of the primary domain
' controller on a Windows NT domain

Declare Function NetGetDCName Lib "netapi32.dll" (ByVal sServerName As
String, _
ByVal sDomainName As String, lPtr As Long) As Long

Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyW" _
(bRet As Byte, ByVal lPtr As Long) As Long

Declare Function NetApiBufferFree Lib "Netapi32" (ByVal buffer As Long) As
Long

Function GetPrimaryDomainControllerName() As String

Dim sDCName As String
Dim bDCNArray() As Byte
Dim lReturn As Long
Dim lPtr As Long

ReDim bDCNArray(256)

lReturn = NetGetDCName(vbNullChar, vbNullChar, lPtr)

If lReturn <> 0 Then
Call NetErrorHandler(lReturn, "Calling NetGetDCName")
Exit Function
End If

lReturn = PtrToStr(bDCNArray(0), lPtr)

lReturn = NetApiBufferFree(lPtr)

sDCName = bDCNArray()
GetPrimaryDomainControllerName = sDCName

End Function


Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

Reposting.

Jeff Hong YAN

Hi all,

The following is the function I use to validate user logon on Windows NT
domain. User account should be global account.

This function is useful if you want to implement an integrated user logon
with the Windows NT domain.

I welcome any comments and suggestions.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)


Option Explicit

'Author: Jeff Hong YAN
'Date: 1/21/97
'Description: Validate user id and password on Windows NT domain.
'This function works on NT Workstation
'If you want to crack a user account, this function is a start.

Private Declare Function LogonUserA Lib "advapi32.dll" _
(ByVal lpszUsername As String, ByVal lpszDomain As String, _
ByVal lpszPassword As String, ByVal dwLogonType As Long, _
ByVal dwLogonProvider As Long, phToken As Long) As Long

'Logon type
Const LOGON32_LOGON_INTERACTIVE = 2
Const LOGON32_NETWORK = 3
Const LOGON32_LOGON_BATCH = 4
Const LOGON32_LOGON_SERVICE = 5

'Provider
Const LOGON32_PROVIDER_WINNT35 = 1
Const LOGON32_PROVIDER_DEFAULT = 0

Function ValidateLogon(UserID As String, Pass As String, sDomainName As
String) As Boolean

Dim sUserID As String
Dim sPass As String
Dim sDName As String

Dim hToken As Long
Dim lReturn As Long

sUserID = Trim$(UserID)
sPass = Trim$(Pass)
ValidateLogon = False

lReturn = LogonUserA(sUserID, sDomainName, sPass, LOGON32_NETWORK ,
LOGON32_PROVIDER_DEFAULT, hToken)
If lReturn = 1 Then
ValidateLogon = True
Exit For
End If

End Function

Jeff Hong YAN

unread,
Jun 16, 1997, 3:00:00 AM6/16/97
to

Reposting:

Jeff Hong YAN

Hi all,

The following is the function I use to trap error code returned by
NetAPI32.dll function calls.

I welcome any comments and suggestions.

Rgds
Jeff Hong YAN
(Hong...@WorldNet.ATT.Net)


'Author: Jeff Hong YAN
'Date: 11/21/96
'Description: This module is for handling the error code returned by
NetAPI32.dll API function calls
'Usage: pass the return value as the first argument, the second string
argument is user defined entity to be displayed as part of error msg.
'All error codes can be found in APIViewer

Sub NetErrorHandler(lErrorCode As Long, sEntity As String)

Dim sMsg As String

On Error Resume Next

If lErrorCode& = 0 Then Exit Sub
sMsg$ = "An error happend when " & sEntity$ & ", "
Select Case lErrorCode&
Case ERROR_ACCESS_DENIED, 65
sMsg$ = sMsg$ + "you had insufficient privileges and the action
failed."
Case 50
sMsg$ = sMsg$ + "a network request was not supported. Possibly :
NetStatisticsGet remoted to downLevel OS/2 server does not work for the
service LanmanWorkstation."
Case ERROR_BAD_NETPATH
sMsg$ = sMsg$ + "the domain controler name is wrong."
Case 71
sMsg$ = sMsg$ + "a network path was not found and the action
failed."
Case 86
sMsg$ = sMsg$ + "an wrong old password was used and the action
failed."
Case 87
sMsg$ = sMsg$ + "an invalid parameter was specified and the action
failed."
Case 123
sMsg$ = sMsg$ + "invalid computer name. (The computer name you gave
is not NULL or the client computer."
Case 124
sMsg$ = sMsg$ + "an invalid parameter was attempted to be passed
and Failed. Possibly: wrong level specified."
Case 234
sMsg$ = sMsg$ + "too little memory (less than 40 bytes) was
allocated to perform the action and failed."
Case 2123
sMsg$ = sMsg$ + "too little memory was allocated to perform the
action and failed."
Case 2102
sMsg$ = sMsg$ + "a device driver was not installed to conduct the
action."
Case 2106
sMsg$ = sMsg$ + "an action was attempted that can take place only
from an NT Server."
Case 2138
sMsg$ = sMsg$ + "NT Workstation services have not been started."
Case 2141
sMsg$ = sMsg$ + "either the NT Server is not configured for this
transaction or IPC is not shared."
Case ERROR_BAD_USERNAME
sMsg$ = sMsg$ + "an invalid NT user name was specified and the
action failed."
Case 2220
sMsg$ = sMsg$ + "the NT group name does not exist."
Case 2221
sMsg$ = sMsg$ + "the NT user account name was not found."
Case 2223
sMsg$ = sMsg$ + "the NT group name already exists."
Case 2224
sMsg$ = sMsg$ + "the NT user account name already exists."
Case 2226
sMsg$ = sMsg$ + "the specified NT Server is not the Primary Domain
Controller and the action failed."
Case 2227
sMsg$ = sMsg$ + "the NT Server is not running at user-level
security."
Case 2228
sMsg$ = sMsg$ + "the accounts database became full. "
Case 2229
sMsg$ = sMsg$ + "a reserved error (code 2229) occurred in accessing
the NT accounts database."
Case 2245
sMsg$ = sMsg$ + "the password is too long or too short."
Case 2247
sMsg$ = sMsg$ + "the NT accounts database file is corrupted."
Case 2320
sMsg$ = sMsg$ + "bad domain name."
Case 2351
sMsg$ = sMsg$ + "an invalid NT computer name was specified and the
action failed."
Case 2456
sMsg$ = sMsg$ + "the NT user accounts database cannot be enlarged
because the NT Server's hard disk is full."
Case Else
sMsg$ = sMsg$ + "an error code of " + Str(lErrorCode&) + " was
generated."
End Select
MsgBox sMsg$ & " Error Code = " & lErrorCode&
End Sub


0 new messages