?
Solved

Set user group

Posted on 2003-03-13
5
Medium Priority
?
146 Views
Last Modified: 2011-08-18
I would like to set a user to a group e.g:

i have a user called "POS" and the user group is "users". But i want to change it to "Power Users".

I know how to get the group a user belongs to. But not how to set.

Thanks in advanced!
Bernard
0
Comment
Question by:sexy-sx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 8134904
Hi sexy-sx,

Check some of these samples:

http://www.btinternet.com/~vbadmincode/code.htm#User and Group

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 

Author Comment

by:sexy-sx
ID: 8149224
i've checked the samples and none of them show how to change a users group.

i also forgot to mention that the user is located on another PC.

So i would like to change a remote user's group.

Thanks,
bernie
0
 

Author Comment

by:sexy-sx
ID: 8156018
That's ok, i worked it out myself.

Got it from msdn.

Thanks anyway!
Bernie
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8157115
PAQ'd and all 300 points refunded.

sexy-sx,
It would be most appreciated if you could post the MS article number or something that would add value to this question.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange
0
 

Author Comment

by:sexy-sx
ID: 8157383
Put this in a class module

Private Declare Function NetUserAdd _
Lib "NETAPI32.DLL" (servername As Byte, _
ByVal level As Long, Buffer As USER_INFO_3, _
parm_err As Long) As Long

Private Declare Function NetApiBufferAllocate _
Lib "NETAPI32.DLL" (ByVal ByteCount As Long, _
Ptr As Long) As Long

Private Declare Function NetApiBufferFree Lib _
"Netapi32" (ByVal pBuffer As Long) As Long

Private Declare Function NetGetDCName Lib _
"NETAPI32.DLL" (ByVal sServerName As String, _
ByVal sDomainName As String, ByVal lPtr As _
Long) As Long

Private Declare Function lstrcpyW Lib _
"kernel32.dll" (bRet As Byte, ByVal _
lPtr As Long) As Long

Private Declare Function NetLocalGroupAddMembers _
Lib "Netapi32" (ByVal psServer As Long, ByVal _
psLocalGroupName As Long, ByVal level As Long, _
pPtrBuffer As Long, ByVal membercount As Long) As Long

Private Declare Function NetLocalGroupDelMembers _
Lib "NETAPI32.DLL" (ByVal psServer As Long, ByVal _
psLocalGroupName As Long, ByVal level As Long, _
pPtrBuffer As Long, ByVal membercount As Long) As Long

Private Declare Function NetServerSetInfo Lib _
"Netapi32" (sServerName As Byte, ByVal lLevel _
As Long, vBuffer As Long, ParmError As Long) As Long

Private Declare Function LogonUser Lib "Advapi32" _
Alias "LogonUserA" (ByVal lpszUsername As String, _
ByVal lpszDomain As Any, ByVal lpszPassword As String, _
ByVal dwLogonType As Long, ByVal dwLogonProvider As _
Long, phToken As Long) As Long

Private Declare Sub CopyMem Lib "kernel32" Alias _
"RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal _
lSize As Long)

Private Declare Function StrLenA Lib "kernel32" Alias _
"lstrlenA" (ByVal Ptr As Long) As Long

Private Declare Function StrCopyA Lib "kernel32" Alias _
"lstrcpyA" (ByVal RetVal As String, ByVal Ptr As Long) As Long

Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias _
"WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, _
ByVal dwUsage As Long, lpNetResource As Any, lppEnumHwnd _
As Long) As Long

Private Declare Function WNetEnumResource Lib "mpr.dll" Alias _
"WNetEnumResourceA" (ByVal pEnumHwnd As Long, lpcCount As Long, _
lpBuffer As NETRESOURCE, lpBufferSize As Long) As Long

Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal _
p_lngEnumHwnd As Long) As Long

Private Declare Function NetUserGetInfo Lib "NETAPI32.DLL" _
(servername As Byte, Username As Byte, ByVal level As Long, _
Buffer As Long) As Long

Private Declare Function NetUserEnum Lib "NETAPI32.DLL" _
(servername As Byte, ByVal level As Long, ByVal Filter _
As Long, Buffer As Long, ByVal PrefMaxLen As Long, _
EntriesRead As Long, totalentries As Long, ResumeHwnd _
As Long) As Long

Private Declare Function StrLenW Lib "kernel32" Alias _
"lstrlenW" (ByVal Ptr As Long) As Long

Private Declare Function NetUserChangePassword Lib _
"NETAPI32.DLL" (ByVal domainname As String, ByVal _
Username As String, ByVal OldPassword As String, _
ByVal NewPassword As String) As Long

Private Declare Function NetUserSetInfo Lib "NETAPI32.DLL" _
(ByVal servername As String, ByVal Username As String, ByVal _
level As Long, UserInfo As Any, ParmError As Long) As Long

' ---------------------------------------------
' Possible errors with API call
' ---------------------------------------------

Private Const ERROR_ACCESS_DENIED As Long = 5
Private Const NERR_BASE As Long = 2100
Private Const NERR_GroupExists As Long = NERR_BASE + 123
Private Const NERR_NotPrimary As Long = NERR_BASE + 126
Private Const NERR_UserExists As Long = NERR_BASE + 124
Private Const NERR_PasswordTooShort As Long = NERR_BASE + 145
Private Const NERR_InvalidComputer As Long = NERR_BASE + 251
Private Const NERR_Success As Long = 0&

' ---------------------------------------------
' General constants used
' ---------------------------------------------

Private Const constUserInfoLevel3 As Long = 3
Private Const TIMEQ_FOREVER As Long = -1&
Private Const MAX_PATH As Long = 260&
Private Const DOMAIN_GROUP_RID_USERS As Long = &H201&
Private Const USER_MAXSTORAGE_UNLIMITED As Long = -1&
Private Const LocalGroupMembersInfo3 As Long = 3&
Private Const MAX_RESOURCES As Long = 256
Private Const NOT_A_CONTAINER As Long = -1
Private Const RESOURCE_GLOBALNET As Long = &H2&
Private Const RESOURCETYPE_ANY As Long = &H0&
Private Const RESOURCEUSAGE_ALL As Long = &H0&
Private Const NO_ERROR As Long = 0&
Private Const RESOURCE_ENUM_ALL As Long = &HFFFF

' ---------------------------------------------
' Constants used by LogonUser
' ---------------------------------------------

Private Const LOGON32_PROVIDER_DEFAULT As Long = 0&
Private Const LOGON32_PROVIDER_WINNT35 As Long = 1&
Private Const LOGON32_LOGON_INTERACTIVE As Long = 2&
Private Const LOGON32_LOGON_NETWORK As Long = 3&
Private Const LOGON32_LOGON_BATCH As Long = 4&
Private Const LOGON32_LOGON_SERVICE As Long = 5&

' ---------------------------------------------
' Used by usri3_flags element of data structure
' ---------------------------------------------

Private Const UF_SCRIPT As Long = &H1&
Private Const UF_ACCOUNTDISABLE As Long = &H2&
Private Const UF_HOMEDIR_REQUIRED As Long = &H8&
Private Const UF_LOCKOUT As Long = &H10&
Private Const UF_PASSWD_NOTREQD As Long = &H20&
Private Const UF_PASSWD_CANT_CHANGE As Long = &H40&
Private Const UF_DONT_EXPIRE_PASSWD As Long = &H10000
Private Const STILL_ACTIVE As Long = &H103&
Private Const UF_NORMAL_ACCOUNT As Long = &H200&
Private Const UF_SERVER_TRUST_ACCOUNT As Long = &H2000&
Private Const PROCESS_QUERY_INFORMATION As Long = &H400&
Private Const UF_TEMP_DUPLICATE_ACCOUNT As Long = &H100&
Private Const UF_INTERDOMAIN_TRUST_ACCOUNT As Long = &H800&
Private Const UF_WORKSTATION_TRUST_ACCOUNT As Long = &H1000&

' ---------------------------------------------
' The USER_INFO_3 data structure
' ---------------------------------------------

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

Private Type USERINFO_2_API
  usri2_name As Long
  usri2_password As Long
  usri2_password_age As Long
  usri2_priv As Long
  usri2_home_dir As Long
  usri2_comment As Long
  usri2_flags As Long
  usri2_script_path As Long
  usri2_auth_flags As Long
  usri2_full_name As Long
  usri2_usr_comment As Long
  usri2_parms As Long
  usri2_workstations As Long
  usri2_last_logon As Long
  usri2_last_logoff As Long
  usri2_acct_expires As Long
  usri2_max_storage As Long
  usri2_units_per_week As Long
  usri2_logon_hours As Long
  usri2_bad_pw_count As Long
  usri2_num_logons As Long
  usri2_logon_server As Long
  usri2_country_code As Long
  usri2_code_page As Long
End Type

Private Type USER_INFO_10_API
  name As Long
  Comment As Long
  UsrComment As Long
  FullName As Long
End Type

Private Type USER_INFO_1003
  usri1003_password As Long
End Type

Private Type LOCALGROUP_MEMBERS_INFO_3
  DomainAndName As Long
End Type

' Type used by NetServerSetInfo

Private Type SERVER_INFO_1005
  sv1005_comment As Long
End Type

Private Type NETRESOURCE
  dwScope As Long
  dwType As Long
  dwDisplayType As Long
  dwUsage As Long
  pLocalName As Long
  pRemoteName As Long
  pComment As Long
  pProvider As Long
End Type

'-------------------

Public Function AddUserToLocal(ByVal xi_strGroupName As String, _
ByVal xi_strUserName As String, _
ByVal xi_strServerName As String) As Boolean
 
Dim p_lngPtrGroupName As Long
Dim p_lngPtrUserName As Long
Dim p_lngPtrServerName As Long
Dim p_lngMemberCount As Long
Dim p_lngRtn As Long
 
' Convert the server name to a pointer
If Len(Trim$(xi_strServerName)) = 0 Then
  p_lngPtrServerName = 0&
Else
  p_lngPtrServerName = StrPtr(xi_strServerName)
End If

' Convert the group name to a pointer
p_lngPtrGroupName = StrPtr(xi_strGroupName)
 
' Convert the user name to a pointer
p_lngPtrUserName = StrPtr(xi_strUserName)
 
' Add the user
p_lngMemberCount = 1
 
p_lngRtn = NetLocalGroupAddMembers(p_lngPtrServerName, _
p_lngPtrGroupName, _
LocalGroupMembersInfo3, _
p_lngPtrUserName, _
p_lngMemberCount)
 
If p_lngRtn = NERR_Success Then
  AddUserToLocal = True
Else
  AddUserToLocal = False
End If
 
End Function
 
' How to use it
NetworkNT.AddUserToLocal "Power Users", UserName, ComputerName
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

752 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