Is there a way I can determine the name of the Domain a user is logged into,
if any?
Thanks,
-Ken
> 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
> 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.
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...
> 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
> 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
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...
> 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