关闭

用delphi增加/获得windows用户帐号

标签: delphiwindowsuserlistviewserverfunction
2045人阅读 评论(0) 收藏 举报
分类:

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;
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;
    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)
    ListView1: TListView;
    btnRefresh: TButton;
    procedure btnRefreshClick(Sender: TObject);
    procedure FormCreate(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';

procedure TForm1.btnRefreshClick(Sender: TObject);
var
  EntiesRead: DWORD;
  TotalEntries: DWORD;
  UserInfo:   lpUSER_INFO_1;
  lpBuffer: pointer;
  ResumeHandle: DWord;
  Counter: Integer;
  NetApiStatus: LongWord;
  TempListItem: TListItem;
  err: string;
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;

procedure TForm1.FormCreate(Sender: TObject);
var
  TempListColumn: TListColumn;
begin
  self.ListView1.ViewStyle := vsReport;
  self.ListView1.GridLines := true;
  TempListColumn := self.ListView1.Columns.Add;
  TempListColumn.Caption := '名称';
  TempListColumn.Width := 100;
  TempListColumn := self.ListView1.Columns.Add;
  TempListColumn.Caption := '描述';
  TempListColumn.Width := 300;
end;

 

end.

 

附:在delphi带的Win32 Developer's References查看USER_INFO_1 ,知道所用头文件是lmaccess.h,但是delphi并没有带这个文件,PB带了这个文件,里面确实有常数USER_PRIV_USER 的定义,下面的内容是从网上找到的。

const
SE_CREATE_TOKEN_NAME               ='SeCreateTokenPrivilege';
SE_ASSIGNPRIMARYTOKEN_NAME         ='SeAssignPrimaryTokenPrivilege';
SE_LOCK_MEMORY_NAME                ='SeLockMemoryPrivilege';
SE_INCREASE_QUOTA_NAME             ='SeIncreaseQuotaPrivilege';
SE_UNSOLICITED_INPUT_NAME          ='SeUnsolicitedInputPrivilege';
SE_MACHINE_ACCOUNT_NAME            ='SeMachineAccountPrivilege';
SE_TCB_NAME                        ='SeTcbPrivilege';
SE_SECURITY_NAME                   ='SeSecurityPrivilege';
SE_TAKE_OWNERSHIP_NAME             ='SeTakeOwnershipPrivilege';
SE_LOAD_DRIVER_NAME                ='SeLoadDriverPrivilege';
SE_SYSTEM_PROFILE_NAME             ='SeSystemProfilePrivilege';
SE_SYSTEMTIME_NAME                 ='SeSystemtimePrivilege';
SE_PROF_SINGLE_PROCESS_NAME        ='SeProfileSingleProcessPrivilege';
SE_INC_BASE_PRIORITY_NAME          ='SeIncreaseBasePriorityPrivilege';
SE_CREATE_PAGEFILE_NAME            ='SeCreatePagefilePrivilege';
SE_CREATE_PERMANENT_NAME           ='SeCreatePermanentPrivilege';
SE_BACKUP_NAME                     ='SeBackupPrivilege';
SE_RESTORE_NAME                    ='SeRestorePrivilege';
SE_SHUTDOWN_NAME                   ='SeShutdownPrivilege';
SE_DEBUG_NAME                      ='SeDebugPrivilege';
SE_AUDIT_NAME                      ='SeAuditPrivilege';
SE_SYSTEM_ENVIRONMENT_NAME         ='SeSystemEnvironmentPrivilege';
SE_CHANGE_NOTIFY_NAME              ='SeChangeNotifyPrivilege';
SE_REMOTE_SHUTDOWN_NAME            ='SeRemoteShutdownPrivilege';

USER_PRIV_MASK      =$3;
USER_PRIV_GUEST     =0;
USER_PRIV_USER      =1;
USER_PRIV_ADMIN     =2;

UF_SCRIPT                =$0001;
UF_ACCOUNTDISABLE        =$0002;
UF_HOMEDIR_REQUIRED      =$0008;
UF_LOCKOUT               =$0010;
UF_PASSWD_NOTREQD        =$0020;
UF_PASSWD_CANT_CHANGE    =$0040;

//
// Account type bits as part of usri_flags.
//

UF_TEMP_DUPLICATE_ACCOUNT        =$0100;
UF_NORMAL_ACCOUNT                =$0200;
UF_INTERDOMAIN_TRUST_ACCOUNT     =$0800;
UF_WORKSTATION_TRUST_ACCOUNT     =$1000;
UF_SERVER_TRUST_ACCOUNT          =$2000;

UF_MACHINE_ACCOUNT_MASK =( UF_INTERDOMAIN_TRUST_ACCOUNT  or
                                  UF_WORKSTATION_TRUST_ACCOUNT  or
                                  UF_SERVER_TRUST_ACCOUNT );

UF_ACCOUNT_TYPE_MASK         =(
                    UF_TEMP_DUPLICATE_ACCOUNT  or
                    UF_NORMAL_ACCOUNT  or
                    UF_INTERDOMAIN_TRUST_ACCOUNT  or
                    UF_WORKSTATION_TRUST_ACCOUNT  or
                    UF_SERVER_TRUST_ACCOUNT
                );

UF_DONT_EXPIRE_PASSWD            =$10000;
UF_MNS_LOGON_ACCOUNT             =$20000;


UF_SETTABLE_BITS        =(
                    UF_SCRIPT  or
                    UF_ACCOUNTDISABLE  or
                    UF_LOCKOUT  or
                    UF_HOMEDIR_REQUIRED   or
                    UF_PASSWD_NOTREQD  or
                    UF_PASSWD_CANT_CHANGE  or  
                    UF_ACCOUNT_TYPE_MASK  or
                    UF_DONT_EXPIRE_PASSWD  or
                    UF_MNS_LOGON_ACCOUNT
                );


type
  _USER_INFO_1 = record
    usri1_name:             PWideChar;
    usri1_password:         PWideChar;
    usri1_password_age:     Cardinal;
    usri1_priv:             Cardinal;
    usri1_home_dir:         PWideChar;
    usri1_comment:          PWideChar;
    usri1_flags:            Cardinal;
    usri1_script_path:      PWideChar;
  end;

  _LOCALGROUP_MEMBERS_INFO_0 = record
    lgrmi0_sid: Cardinal;
  end;
  _LOCALGROUP_MEMBERS_INFO_3 = record
    lgrmi3_domainandname: PWideChar;
  end;

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:387354次
    • 积分:5912
    • 等级:
    • 排名:第4508名
    • 原创:145篇
    • 转载:63篇
    • 译文:11篇
    • 评论:56条
    文章分类
    最新评论