Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From Novell to Windows Server

Status
Not open for further replies.

MagicFrisbee

Programmer
Feb 22, 2006
74
US
I have many in-house programs which read the currently-logged-in Novell user name to see if they have the necessary permissions to run in-house programs. If the username is empty or [Public], I know they are not logged in to the server and I can abort my programs before they even start.

But next year we'll be switching to Windows Server, complete with domains. How do I grab the username then? What's the name of the function or procedure? What goes in my uses clause, and what are the meanings of the parameters I would pass to the method?

Mind you, I don't need the user's password or even their complete domain--I just need the plain ol' username.

GIS Programmer
City of Orem, UT
 
I have a program I wrote for a laptop that's not always connected to the network (for obvious reasons--it's a laptop!). The function works whether or not the user is logged in to the network because it retrieves the Windows user name, not a network or Novell user name.

However, I don't know Windows Server very well. Apparently a Windows Server user can log in to any machine and not have to have a local Windows user account on that machine already, and Windows Server will let them through. If grabbing the user name is that simple, then yay! But I have to know if it guarantees the user is logged on to the network or not.


GIS Programmer
City of Orem, UT
 
I think the article will only return the windows username, which probably will not tell you if the user is logged into a local account or onto the network.

A windows computer can be part of a network either via a Domain, or through a Workgroup. There are several examples on how to tell if the computer is on a domain, such as:


not sure about checking if it is part of a workgroup.
 
I read at the article you linked, and although the function can tell me if the computer is part of a domain or not, it doesn't tell me the logged-in user name. Do I supply the internal method with the results of GetUserName? Will the function actually DO a rename of the machine if it IS part of a domain?

GIS Programmer
City of Orem, UT
 
I found the following code on Microsoft's site. It is in C and I translated it to Delphi:

Code:
{
  The following code was translated into Delphi from C by Roger Dunn.
  It was originally downloaded from the following URL:
  [URL unfurl="true"]http://support.microsoft.com/kb/111544[/URL]
}

//**********************************************************************
//
//  FUNCTION:     GetCurrentUserAndDomain - This function looks up
//                the user name and domain name for the user account
//                associated with the calling thread.
//
//  PARAMETERS:   szUser - a buffer that receives the user name
//                pcchUser - the size, in characters, of szUser
//                szDomain - a buffer that receives the domain name
//                pcchDomain - the size, in characters, of szDomain
//
//  RETURN VALUE: TRUE if the function succeeds. Otherwise, FALSE and
//                GetLastError() will return the failure reason.
//
//**********************************************************************

function GetCurrentUserAndDomain(out User, Domain: String): Boolean;
var
  pcchUser: DWORD;
  pcchDomain: DWORD;
  hToken: THandle;
  ptiUser: PTokenUser;
  cbti: DWORD;
  snu: SID_NAME_USE;
begin
  Result := False;
  hToken := 0;
  ptiUser := nil;
  cbti := 0;

  try
    // Get the calling thread's access token.
    if not OpenThreadToken( GetCurrentThread, TOKEN_QUERY, True, hToken ) then
    begin
      if GetLastError <> ERROR_NO_TOKEN then
        Exit;
      // Retry against process token if no thread token exists.
      if not OpenProcessToken( GetCurrentProcess, TOKEN_QUERY, hToken ) then
        Exit;
    end;

    // Obtain the size of the user information in the token.
    if GetTokenInformation( hToken, TokenUser, nil, 0, &cbti ) then
      // Call should have failed due to zero-length buffer.
      Exit
    else
      // Call should have failed due to zero-length buffer.
      if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
        Exit;

    // Allocate buffer for user information in the token.
    ptiUser := HeapAlloc( GetProcessHeap, 0, cbti );
    if ptiUser = nil then
      Exit;

    // Retrieve the user information from the token.
    if not GetTokenInformation( hToken, TokenUser, ptiUser, cbti, &cbti ) then
      Exit;

    {Allocate szUser and szDomain and their sizes}
    pcchUser := MAX_PROFILE_LEN;
    User := StringOfChar( #0, pcchUser );
    pcchDomain := MAX_COMPUTERNAME_LENGTH;
    Domain := StringOfChar( #0, pcchDomain );
    // Retrieve user name and domain name based on user's SID.
    if not LookupAccountSid( nil, ptiUser.User.Sid, PChar(User), pcchUser, PChar(Domain), pcchDomain, &snu ) then
      if GetLastError = ERROR_INSUFFICIENT_BUFFER then
      begin
        User := StringOfChar( #0, pcchUser );
        Domain := StringOfChar( #0, pcchDomain );
        if not LookupAccountSid( nil, ptiUser.User.Sid, PChar(User), pcchUser, PChar(Domain), pcchDomain, &snu ) then
          Exit;
      end
      else
        Exit;

    Result := True;

  finally
    // Free resources.
    if hToken <> 0 then
      CloseHandle( hToken );
    if ptiUser <> nil then
      HeapFree( GetProcessHeap, 0, ptiUser );
   end;
end;

So, the way you would use this function is:

Code:
procedure Button1Click(Sender: TObject);
var
  sUser, sDomain: String;
begin
  if GetCurrentUserAndDomain( sUser, sDomain ) then
    //do something with sUser and sDomain
  else
    //the function failed
end;

GIS Programmer
City of Orem, UT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top