Vytvor si tento unit:
Kód:
unit CriaNTUser;
{
Cria usuario de NT
Copyright 1997 por Mauro Sant' Anna
Todos os direitos reservados
}
interface
uses
Windows;
// Create user
function CriaUsuario(
const XUsuario, XSenha, XComentario, XServidor: string): integer;
// Add to group
function AdicionaAoGrupo(const XServidor, XUsuario, XGrupo: string): integer;
// Remove from group
function EliminaDoGrupo(const XServidor, XUsuario, XGrupo: string): integer;
function ErrToMsg(const Err: integer): string;
implementation
type
T_USER_INFO_1 = record
usri1_name: PWideChar;
usri1_password: PWideChar;
usri1_password_age: integer;
usri1_priv: integer;
usri1_home_dir: PWideChar;
usri1_comment: PWideChar;
usri1_flags: integer;
usri1_script_path: PWideChar;
end;
PInteger = ^Integer;
function NetUserAdd(
servername: PWideChar;
level: integer;
const buf: T_USER_INFO_1;
parm_err: PInteger
): integer; stdcall; external 'netapi32.dll';
function NetGroupAddUser(servername: PWideChar;
GroupName: PWideChar;
username: PWideChar): integer; stdcall; external 'netapi32.dll';
function NetGroupDelUser(servername: PWideChar;
GroupName: PWideChar;
username: PWideChar): integer; stdcall; external 'netapi32.dll';
const
USER_PRIV_USER = 1;
UF_SCRIPT = 1;
NERR_BASE = 2100;
NERR_InvalidComputer = NERR_BASE + 251;
NERR_NotPrimary = NERR_BASE + 126;
NERR_GroupExists = NERR_BASE + 123;
NERR_UserExists = NERR_BASE + 124;
NERR_PasswordTooShort = NERR_BASE + 145;
NERR_SpeGroupOp = NERR_BASE+134; // The operation is not allowed on specified special groups, which are user groups, admin groups, local groups, or guest groups.
NERR_UserNotFound = NERR_BASE+121;// The user name could not be found.
NERR_GroupNotFound = NERR_BASE+120; // The group name could not be found.
NERR_UserNotInGroup = NERR_BASE+137; // The user does not belong to this group.
cTamStr = 256;
procedure StrToMB(const S: string; Buffer: PWideChar);
begin
fillchar(Buffer^, cTamStr * 2, 0);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, Pointer(S), length(S), Buffer, cTamStr);
end;
function ErrToMsg(const Err: integer): string;
begin
Result := 'Ok';
case Err of
ERROR_ACCESS_DENIED: Result := 'Access Denied';
NERR_InvalidComputer: Result := 'InvalidComputer';
NERR_NotPrimary: Result := 'NotPrimary';
NERR_GroupExists: Result := 'GroupExists';
NERR_UserExists: Result := 'UserExists';
NERR_PasswordTooShort: Result := 'PasswordTooShort';
NERR_SpeGroupOp: Result := 'The operation is not allowed on specified special groups, which are user groups, admin groups, local groups, or guest groups.';
NERR_UserNotFound: Result := 'The user name could not be found.';
NERR_GroupNotFound: Result := 'The group name could not be found.';
NERR_UserNotInGroup: Result := 'The user does not belong to this group.';
end;
end;
function CriaUsuario(
const XUsuario, XSenha, XComentario, XServidor: string): integer;
var
U: T_USER_INFO_1;
Err, Ret: integer;
Usuario, Senha, Comentario, Servidor: array[0..cTamStr] of widechar;
procedure Converte;
begin
StrToMB(XUsuario, Usuario);
StrToMB(XSenha, Senha);
StrToMB(XComentario, Comentario);
StrToMB(XServidor, Servidor);
end;
begin
Converte;
with U do
begin
usri1_name := Usuario;
usri1_password := Senha;
usri1_priv := USER_PRIV_USER;
usri1_home_dir := nil;
usri1_comment := 'Teste de cadastramento';
usri1_flags := UF_SCRIPT;
usri1_script_path := NIL;
end;
Ret := NetUserAdd(Servidor, 1, U, @Err);
Result := Ret;
end;
function AdicionaAoGrupo(const XServidor, XUsuario, XGrupo: string): integer;
var
Usuario, Servidor, Grupo: array[0..cTamStr] of widechar;
begin
StrToMB(XUsuario, Usuario);
StrToMB(XServidor, Servidor);
StrToMB(XGrupo, Grupo);
Result := NetGroupAddUser(Servidor, Grupo, Usuario);
end;
function EliminaDoGrupo(const XServidor, XUsuario, XGrupo: string): integer;
var
Usuario, Servidor, Grupo: array[0..cTamStr] of widechar;
begin
StrToMB(XUsuario, Usuario);
StrToMB(XServidor, Servidor);
StrToMB(XGrupo, Grupo);
Result := NetGroupDelUser(Servidor, Grupo, Usuario);
end;
end.
a pomocou zadefinovaných funkcií môžeš pracovať s užívateľskými účtami.
Btw, ak by si predsa len zvolil riešenie pomocou *.bat súboru, nemusíš imitovať dvojklik, existujú funkcie, ako
WinExec alebo
ShellExecute (pri použití tejto pripoj unit
ShellAPI), ktoré otvoria nejaký súbor.