Grupuri Google nu mai acceptă postările sau abonamentele noi Usenet. Conținutul anterior este în continuare vizibil.

GetUserName

90 de afișări
Accesați primul mesaj necitit

guit...@excite.com

necitită,
10 nov. 1999, 03:00:0010.11.1999
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

necitită,
11 nov. 1999, 03:00:0011.11.1999

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

necitită,
11 nov. 1999, 03:00:0011.11.1999
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

necitită,
11 nov. 1999, 03:00:0011.11.1999

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

necitită,
11 nov. 1999, 03:00:0011.11.1999
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

necitită,
11 nov. 1999, 03:00:0011.11.1999
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

necitită,
15 nov. 1999, 03:00:0015.11.1999

> 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

necitită,
18 nov. 1999, 03:00:0018.11.1999
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 mesaje noi