Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GetUserName

90 views
Skip to first unread message

guit...@excite.com

unread,
Nov 10, 1999, 3:00:00 AM11/10/99
to
I have an application that runs as a Windows NT Service. The NT Service
is set to run as the 'SYSTEM' account. From my NT Service, how can I
retrieve the currently logged-on username? Calling GetUserName results
in 'SYSTEM'. I want to be able to see the correct username, such as
'administrator', 'guest', 'richie', etc.

Gordon Waters

unread,
Nov 11, 1999, 3:00:00 AM11/11/99
to

Here is a function I use to do this.


----------------------------------
Function GetUser : string;
Var
buffer : String;
buffsize : DWORD;
Begin
buffsize := 128;
Setlength(buffer,buffsize);
Getusername(Pchar(buffer),buffsize);
result := buffer;
end;

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

Showmessage(Getuser);

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

"guit...@excite.com" wrote:

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

Gordon Waters
Systems Support Voice: +61 2 62802393
Information Technology Fax: +61 2 62805420
The Canberra Times
9 Pirie St
Fyshwick ACT 2609

gordon...@canberratimes.com.au

http://www.canberratimes.com.au
------------------------------------

Anders LEE

unread,
Nov 11, 1999, 3:00:00 AM11/11/99
to
On Thu, 11 Nov 1999 10:37:53 +1100, Gordon Waters
<Gordon...@canberratimes.com.au> wrote:

>
>Here is a function I use to do this.
>
>
>----------------------------------
>Function GetUser : string;
> Var
> buffer : String;
> buffsize : DWORD;
> Begin
> buffsize := 128;
> Setlength(buffer,buffsize);
> Getusername(Pchar(buffer),buffsize);
> result := buffer;
>end;

Be careful with the string it returns. Say if the user is "Peter",
to display it, you get "Peter" but the string that holds it is
actually
"Peter"#0#0#0#0#0#0#0#0# ......

Michael Wong

unread,
Nov 11, 1999, 3:00:00 AM11/11/99
to

Anders LEE <and...@aelhk.com> wrote in message
news:382c5c2c...@forums.borland.com...

couldn't you use the Trim function?

...
result:=trim(buffer);

José Antonio Mesa

unread,
Nov 11, 1999, 3:00:00 AM11/11/99
to
I beleive it does not solve Gordon's problem, GetUserName() retrieve the
user of the calling thread not the logged in user name, unless, of couse,
the
calling thread was started by the logged in user.

I'm also interested in the right answer, I have almost the same problem.

About the function to retrive the user name, i'd suggest the following:

Function GetUser : string;
Var
buffer : array[0..256] of char;
i: DWORD;
begin
i:=256;
if Getusername(buffer, i) then
result := String(buffer)
else
result:='';
end;

Gordon Waters <Gordon...@canberratimes.com.au> escribió en el mensaje de
noticias 382A01D1...@canberratimes.com.au...


>
> Here is a function I use to do this.
>
>
> ----------------------------------
> Function GetUser : string;
> Var
> buffer : String;
> buffsize : DWORD;
> Begin
> buffsize := 128;
> Setlength(buffer,buffsize);
> Getusername(Pchar(buffer),buffsize);
> result := buffer;
> end;
>

vn...@my-deja.com

unread,
Nov 11, 1999, 3:00:00 AM11/11/99
to
No, the Trim() function won't work - it doesn't
trim #0's (ie., null characters); Trim() was my
first thought though.

My version of GetUser looks like:

function MyGetUserName: string;
var
sBuffer: string;
dwBuffSize: DWORD;
begin
Result := '';
dwBuffSize := 256;
SetLength(sBuffer,dwBuffSize);
if GetUserName(PChar(sBuffer),dwBuffSize) then
begin
//Resize the string to remove the trailing
//#0's
SetLength(sBuffer,(dwBufferSize - 1));
Result := sBuffer;
end;
end;


Vince Noga


In article <80eg21$3d...@forums.borland.com>,


"Michael Wong" <illus...@earthlink.net> wrote:
>
> Anders LEE <and...@aelhk.com> wrote in message
> news:382c5c2c...@forums.borland.com...
> > On Thu, 11 Nov 1999 10:37:53 +1100, Gordon
Waters
> > <Gordon...@canberratimes.com.au> wrote:
> >
> > >

> > >Here is a function I use to do this.
> > >
> > >
> > >----------------------------------
> > >Function GetUser : string;
> > > Var
> > > buffer : String;
> > > buffsize : DWORD;
> > > Begin
> > > buffsize := 128;
> > > Setlength(buffer,buffsize);
> > > Getusername(Pchar(buffer),buffsize);
> > > result := buffer;
> > >end;
> >

> > Be careful with the string it returns. Say
if the user is "Peter",
> > to display it, you get "Peter" but the string
that holds it is
> > actually
> > "Peter"#0#0#0#0#0#0#0#0# ......
> >
> >
>
> couldn't you use the Trim function?
>
> ...
> result:=trim(buffer);
>
>

Sent via Deja.com http://www.deja.com/
Before you buy.

Colin Wilson

unread,
Nov 15, 1999, 3:00:00 AM11/15/99
to

> I have an application that runs as a Windows NT Service. The NT Service
> is set to run as the 'SYSTEM' account. From my NT Service, how can I
> retrieve the currently logged-on username? Calling GetUserName results
> in 'SYSTEM'. I want to be able to see the correct username, such as

> 'administrator', 'guest', 'richie'.

There may be several 'users' logged on, especially if you have services
that run in their own accounts, or if your box provides DCOM servers, etc.

The best way to do it may be to see who's running explorer.exe.

First get a process handle for it. Maybe do
FindWindow/GetWindowThreadProcessId/OpenProcess.

Once you've got the process handle, use OpenProcessToken,
GetTokenInformation, LookupAccountSID, etc. to find out who owns it...


Colin
e-mail :co...@wilsonc.demon.co.uk
web: http://www.wilsonc.demon.co.uk/delphi.htm


Colin Wilson

unread,
Nov 18, 1999, 3:00:00 AM11/18/99
to
In article <VA.00000ae3.1b223d66@gbhstorewkss490>, Colin Wilson wrote:

> The best way to do it may be to see who's running explorer.exe.
>

.. Like this...

uses windows, psapi;

function GetExplorerUser : string;
var
explorerProcessHandle : THandle;
tokenhandle : THandle;
puser : PSidAndAttributes;
infoLen : DWORD;
userName, domainName : array [0..256] of char;
userNameLen, domainNameLen : DWORD;
use : Sid_Name_Use;

function OpenProcessHandle (const process : string) : THandle;
var
buffer, pid : PDWORD;
bufLen, cbNeeded : DWORD;
hp : THandle;
fileName : array [0..256] of char;
i : Integer;
begin
result := 0;
bufLen := 65536;
GetMem (buffer, bufLen);
try
if EnumProcesses (buffer, bufLen, cbNeeded) then
begin
pid := buffer;
for i := 0 to cbNeeded div sizeof (DWORD) - 1 do
begin
hp := OpenProcess (PROCESS_VM_READ or
PROCESS_QUERY_INFORMATION, False, pid^);
if hp <> 0 then
try
if (GetModuleBaseName (hp, 0, fileName, sizeof (fileName))
> 0) and
(CompareText (fileName, process) = 0) then
begin
result := hp;
break
end
finally
if result = 0 then
CloseHandle (hp)
end;

Inc (pid)
end
end
finally
FreeMem (buffer)
end
end;

begin
result := '';
explorerProcessHandle := OpenProcessHandle ('explorer.exe');
if explorerProcesshandle <> 0 then
try
if OpenProcessToken (explorerProcessHandle, TOKEN_QUERY,
tokenHandle) then
try
GetMem (puser, 65536);
try
if GetTokenInformation (tokenHandle, TokenUser, puser, 65536,
infoLen) then
begin
if LookupAccountSID (nil, puser^.Sid, userName, userNameLen,
domainName, domainNameLen, use) then
result := userName
end
finally
FreeMem (puser)
end
finally
CloseHandle (tokenHandle)
end
finally
CloseHandle (explorerProcessHandle)
end
end;

0 new messages