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

How to Fetch Domain Name in Delphi

226 views
Skip to first unread message

Ken faber

unread,
Dec 2, 2005, 10:56:04 AM12/2/05
to
I'm using Delphi 5 and 6.

Is there a way I can determine the name of the Domain a user is logged into,
if any?

Thanks,

-Ken


Liz

unread,
Dec 2, 2005, 10:06:55 AM12/2/05
to
Ken faber wrote:

> I'm using Delphi 5 and 6.
>
> Is there a way I can determine the name of the Domain a user is
> logged into, if any?

if they are logged into the domain

USERDOMAIN from the environment will contain the domain name

or

http://www.swissdelphicenter.ch/torry/showcode.php?id=2111

--
Liz the Brit
Delphi things I have released: http://www.xcalibur.co.uk/DelphiThings

Liz

unread,
Dec 2, 2005, 3:29:30 PM12/2/05
to
Ken faber wrote:

> I'm having trouble getting this logic to work - it gives an access
> violation on the GetEnvironmentVariable call.

Did you paste the rest of the code off the page or just the relevant
looking bit??

Its certainly working for me.

Ken faber

unread,
Dec 2, 2005, 4:12:34 PM12/2/05
to
Thanks Liz,

I'm having trouble getting this logic to work - it gives an access violation
on the GetEnvironmentVariable call.

function GetDomainName : string;
var
Env_VarName : PChar;
Env_Answer : PChar;
nSize : dword;
ResCode : integer;
begin
GetMem(Env_VarName,15);
Env_VarName := 'USERDOMAIN'+#0;
nSize := 255;
GetMem(Env_Answer,nSize);
Env_Answer := #0;
resCode := GetEnvironmentVariable(Env_VarName,Env_Answer,nSize);
if rescode = 0 then
ShowMessage('No Domain Name')
else
ShowMessage('Your Domain Name is=' + StrPas(Env_Answer));
result := StrPas(Env_Answer);
freemem(Env_VarName);
freemem(Env_Answer);
end;

"Liz" <liz_want...@xcalibur.nospam.co.uk> wrote in message
news:xn0eailt...@newsgroups.borland.com...

Ben Hochstrasser

unread,
Dec 2, 2005, 5:01:40 PM12/2/05
to
Ken faber wrote:

> Is there a way I can determine the name of the Domain a user is logged
> into, if any?

NetWkstaGetInfo() is the API way to do it. Else use USERDOMAIN and/or
USERDNSDOMAIN from the environment.

--
Ben

Ben Hochstrasser

unread,
Dec 2, 2005, 5:16:45 PM12/2/05
to
Ken faber wrote:

> I'm having trouble getting this logic to work - it gives an access
> violation on the GetEnvironmentVariable call.

function GetEnvVar(AValue: String): String;
var
n: integer;
begin
SetLength(Result, 256);
n := GetEnvironmentVariable(PChar(AValue), PChar(Result), Length(Result));
if n > Length(Result) then // oops, our buffer was too small
begin
SetLength(Result, n);
n := GetEnvironmentVariable(PChar(AValue), PChar(Result), Length(Result));
end;
SetLength(Result, n);
end;

--
Ben

Ken faber

unread,
Dec 5, 2005, 4:06:12 PM12/5/05
to
Thanks Ben,

For some reason, your version works fine.

-Ken

"Ben Hochstrasser" <bhoc@tiscali123^H^H^H.ch> wrote in message
news:Xns9720EC...@207.105.83.66...

Ben Hochstrasser

unread,
Dec 5, 2005, 5:48:41 PM12/5/05
to
Ken faber wrote:

> For some reason, your version works fine.

You're welcome.

For some reason, I don't like the construct you were using:

GetMem(Env_VarName,15);
Env_VarName := 'USERDOMAIN'+#0;
nSize := 255;
GetMem(Env_Answer,nSize);
Env_Answer := #0;

You set the length of a PChar, assign a constant to it. I'd assume that
this would reset the PChar variable, so that you'd end up eg. with an
Env_Answer variable of size 1 (instead of 255).

Maybe Rudy (the King of PChars) can elaborate?

--
Ben

0 new messages