声明部分 option explicit const nerr_success = 0 const error_more_data = 234& const max_preferred_length = -1& const lg_include_indirect = &h1 const user_priv_user = &h1 const format_message_from_system = &h1000 const nerr_base = 2100 const max_nerr = nerr_base + 899 const load_library_as_datafile = &h2 const format_message_from_hmodule = &h800 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 user_info_0 usri0_name as long end type type localgroup_info_0 lgrpi0_name as long end type type localgroup_user_info_0 lgrui0_name as long end type type userinfo_1 username as string password as string passwordage as long privilege as long homedir as string comment as long flags as long scriptpath as string end type type localgroup_members_info_3 lgrmi3_domainandname as long end type type user_info_1003 usri1003_password as long end type private usr1 as userinfo_1 ’用户所在组 declare function netusergetlocalgroups lib "netapi32.dll" (byval servername as string, byval username as string, byval level as long, byval flag as long, bufptr as any, byval prefmaxlen as long, entriesread as long, totalentries as long) as long ’本地组 declare function netlocalgroupenum lib "netapi32.dll" (byval servername as string, byval level as long, bufptr as any, byval prefmaxlen as long, entriesread as long, totalentries as long, resumehandle as long) as long declare function lstrlen lib "kernel32.dll" alias "lstrlenw" (byval lpszstring as long) as long declare function lstrcpy lib "kernel32.dll" alias "lstrcpyw" (lpszstring1 as any, lpszstring2 as any) as long declare function netapibufferfree lib "netapi32.dll" (byval buffer as long) as long declare sub rtlmovememory lib "kernel32.dll" (destination as any, source as any, byval length as long) ’添加用户 private declare function netuseradd lib "netapi32" (byval servername as string, byval level as long, buffer as any, paramerr as long) as long ’用户列表 declare function netuserenum lib "netapi32.dll" (byval servername as string, byval level as long, byval filter as long, bufptr as any, byval prefmaxlen as long, entriesread as long, totalentries as long, resume_handle as long) as long ’添加到本地组 declare function netlocalgroupaddmembers lib "netapi32.dll" (byval servername as string, byval groupname as string, byval level as long, buf as any, byval totalentries as long) as long ’删除用户 declare function netuserdel lib "netapi32.dll" (servername as byte, username as byte) as long ’从组中删除用户 declare function netgroupdeluser lib "netapi32.dll" (servername as byte, groupname as byte, username as byte) as long ’修改密码 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 netgetdcname lib "netapi32.dll" (servername as long, domainname as byte, bufptr as long) as long private declare function loadlibraryex lib "kernel32" alias "loadlibraryexa" (byval lplibfilename as string, byval hfile as long, byval dwflags as long) 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 private declare sub lstrcpyw lib "kernel32" (dest as any, byval src as any) private declare function formatmessage lib "kernel32" alias "formatmessagea" (byval dwflags as long, byval lpsource as long, byval dwmessageid as long, byval dwlanguageid as long, byval lpbuffer as string, byval nsize as long, arguments as any) as long private declare function freelibrary lib "kernel32" (byval hlibmodule as long) as long 函数部分 修改密码 function changepassword(byval servername as string, byval username as string, byval oldpassword as string, byval newpassword as string) dim strserver as string, strusername as string dim strnewpassword as string, stroldpassword as string dim ui1003 as user_info_1003 dim dwlevel as long dim lret as string dim snew as string
’strserver = strconv(servername, vbunicode) strusername = strconv(username, vbunicode) ’stroldpassword = strconv(oldpassword, vbunicode) strnewpassword = strconv(newpassword, vbunicode) if left(servername, 2) = "//" then strserver = strconv(servername, vbunicode) else ’ domain was referenced, get the primary domain controller strserver = strconv(getprimarydcname(servername), vbunicode) end if if oldpassword = "" then ’ administrative over-ride of existing password. ’ does not require old password dwlevel = 1003 snew = newpassword ui1003.usri1003_password = strptr(snew) lret = netusersetinfo(strserver, strusername, dwlevel, ui1003, 0&) else ’ set the old password and attempt to change the user’s password stroldpassword = strconv(oldpassword, vbunicode) lret = netuserchangepassword(strserver, strusername, stroldpassword, strnewpassword) end if if lret <> 0 then displayerror lret else msgbox "password change was successful" end if end function 添加用户 function useradd(byval servername as string, byval username as string, byval password as string) as string servername = strconv(servername, vbunicode) usr1.username = strconv(username, vbunicode) usr1.password = strconv(password, vbunicode) usr1.privilege = user_priv_user usr1.comment = 0 usr1.flags = 0 useradd = netuseradd(servername, 1, usr1, 0) end function 添加用户到组
function addusertogroup(byval servername as string, byval groupname as string, byval username as string) as long dim lngwin32apiresultcode as long dim strservername as string dim strlocalgroupname as string dim lngbufptr as long dim udtlgmeminfo as localgroup_members_info_3 dim strname as string
udtlgmeminfo.lgrmi3_domainandname = strptr(strname) lngwin32apiresultcode = netlocalgroupaddmembers(strservername, strlocalgroupname, 3, udtlgmeminfo, 1) netapibufferfree lngbufptr end function 列举用户
sub enumusers(cbousers as combobox) dim lngwin32apiresultcode as long dim strservername as string dim lngbufptr as long dim lngmaxlen as long dim lngentriesread as long dim lngtotalentries as long dim lngresumehandle as long dim udtuserinfo0 as user_info_0 dim lngentry as long
strservername = strconv("", vbunicode) do lngwin32apiresultcode = netuserenum(strservername, 0, 0, lngbufptr, lngmaxlen, lngentriesread, lngtotalentries, lngresumehandle) if (lngwin32apiresultcode = nerr_success) or (lngwin32apiresultcode = error_more_data) then for lngentry = 0 to lngentriesread - 1 rtlmovememory udtuserinfo0, byval lngbufptr + len(udtuserinfo0) * lngentry, len(udtuserinfo0) cbousers.additem pointertostring(udtuserinfo0.usri0_name) next end if if lngbufptr <> 0 then netapibufferfree lngbufptr end if loop until lngentriesread = lngtotalentries end sub 列举本地组
sub enumlocalgroups(lstlocalgroups as listbox) dim lngwin32apiresultcode as long dim strservername as string dim lngbufptr as long dim lngentriesread as long dim lngtotalentries as long dim lngresumehandle as long dim udtlginfo0 as localgroup_info_0 dim lngentry as long
lstlocalgroups.clear strservername = strconv("", vbunicode) do lngwin32apiresultcode = netlocalgroupenum(strservername, 0, lngbufptr, max_preferred_length, lngentriesread, lngtotalentries, lngresumehandle) if (lngwin32apiresultcode = nerr_success) or (lngwin32apiresultcode = error_more_data) then for lngentry = 0 to lngentriesread - 1 rtlmovememory udtlginfo0, byval lngbufptr + len(udtlginfo0) * lngentry, len(udtlginfo0) lstlocalgroups.additem pointertostring(udtlginfo0.lgrpi0_name) next end if if lngbufptr <> 0 then netapibufferfree lngbufptr end if loop while lngwin32apiresultcode = error_more_data end sub 用户所在组
sub enumuserlocalgroups(lstuserlocalgroups as listbox, lstlocalgroups as listbox, cmbuser as combobox) dim lngwin32apiresultcode as long dim strservername as string dim strusername as string dim lngbufptr as long dim lngentriesread as long dim lngtotalentries as long dim lngresumehandle as long dim udtlginfo0 as localgroup_user_info_0 dim lngentry as long dim strlocalgroup as string dim lnglistcounter as long
lstuserlocalgroups.clear strservername = strconv("", vbunicode) strusername = strconv(cmbuser.text, vbunicode) do lngwin32apiresultcode = netusergetlocalgroups(strservername, strusername, 0, lg_include_indirect, lngbufptr, max_preferred_length, lngentriesread, lngtotalentries) if (lngwin32apiresultcode = nerr_success) or (lngwin32apiresultcode = error_more_data) then for lngentry = 0 to lngentriesread - 1 rtlmovememory udtlginfo0, byval lngbufptr + len(udtlginfo0) * lngentry, len(udtlginfo0) strlocalgroup = pointertostring(udtlginfo0.lgrui0_name) lstuserlocalgroups.additem strlocalgroup ’with lstlocalgroups ’for lnglistcounter = 0 to .listcount - 1 ’if strlocalgroup = .list(lnglistcounter) then ’.removeitem (lnglistcounter) ’end if ’next ’end with next end if if lngbufptr <> 0 then netapibufferfree lngbufptr end if loop until lngentriesread = lngtotalentries end sub 删除用户 function deluser(byval sname as string, byval uname as string) as long dim unarray() as byte, snarray() as byte unarray = uname & vbnullchar snarray = sname & vbnullchar deluser = netuserdel(snarray(0), unarray(0)) end function