Thread View: borland.public.cppbuilder.nativeapi
5 messages
5 total messages
Started by mataus
Wed, 23 Jan 2008 23:55
Get user SID with LookupAccountName
Author: mataus
Date: Wed, 23 Jan 2008 23:55
Date: Wed, 23 Jan 2008 23:55
9 lines
1117 bytes
1117 bytes
Helo everyone. I have problem - I nned to get a users SID (as text) where i have username. I no i use LookupAccountName api but i am confussed with the syntax - does anyone have code to do this? Many thanks Mataus
Re: Get user SID with LookupAccountName
Author: Remy Lebeau (TeamB)
Date: Thu, 24 Jan 2008 00:54
Date: Thu, 24 Jan 2008 00:54
106 lines
4508 bytes
4508 bytes
"mataus" <mataus@gmail.com> wrote in message
news:4797d1c8@newsgroups.borland.com...
> I nned to get a users SID (as text) where i have username. I no
> i use LookupAccountName api but i am confussed with the
> syntax - does anyone have code to do this?
BOOL GetTextualSid(PSID pSid, LPTSTR TextualSid, LPDWORD lpdwBufferLen)
{
if( !IsValidSid(pSid) )
return FALSE;
PSID_IDENTIFIER_AUTHORITY psia = GetSidIdentifierAuthority(pSid);
DWORD dwSubAuthorities = *GetSidSubAuthorityCount(pSid);
DWORD dwSidSize = (15 + 12 + (12 * dwSubAuthorities) + 1) *
sizeof(TCHAR);
if( *lpdwBufferLen < dwSidSize )
{
*lpdwBufferLen = dwSidSize;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
DWORD dwSidRev = SID_REVISION;
dwSidSize = wsprintf(TextualSid, TEXT("S-%lu-"), dwSidRev);
if( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
{
dwSidSize += wsprintf(TextualSid + lstrlen(TextualSid),
TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
(USHORT) psia->Value[0],
(USHORT) psia->Value[1],
(USHORT) psia->Value[2],
(USHORT) psia->Value[3],
(USHORT) psia->Value[4],
(USHORT) psia->Value[5]);
}
else
{
dwSidSize += wsprintf(TextualSid + lstrlen(TextualSid),
TEXT("%lu"),
(ULONG) (psia->Value[5] ) +
(ULONG) (psia->Value[4] << 8) +
(ULONG) (psia->Value[3] << 16) +
(ULONG) (psia->Value[2] << 24) );
}
for (DWORD dwCounter = 0; dwCounter < dwSubAuthorities; ++dwCounter)
{
dwSidSize += wsprintf(TextualSid + dwSidSize,
TEXT("-%lu"),
*GetSidSubAuthority(pSid, dwCounter));
}
return TRUE;
}
void UserNameToSid(LPTSTR UserName)
{
DWORD dwSidSize = 0;
TCHAR szDomain[128] = {0};
DWORD dwDomainSize = 128;
SID_NAME_USE SidType;
LookupAccountName(NULL, UserName, NULL, &dwSidSize, szDomain,
&dwDomainSize, &SidType);
if( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
{
PSID pSid = (PSID) LocalAlloc(LMEM_FIXED, dwSidSize);
if( pSid )
{
ZeroMemory(szDomain, sizeof(szDomain));
dwDomainSize = 128;
if( LookupAccountName(NULL, UserName, pSid, &dwSidSize,
szDomain, &dwDomainSize, &SidType) )
{
dwSidSize = 0;
GetTextualSid(pSid, NULL, &dwSidSize);
if( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
{
LPTSTR lpSidStr = (LPTSTR) LocalAlloc(LMEM_FIXED,
dwSidSize);
if( lpSidStr )
{
GetTextualSid(pSid, lpSidStr, &dwSidSize);
// use pSid, lpSidStr, szDomain, and SidType as
needed...
LocalFree(lpSidStr);
}
}
}
LocalFree(pSid);
}
}
}
Gambit
Re: Get user SID with LookupAccountName
Author: Mike Collins
Date: Thu, 24 Jan 2008 14:59
Date: Thu, 24 Jan 2008 14:59
21 lines
1810 bytes
1810 bytes
Hi Remy, just saw your post and I've got a couple of questions about the code you have shown. I've used a similar system in the past to get the computer SID but I'm not 100% on how it works. 1) Why have you coded the GetTextualSid instead of using the ConvertSidToStringSid? Does it have any advantages doing it your way? 2) Why do you use LocalFree? In my implementation i use GlobalFree - what is the difference in using these two? Thanks in advance, Mike C "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:4797e247$1@newsgroups.borland.com... > > "mataus" <mataus@gmail.com> wrote in message > news:4797d1c8@newsgroups.borland.com...
Re: Get user SID with LookupAccountName
Author: Dennis Jones
Date: Thu, 24 Jan 2008 15:18
Date: Thu, 24 Jan 2008 15:18
13 lines
1509 bytes
1509 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message news:4798a5a6$1@newsgroups.borland.com... > 2) Why do you use LocalFree? In my implementation i use GlobalFree - what > is the difference in using these two? In earlier versions of windows (16-bit), GlobalAlloc allocated memory from the global heap while LocalAlloc allocated memory from the local heap. However, in Win32, there is no difference between the global and local heaps so, as I understand it, the Global/Local functions have identical behavior. - Dennis
Re: Get user SID with LookupAccountName
Author: Remy Lebeau (TeamB)
Date: Thu, 24 Jan 2008 18:03
Date: Thu, 24 Jan 2008 18:03
34 lines
2109 bytes
2109 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message
news:4798a5a6$1@newsgroups.borland.com...
> Why have you coded the GetTextualSid instead of using
> the ConvertSidToStringSid?
1) GetTextualSid() is provided in MSDN's own examples, and keeps showing up
in newsgroup postings, even from Microsoft MVPs.
2) ConvertSidToStringSid() is not available on NT
If you have ConvertSidToStringSid() available on your machine, you can
certainly use it.
> Why do you use LocalFree?
Because I use LocalAlloc(). You can't use GlobalFree() to release memory
allocated by LocalAlloc(). Microsoft's documentation even says as much:
LocalAlloc Function
http://msdn2.microsoft.com/en-us/library/aa366723.aspx
"It is not safe to free memory allocated with LocalAlloc using
GlobalFree."
> In my implementation i use GlobalFree - what is the difference
> in using these two?
Global and Local Functions
http://msdn2.microsoft.com/en-us/library/aa366596(VS.85).aspx
Gambit
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads