NT - Using Delphi to Extract Username *AND* Full Name

Hi All,

I am writing an application that as part of the output prints the details of the user who printed it.

I have no problem getting the username (I.e. the string that the user has to type in before their pasword) but I would like to get the Full Name.

To explain:

Under NT if you fire up User Manager and add a new user there are 5 boxes you fill in:

1. Username  
  I can get this with:
    S := SizeOf(GetUserNameArray);
    GetUserName(GetUserNameArray, S);
    frmMain.txtUserName.Text := GetUserNameArray;

2. Full Name
3. Description
4. Pasword
5. Confirm Password

So, what I am after is some code (probably an API thing?) to get Full Name and Description of the currently logged in user.

Hope that makes sense.

Cheers

Ian
iwatkinsAsked:
Who is Participating?
 
MadshiConnect With a Mentor Commented:
Hi Ian,

do something like this:

function NetUserGetInfo(servername,username: PWideChar; level: dword; var buf: pointer) : dword; stdcall;
         external 'netapi32.dll' name 'NetUserGetInfo';
function NetApiBufferFree(buf: pointer) : dword; stdcall;
         external 'netapi32.dll' name 'NetApiBufferFree';

type TUserInfo1  = packed record
                     name         : PWideChar;
                     password     : PWideChar;
                     password_age : dword;
                     priv         : dword;
                     home_dir     : PWideChar;
                     comment      : PWideChar;
                     flags        : dword;
                     script_path  : PWideChar;
                   end;
     TPUserInfo1 = ^TUserInfo1;

function GetUserComment(servername,username: string) : string;
var ui1 : TPUserInfo1;
begin
  result:='?';
  if NetUserGetInfo(pwidechar(wideString(servername)),pwidechar(wideString(username)),1,pointer(ui1))=0 then
    try
      result:=wideString(ui1^.comment);
    finally NetApiBufferFree(ui1) end;
end;

Regards, Madshi.

0
 
MadshiCommented:
Simply call NetUserGetInfo with e.g. level 1.
If you have questions, please ask me...

Regards, Madshi.
0
 
iwatkinsAuthor Commented:
Madshi,

Found it!! I can read all about it in WIN32.hlp but I have tried using NetGetUserInfo in a Delphi program but the compiler complains that the:

'identifier NetUserGetInfo hasn't been declared'

Ok, must be in on of the rtl files that haven't been included in this app. But I have searched the full source tree only to not find the delphi wrapper to this function.

I have never come across this before, so how do I use this function directly ? Example code would be good.

Cheers

Ian
0
 
iwatkinsAuthor Commented:
Hi Madshi,

Perfect. Works a treat.

Also managed to get the Full name too, see code below.

Nice one, thanks for the help. Now I know how to access other win32 stuff not covered by the wrappers. Hence a few extras for you.

Cheers

Ian

----------------------

function NetUserGetInfo(servername,username: PWideChar; level: dword; var buf: pointer) : dword; stdcall;
         external 'netapi32.dll' name 'NetUserGetInfo';
function NetApiBufferFree(buf: pointer) : dword; stdcall;
         external 'netapi32.dll' name 'NetApiBufferFree';

type TUserInfo2  = packed record
                     name : PWideChar;
                     password : PWideChar;
                     password_age : dword;
                     priv : dword;
                     home_dir : PWideChar;
                     comment : PWideChar;
                     flags : dword;
                     script_path : PWideChar;
                     auth_flags : dword;
                     full_name : PWideChar;
                     usr_comment : PWideChar;
                     parms : PWideChar;
                     workstations : PWideChar;
                     last_logon : dword;
                     last_logoff : dword;
                     acct_expires : dword;
                     max_storage : dword;
                     units_per_week : dword;
                     logon_hours : dword;
                     bad_pw_count : dword;
                     num_logons : dword;
                     logon_server : PWideChar;
                     country_code : dword;
                     code_page : dword;
                   end;
     TPUserInfo2 = ^TUserInfo2;

function GetUserFullName(servername,username: string) : string;
var ui2 : TPUserInfo2;
begin
  result:='?';
  if NetUserGetInfo(pwidechar(wideString(servername)),pwidechar(wideString(username)),2,pointer(ui2))=0 then
    try
      result:=wideString(ui2^.full_name);
    finally NetApiBufferFree(ui2) end;
end;


0
 
MadshiCommented:
Thanx for the extras...   :-)
0
All Courses

From novice to tech pro — start learning today.