Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Windows NT Domains and Local System Account

Posted on 2003-11-23
2
596 Views
Last Modified: 2012-05-04
Greetings


Does anyone know how I can make a clear destinction between a user that logged into an NT domain and one that logs unto a local system account.

With my Microsoft client, I have the option to either log onto the domain or log in with a local system account.

I would like some code that will indicate whether I am on the domain or a local account.

Any pointers or samples will be greatly appreciated.

Thanks in advance.
 Engwi
0
Comment
Question by:Engwi
2 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 50 total points
ID: 9809761
Hi,

There should be an environment variable USERDOMAIN that shows the domain name (or local computer name) you are currently logged on. Get it like:

function GetEnvStr(EnvName: AnsiString): AnsiString;
var
  Buffer: Array[1..256] of Char;
begin
  EnvName:='%'+EnvName+'%'+#0;
  ExpandEnvironmentStrings(@EnvName[1],@Buffer,SizeOf(Buffer));
  Result:=StrPas(@Buffer);
  if (Result+#0=EnvName) then Result:='';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetEnvStr('USERDOMAIN'));
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9810388
The best way would be by using GetTokenInformation to get the user token and then use LookupAccountSid to convert the token to a username/domain name pair. Something like:

var
  hAccessToken: THandle;
  UserToken: PSIDAndAttributes;
  InfoBufferSize: DWORD;
  dwInfoBufferSize: DWORD;
  vName, vDomain: PChar;
  cbName, cbDomain: DWORD;
  peUse: SID_NAME_USE;
begin
  if OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, hAccessToken) then begin
    UserToken := nil;
    GetTokenInformation(hAccessToken, TokenUser, UserToken, 0, InfoBufferSize);
    GetMem(UserToken, InfoBufferSize);
    if GetTokenInformation(hAccessToken, TokenUser, UserToken, InfoBufferSize, dwInfoBufferSize) then begin
      cbName := 0;
      cbDomain := 0;
      vName := nil;
      vDomain := nil;
      LookupAccountSid(nil, SID, vName, cbName, vDomain, cbDomain, peUse);
      LastError := GetLastError;
      if (LastError = ERROR_INSUFFICIENT_BUFFER) then begin
        GetMem(vName, cbName);
        GetMem(vDomain, cbDomain);
        if LookupAccountSid(nil, SID, vName, cbName, vDomain, cbDomain, peUse) then begin
        WriteLn(Log, 'User ', UserName, ' (', DisplayName, ') on domain ', Domain, '.');
      end;
    end;
  end;
end;

LookupAccountSid provides three types of information. The username and length of the username, the domain name and the length of the domain name and finally, the type of the account. Now, the domain name can be equal to the computer name, in which case it's a local account. Or it is the name of the domain.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
error 1.1 400 Bad request idhttp delphi 18 140
Mobile Keyboard covers the display of the TMemo 3 95
Performance of SQL statement 37 127
Dynamically Created Query 3 62
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question