BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Getting computer sid, problems with LookupAccountName

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Mike Collins
Guest





PostPosted: Mon Apr 23, 2007 2:32 am    Post subject: Getting computer sid, problems with LookupAccountName Reply with quote



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");
Back to top
Jyrki Holopainen
Guest





PostPosted: Thu Apr 26, 2007 8:10 am    Post subject: Re: Getting computer sid, problems with LookupAccountName Reply with quote



Mike Collins wrote:

Quote:
// Generate computer SID hash
AnsiString szComputerName;
szComputerName.SetLength(MAX_COMPUTERNAME_LENGTH + 1);
DWORD dwSize = szComputerName.Length();
if (GetComputerName(szComputerName.c_str(), &dwSize))

This is probably not safe. You must use a separate char buffer as a
parameter to GetComputerName() and assign that to the szComputerName
after the function call.
Back to top
Bob Gonder
Guest





PostPosted: Thu Apr 26, 2007 6:39 pm    Post subject: Re: Getting computer sid, problems with LookupAccountName Reply with quote



Mike Collins wrote:

Quote:
AnsiString szComputerName;
szComputerName.SetLength(MAX_COMPUTERNAME_LENGTH + 1);
DWORD dwSize = szComputerName.Length();
if (GetComputerName(szComputerName.c_str(), &dwSize))
{
szComputerName = szComputerName + "$"; <- Never gets appended...

szComputerName is still MAX_COMPUTERNAME_LENGTH + 1in size, now you
are adding "$" at position MAX_COMPUTERNAME_LENGTH + 2
Back to top
Mike Collins
Guest





PostPosted: Fri Apr 27, 2007 6:22 pm    Post subject: Re: Getting computer sid, problems with LookupAccountName Reply with quote

hay guys thanks for the pointers. As it happend, this bit has nothing to do
with it. If i manually / hard code my computer name with the $ the
LookupAccount function fails - it appears it is not as the documentation
states - you simply pass the computer name and you get the system works - no
appending needed.

Thanks for the help,

Mike C

"Bob Gonder" <notbg (AT) notmindspring (DOT) invalid> wrote in message
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.