Delphi 增加/获得windows用户帐号
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
USER_INFO_1 = record
usri1_name: LPWSTR;
usri1_password: LPWSTR;
usri1_password_age: DWORD;
usri1_priv: DWORD;
usri1_home_dir: LPWSTR;
usri1_comment: LPWSTR;
usri1_flags: DWORD;
usri1_script_path: LPWSTR;
end;
lpUSER_INFO_1 = ^USER_INFO_1;
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';
type
TForm1 = class(TForm)
Button1: TButton;
ListView1: TListView;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
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';
procedure TForm1.Button1Click(Sender: TObject);
var
EntiesRead: DWORD;
TotalEntries: DWORD;
UserInfo: lpUSER_INFO_1;
lpBuffer: pointer;
ResumeHandle: DWord;
Counter: Integer;
NetApiStatus: LongWord;
TempListItem: TListItem;
begin
//增加用户
// getmem(UserInfo, sizeof(USER_INFO_1));
//
// with UserInfo^ do
// begin
// usri1_name := 'Gao168';
// usri1_password := '610110';
// usri1_password_age := 0;
// usri1_priv := 1;
// usri1_home_dir := nil;
// usri1_comment := nil;
// usri1_flags := 1;
// usri1_script_path := nil;
// end;
//
// NetApiStatus := NetUserAdd(nil, 1, pointer(UserInfo), 0);
// freemem(UserInfo);
//获得用户
self.ListView1.Items.Clear;
ResumeHandle := 0;
repeat
NetApiStatus := NetUserEnum(nil, 1, 0, lpBuffer, 0, EntiesRead,
TotalEntries, ResumeHandle);
UserInfo := lpBuffer;
for Counter := 0 to EntiesRead - 1 do
begin
TempListItem := self.ListView1.Items.Add;
TempListItem.Caption := WideCharToString(UserInfo^.usri1_name);
TempListItem.SubItems.Add(WideCharToString(UserInfo^.usri1_comment));
Inc(UserInfo);
end;
NetApiBufferFree(lpBuffer);
until (NetApiStatus <> ERROR_MORE_DATA);
end;
posted on 2020-03-16 15:52 windsonvip 阅读(333) 评论(0) 编辑 收藏 举报