Board index » delphi » Domain name in WinNT/2000

Domain name in WinNT/2000

Hello,

My task is to secure the program to make it working in the specified Windows
NT domain only.
The question is: how can I get the name of domain my computer is logged to
(using Delphi 6.0 or 5.0).
Thank you in advance for your help.

Best regards,
Przemyslaw Czaplinski

 

Re:Domain name in WinNT/2000


Quote
> The question is: how can I get the name of domain my computer is logged to
> (using Delphi 6.0 or 5.0).

Have a look at the API function LookupAccountName.
The return in parameter ReferencedDomainName is what you need...

Hth,
Matthias.

var Sid:PSid;
    cbSid:DWord;
    ReferencedDomainName:array[0..255] of char;
    cbReferencedDomainName:DWord;
    peUse:DWord;
    myIA:TSIDIdentifierAuthority;
    PmyIA:PSIDIdentifierAuthority;
begin
     FillChar(myIA,SizeOf(myIA),0);
     myIA.Value[0]:=1;
     myIA.Value[1]:=1;
     myIA.Value[2]:=1;
     myIA.Value[3]:=1;
     myIA.Value[4]:=1;
     myIA.Value[5]:=1;
     PmyIA:=@myIA;
     if not AllocateAndInitializeSid( myIA,6,
                                      0,0,0,0,0,0,0,0,
                                      Sid)
     then begin
        {... an error occured...}
         exit;
     end;
     cbSid:=getLengthSID(Sid);
     cbReferencedDomainName:=SizeOf(ReferencedDomainName);
     peUse:=SidTypeUser;
     if LookupAccountName(
                          nil,
                          'currentUser',
                          Sid,
                          cbSid,
                          ReferencedDomainName,
                          cbReferencedDomainName,
                          peUse
     ) then begin
        {...}
    end;

Re:Domain name in WinNT/2000


Ok. Such function is working fine (I have tested it putting my username
intead of  'currentUser'), but - there is still one problem.
I have to know the exact CURRENT USER name ('currentUser').
And the program has to run for all users within the domain (any computer,
any user from this domain).
Do you maybe know how I can get then the current user name so?

Best regards,
Przemyslaw Czaplinski

Quote
"Matthias Thiel" <matthias_th...@sj.com> wrote in message

news:9lion1$ghb$1@crusher.de.colt.net...
Quote
> > The question is: how can I get the name of domain my computer is logged
to
> > (using Delphi 6.0 or 5.0).

> Have a look at the API function LookupAccountName.
> The return in parameter ReferencedDomainName is what you need...

> Hth,
> Matthias.

> var Sid:PSid;
>     cbSid:DWord;
>     ReferencedDomainName:array[0..255] of char;
>     cbReferencedDomainName:DWord;
>     peUse:DWord;
>     myIA:TSIDIdentifierAuthority;
>     PmyIA:PSIDIdentifierAuthority;
> begin
>      FillChar(myIA,SizeOf(myIA),0);
>      myIA.Value[0]:=1;
>      myIA.Value[1]:=1;
>      myIA.Value[2]:=1;
>      myIA.Value[3]:=1;
>      myIA.Value[4]:=1;
>      myIA.Value[5]:=1;
>      PmyIA:=@myIA;
>      if not AllocateAndInitializeSid( myIA,6,
>                                       0,0,0,0,0,0,0,0,
>                                       Sid)
>      then begin
>         {... an error occured...}
>          exit;
>      end;
>      cbSid:=getLengthSID(Sid);
>      cbReferencedDomainName:=SizeOf(ReferencedDomainName);
>      peUse:=SidTypeUser;
>      if LookupAccountName(
>                           nil,
>                           'currentUser',
>                           Sid,
>                           cbSid,
>                           ReferencedDomainName,
>                           cbReferencedDomainName,
>                           peUse
>      ) then begin
>         {...}
>     end;

Re:Domain name in WinNT/2000


Quote
>"Przemyslaw Czaplinski" <ge...@thenut.eti.pg.gda.pl> schrieb im Newsbeitrag news:9lj2o2$9nd$1@news.tpi.pl...
> Do you maybe know how I can get then the current user name so?

currentUser was only a placeholder for ... the current user...

You can use the second function below to get the name:
(the second function just formats a possible error message)

function getFormattedErrorMessage(MessageID:DWord):string;
var aBuff:array[0..1000] of char;
    tmpResult:string;
begin
     tmpResult:='unknown error ';
     if (FormatMessage(
       FORMAT_MESSAGE_FROM_SYSTEM,
       nil,
       MessageID,
       0,
       @aBuff,
       SizeOf(aBuff),
       nil
       )<>0)
     then begin
          tmpResult:=Strpas(aBuff);
          if tmpResult[length(tmpResult)] in [#13,#10] then tmpResult:=copy(tmpResult,1,length(tmpResult)-1);
          if tmpResult[length(tmpResult)] in [#13,#10] then tmpResult:=copy(tmpResult,1,length(tmpResult)-1);
     end;
     Result:=tmpResult+' [#'+IntToStr(MessageID)+']';
end;

{This one:}

function getUserName:string;
var UserName:PChar;
    NameLength:dWord;
begin
     UserName:=StrAlloc(80);
     NameLength:=dWord(StrBufSize(UserName));
     if WNetGetUser('',UserName,Namelength)=NO_ERROR then
        result:=StrPas(UserName)
     else
         result:=getFormattedErrorMessage(GetLastError);
     StrDispose(UserName);
end;

You need to include the units Windows and SysUtils in your uses clause.

Hth,
Matthias.

Re:Domain name in WinNT/2000


Quote
>"Przemyslaw Czaplinski" <ge...@thenut.eti.pg.gda.pl> schrieb im Newsbeitrag news:9lj2o2$9nd$1@news.tpi.pl...
> Do you maybe know how I can get then the current user name so?

currentUser was only a placeholder for ... the current user...

You can use the second function below to get the name:
(the first function just formats a possible error message)

function getFormattedErrorMessage(MessageID:DWord):string;
var aBuff:array[0..1000] of char;
    tmpResult:string;
begin
     tmpResult:='unknown error ';
     if (FormatMessage(
       FORMAT_MESSAGE_FROM_SYSTEM,
       nil,
       MessageID,
       0,
       @aBuff,
       SizeOf(aBuff),
       nil
       )<>0)
     then begin
          tmpResult:=Strpas(aBuff);
          if tmpResult[length(tmpResult)] in [#13,#10] then tmpResult:=copy(tmpResult,1,length(tmpResult)-1);
          if tmpResult[length(tmpResult)] in [#13,#10] then tmpResult:=copy(tmpResult,1,length(tmpResult)-1);
     end;
     Result:=tmpResult+' [#'+IntToStr(MessageID)+']';
end;

{This one:}

function getUserName:string;
var UserName:PChar;
    NameLength:dWord;
begin
     UserName:=StrAlloc(80);
     NameLength:=dWord(StrBufSize(UserName));
     if WNetGetUser('',UserName,Namelength)=NO_ERROR then
        result:=StrPas(UserName)
     else
         result:=getFormattedErrorMessage(GetLastError);
     StrDispose(UserName);
end;

You need to include the units Windows and SysUtils in your uses clause.

Hth,
Matthias.

Other Threads