转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
用VB写的一个组件,实现添加系统用户,并添加到指定组         ★★★★

用VB写的一个组件,实现添加系统用户,并添加到指定组

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1927 更新时间:2009/4/23 15:37:21

声明部分

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
   
    strServerName = StrConv(ServerName, vbUnicode)
    strLocalGroupName = StrConv(GroupName, vbUnicode)
    ''''strName = StrConv(UserName, vbUnicode)
    strName = Username
   
    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   &

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台