unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,shellapi;
type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Edit1: TEdit; Label1: TLabel; Label2: TLabel; Edit2: TEdit; Label3: TLabel; Edit3: TEdit; Button3: TButton; Button4: TButton; Label4: TLabel; Edit4: TEdit; Button7: TButton; Button8: TButton; ListBox1: TListBox; Button6: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button7Click(Sender: TObject); procedure Button8Click(Sender: TObject); procedure Button6Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1; type USER_INFO_1=record usri1_name:pwidechar; usri1_password:pwidechar; usri1_password_age:dword; usri1_priv:dword; usri1_home_dir:pwidechar; usri1_comment:pwidechar; usri1_flags:dword; usri1_script_path:pwidechar; end; buffer=^USER_INFO_1;
type GROUP_USERS_INFO_1=record grui1_name:pwidechar; grui1_attributes:DWORD; end; buffer1=^GROUP_USERS_INFO_1;
type LOCALGROUP_MEMBERS_INFO_3=record lgrmi3_domainandname:pwidechar; end; buffer2=^LOCALGROUP_MEMBERS_INFO_3;
implementation
{$R *.dfm} //加用户 function NetUserAdd(Server:PWideChar;Level:DWORD;Buf:pointer;ParmError:dword):LongInt;stdcall; external 'netapi32.dll' //删除用户 function NetUserDel(Server:PWideChar;username:pwidechar):LongInt;stdcall; external 'netapi32.dll'; //改用户密码 function NetUserChangePassword(domainname:PWideChar;username:pwidechar;oldpassword:pwidechar;newpassword:pwidechar):LongInt;stdcall; external 'netapi32.dll'; //设置用户信息 function NetUserSetInfo(Servername:PWideChar;Username:pWidechar;Level:DWORD;Buf:pointer;ParmError:dword):LongInt;stdcall; external 'netapi32.dll'; //设置用户的组信息(无用) //function NetUserSetGroups(Servername:PWideChar;Username:pWidechar;Level:DWORD;Buf:pointer;num_entries:DWORD):LongInt;stdcall; external 'netapi32.dll'; //在指定的组中加用户(无用) //function NetGroupAddUser(Servername:PWideChar;Groupname:PWideChar;Username:PWideChar):LongInt;stdcall; external 'netapi32.dll'; //获得nt用户 function NetUserEnum(ServerName: PWideChar;Level,Filter: DWord;var Buffer: Pointer;PrefMaxLen: DWord;var EntriesRead,TotalEntries, ResumeHandle: DWord): LongWord; stdcall; external 'netapi32.dll'; function NetApiBufferFree(pBuffer: PByte): LongInt; stdcall; external'netapi32.dll'; //在本地指定的组中加用户 function NetLocalGroupAddMembers(servername:PWideChar;LocalGroupName:pwidechar;Level:DWORD;Buf:pointer;membercount:dword):LongInt;stdcall; external 'netapi32.dll' //在本地指定的组中删除用户 function NetLocalGroupDelMembers(servername:PWideChar;LocalGroupName:pwidechar;Level:DWORD;Buf:pointer;membercount:dword):LongInt;stdcall; external 'netapi32.dll'
{得到计算机名} function GetComputerNameD:string; var Buffer: array[0..MAX_COMPUTERNAME_LENGTH] of Char; BSize:Cardinal; begin BSize:=SizeOf(Buffer); if GetComputerName(Buffer,BSize) then begin Result:=Buffer; end else begin Result:=''; end; end;
procedure TForm1.Button1Click(Sender: TObject); var buf:buffer; var i:longint; begin getmem(buf,sizeof(USER_INFO_1)); with buf^ do begin usri1_name:=StringToOleStr(edit1.text); //string to pwidechar usri1_password:=StringToOleStr(edit2.text); //string to pwidechar // usri1_name:='123'; // usri1_password:='123456'; usri1_password_age:=0; //此内容被忽略 usri1_priv:=1; //用户等级 建立的时候必须为user usri1_home_dir:=nil; usri1_comment:=nil; //注释 515:禁用 513:使用 usri1_flags:=0513; usri1_script_path:=nil; end; netuseradd(nil,1,pointer(buf),0); freemem(buf); end;
procedure TForm1.Button2Click(Sender: TObject); var username:pwidechar; begin username:=StringToOleStr(edit1.text); NetUserDel(nil,username); end;
procedure TForm1.Button3Click(Sender: TObject); begin NetUserChangePassword(nil,StringToOleStr(edit1.text),StringToOleStr(edit2.text),StringToOleStr(edit3.text)); end;
procedure TForm1.Button4Click(Sender: TObject); var buf:buffer; i:longint; begin //好象没啥大用处 getmem(buf,sizeof(USER_INFO_1)); with buf^ do begin // usri1_name:=StringToOleStr(edit1.text); //由于是修改用户信息,此内容被忽略 usri1_password:=StringToOleStr(edit2.text); //string to pwidechar usri1_password_age:=0; //此内容被忽略 usri1_priv:=2; //用户等级 0:guest,1:user(不能选),2:adminstrator usri1_home_dir:=nil; usri1_comment:=nil; //注释 usri1_flags:=515; usri1_script_path:=nil; end; NetUserSetInfo(nil,StringToOleStr(edit1.text),1,pointer(buf),0); freemem(buf); end;
procedure TForm1.Button7Click(Sender: TObject); var buf:buffer2; i:longint; begin getmem(buf,sizeof(LOCALGROUP_MEMBERS_INFO_3)); with buf^ do begin lgrmi3_domainandname:=StringToOleStr(GetComputerNameD+'\'+edit1.text); end; i:=NetLocalGroupaddMembers(nil,StringToOleStr(edit4.text),3,pointer(buf),1); freemem(buf); end;
procedure TForm1.Button8Click(Sender: TObject); var EntiesRead: DWORD; TotalEntries: DWORD; UserInfo: buffer; lpBuffer: Pointer; ResumeHandle: DWord; Counter: Integer; NetApiStatus: LongWord; begin ResumeHandle := 0; repeat // NetApiStatus := NetUserEnum(PChar('\\NT-Domain'), 1, 0, lpBuffer, 0,EntiesRead, TotalEntries, ResumeHandle); NetApiStatus := NetUserEnum(nil, 1, 0, lpBuffer, 0, EntiesRead, TotalEntries, ResumeHandle); UserInfo := lpBuffer; for Counter := 0 to EntiesRead - 1 do begin listbox1.items.add(WideCharToString(UserInfo^.usri1_name)+'-->'+WideCharToString(UserInfo^.usri1_comment)+'-->'+floattostr(UserInfo^.usri1_priv)+'-->'+floattostr(UserInfo^.usri1_flags)); Inc(UserInfo); end; NetApiBufferFree(lpBuffer); //frees the memory that NetApiBufferAllocate allocates until (NetApiStatus <> ERROR_MORE_DATA); end;
procedure TForm1.Button6Click(Sender: TObject); var buf:buffer2; i:longint; begin getmem(buf,sizeof(LOCALGROUP_MEMBERS_INFO_3)); with buf^ do begin lgrmi3_domainandname:=StringToOleStr(GetComputerNameD+'\'+edit1.text); end; i:=NetLocalGroupdelMembers(nil,StringToOleStr(edit4.text),3,pointer(buf),1); freemem(buf); end;
end.
|
请发表评论