Board index » delphi » How to get a logged on users name to put it into a log file

How to get a logged on users name to put it into a log file

How can I get the name of the (windows-)user, that is actually logged on to
a machine.
I need this because I want to handle a logfile, that contains information
about the users that manipulates data of a paradox-database. My
paradox-application doesn't ask for a user explicitly.

thanks for information

Christian Bach

 

Re:How to get a logged on users name to put it into a log file


On Sun, 23 Jan 2000 16:35:35 +0100, "Christian Bach"

Quote
<acb...@01019frenet.de> wrote:
>How can I get the name of the (windows-)user, that is actually logged on to
>a machine.

Use the GetUserName function.

---
Yorai Aminov (TeamB)
http://ourworld.compuserve.com/homepages/yaminov
(TeamB cannot answer questions received via email.
To contact me for any other reason remove nospam from my address)

Re:How to get a logged on users name to put it into a log file


procedure TForm1.Button1Click(Sender: TObject);
var
  MyKey: HKey;                               {holds a registry key handle}
  ValueType: DWORD;                     {holds the key value type}
  MyData: array[0..255] of char;        {holds the string from the registry}
  dLength: DWORD;                         {holds the length of the returned
string}
begin                                                 {Procedure
Button1Click}
                                                          {Open the registry
key}
  if RegOpenKeyEx(HKEY_LOCAL_MACHINE,'NETWORK\LOGON',0,0,MyKey) =0 then
    begin                                             {if RegOpenKeyEx}
                                                          {Retrieve and
display directory information}
        dLength := SizeOf(MyData);
        RegQueryValueEx(MyKey,'UserName',nil,
@ValueType,@MyData[0],@dLength);
        StaticText1.Caption := MyData;
    end;                                                  {RegOpenkeyEx}
end;                                                        {end procedure
Button1Click}

Quote
Christian Bach <acb...@01019frenet.de> wrote in message

news:86f6n1$lf210@bornews.borland.com...
Quote
> How can I get the name of the (windows-)user, that is actually logged on
to
> a machine.
> I need this because I want to handle a logfile, that contains information
> about the users that manipulates data of a paradox-database. My
> paradox-application doesn't ask for a user explicitly.

> thanks for information

> Christian Bach

Re:How to get a logged on users name to put it into a log file


Here's one way of doing it. This also works with DBISAM etc.

function GetUserName: String;
var
   pcUser   : PChar;
   dwUSize : DWORD;
begin
   dwUSize := 21; // user name can be up to 20 characters
   GetMem(pcUser, dwUSize); // allocate memory for the string
   try
      if Windows.GetUserName( pcUser, dwUSize ) then
         Result := pcUser
   finally
      FreeMem(pcUser); // now free the memory allocated for the string
   end;
end;

Kai (MarSoft)

Quote
Christian Bach wrote in message <86f6n1$lf...@bornews.borland.com>...
>How can I get the name of the (windows-)user, that is actually logged on to
>a machine.
>I need this because I want to handle a logfile, that contains information
>about the users that manipulates data of a paradox-database. My
>paradox-application doesn't ask for a user explicitly.

>thanks for information

>Christian Bach

Other Threads