🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2175

Getting computer sid, problems with LookupAccountName

#2175
From: Mike Collins
Date: Sun, 22 Apr 2007 21:32
83 lines
4370 bytes
Hi all, got a really strange problem.  Basically i have a piece of code that 
i wrote last year that retrieves the Computer's SID.  It is based on a 
article written by Mark Russinovich of System Internals, and loosely based 
on his psgetsid application.

The basic mechanics behind the code is that you get the computers name, 
append a $ to the end, the call LookupAccountName - which will return the 
SID for the account, beofre cponverting it to a string representation using 
ConvertSidToString.  The documentation about the appending of the $ is very 
vauge and i had never seen it before reading this article.  Anyway, the code 
i use was written sometime last year and seemed to work perfectly, i get 
exactly the same SID returned as i get with the psgetsid tool.

However, today i was working on something and started to walk throught the 
code (shown below).  I noticed that for some strange reason, the $ character 
was not being appended to the end of the computer name.  However, the code 
still seemed to work without issue.  I cannot for the life of me understand 
why the $ is not being appended and have posted on the .vlc.components.using 
newsgroup.  However, if i hard code in the name of my computer, with the $ 
at the end, LookupAccountName fails and reports

"No mapping between account names and security IDs was done"

Can anyone tell me where i'm going wrong here.  My code as it stands works - 
but doesn't execute as documented, and if i modify it so that it does 
conform, it fails to work.

Any help or pointers woudl be excellent.

{code snippet}

         // Generate computer SID hash
         AnsiString szComputerName;
         szComputerName.SetLength(MAX_COMPUTERNAME_LENGTH + 1);
         DWORD dwSize = szComputerName.Length();
         if (GetComputerName(szComputerName.c_str(), &dwSize))
         {
            szComputerName = szComputerName + "$";     <- Never gets 
appended...

            PSID pSID           = NULL;
            DWORD dwSIDSize     = 0;
            TCHAR cDomain[1024] = {0};
            DWORD dwDomainSize  = 1024;
            SID_NAME_USE AccountType;

            LookupAccountName(NULL, szComputerName.c_str(), NULL, 
&dwSIDSize, cDomain, &dwDomainSize, &AccountType);
            if (dwSIDSize)
            {
               pSID = (PSID)GlobalAlloc(GPTR, dwSIDSize);
               if (pSID)
               {
                  if (LookupAccountName(NULL, szComputerName.c_str(), pSID, 
&dwSIDSize, cDomain, &dwDomainSize, &AccountType))
                  {
                    char *cStringSID;
                     if (ConvertSidToStringSid(pSID, &cStringSID))
                     {
                     }
                     else
                        // ERROR: Calling ConvertSidToStringSid()");

                     LocalFree(cStringSID);
                     cStringSID = NULL;
                  }
                  else
                     // ERROR: Calling LookupAccountName() failed to 
retrieve account SID");
               }
               else
                  // ERROR: Calling GlobalAlloc() failed to allocate SID");

               GlobalFree(pSID);
               pSID = NULL;
            }
            else
               // ERROR: Calling LookupAccountName() failed to return 
account size");
         }
         else
            // ERROR: Calling GetComputerName() failed to return computer 
name");
Technical Details
Message-ID: <462bc454@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,bd7990a8e1a7b3b1,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Mike Collins" <its@TheBottomOfThePost>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Getting computer sid, problems with LookupAccountName
Date: Sun, 22 Apr 2007 21:32:26 -0000
Lines: 85
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.3790.3959
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.3959
NNTP-Posting-Host: 89.241.89.65
Message-ID: <462bc454@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1177273428 89.241.89.65 (22 Apr 2007 13:23:48 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail