Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I get the computer SID?

388 views
Skip to first unread message

Andre Luis V. S. de Andrade

unread,
Apr 3, 2001, 5:00:05 PM4/3/01
to
Hi.

I'd like to know how can I get the SID of a computer.

Can anyone help me?

Thanks in advance.

Andre Luis.


Prabagar Ramadasse

unread,
Apr 3, 2001, 10:02:57 PM4/3/01
to
Andre Luis,

A computer account name always has a trailing dollar sign ($). You can use
any APIs that takes a user account by passing in the computer name with a
dollar appended to it. With COMPUTERNAME$, you can use LookupAccountName()
API to get the SID of the computer.

For computer name specification, see the explanation in "Considerations for
Managing Computer Accounts" in MORE INFORMATION section in the following
knowledge base article.

Q136867 - HOWTO: Manage Computer Accounts Programmatically in Windows NT
http://support.microsoft.com/support/kb/articles/Q136/8/67.asp

Hope this helps!!

Thanks,
Prabagar

"Andre Luis V. S. de Andrade" <al...@globo.com> wrote in message
news:uwTIKEIvAHA.1336@tkmsftngp04...

Andre Luis V. S. de Andrade

unread,
Apr 4, 2001, 8:15:25 AM4/4/01
to
Thanks, Ramadasse.

I think this will help me, but I have a doubt yet: does it work on Windows
2000? Have you already tested it?

Thanks.

Andre Luis.

"Prabagar Ramadasse" <nospam_...@microsoft.com> escreveu na mensagem
news:uTyVgtKvAHA.2228@tkmsftngp02...

Prabagar Ramadasse

unread,
Apr 4, 2001, 2:54:39 PM4/4/01
to
Andre Luis,

It should work on Windows 2000. Here is a working code for your reference.

/*****************************************/

#include <windows.h>
#include <stdio.h>

BOOL GetTextualSid(
PSID pSid, // binary Sid
LPSTR szTextualSid, // buffer for Textual Sid
LPDWORD dwBufferLen // required/provided buffer size
);

void main(void)
{
char computerName[MAX_COMPUTERNAME_LENGTH+1];

PSID Sid = NULL;
DWORD cbSid = 0;
LPSTR ReferencedDomainName = NULL;
DWORD cbReferencedDomainName = 0;
SID_NAME_USE eUse;

char szTextualSid[256];
DWORD dwBufferLen;

__try {

// Obtain computer name
dwBufferLen = sizeof(computerName);
if (!GetComputerName(computerName, &dwBufferLen)) {
fprintf(stderr, "GetComputerName() failed. Error %d\n",
GetLastError());
__leave;
}

strcat(computerName, "$");

cbSid = 0;
cbReferencedDomainName = 0;
Sid = NULL;
ReferencedDomainName = NULL;
LookupAccountName(
NULL, // Machine to lookup account on.
computerName, // Address of string for account name.
Sid, // Address of security identifier.
&cbSid, // Address of size of security identifier.
ReferencedDomainName, // Address of string for referenced domain.
&cbReferencedDomainName, // Address of size of domain string.
&eUse // Address of SID-type indicator.
);

Sid = (PSID)malloc(cbSid);
ReferencedDomainName = (LPSTR)malloc(cbReferencedDomainName);

if (!LookupAccountName(
NULL, // Machine to lookup account on.
computerName, // Address of string for account name.
Sid, // Address of security identifier.
&cbSid, // Address of size of security identifier.
ReferencedDomainName, // Address of string for referenced domain.
&cbReferencedDomainName, // Address of size of domain string.
&eUse // Address of SID-type indicator.
)) {
fprintf(stderr, "LookupAccountName() failed. Error %d\n",
GetLastError());
__leave;
}

dwBufferLen = sizeof(szTextualSid);
// Obtain the textual representation of the SID.
if (!GetTextualSid(
Sid, // user binary Sid
szTextualSid, // buffer for TextualSid
&dwBufferLen)) { // size/required buffer

fprintf(stderr, "GetTextualSid() failed. Error %d\n",
GetLastError());
__leave;
}

// Display the TextualSid representation.
fprintf(stdout, "%s\n", szTextualSid);

} __finally {

if (Sid)
free((void *)Sid);
if (ReferencedDomainName)
free((void *)ReferencedDomainName);
if (szTextualSid)
free((void *)szTextualSid);
}

return;
}


BOOL GetTextualSid(PSID pSid, LPSTR szTextualSid,
LPDWORD dwBufferLen) {

PSID_IDENTIFIER_AUTHORITY psia;
DWORD dwSubAuthorities;
DWORD dwSidRev = SID_REVISION;
DWORD dwCounter;
DWORD dwSidSize;

// Test if SID passed in is valid.
if(!IsValidSid(pSid))
return FALSE;

// Obtain SidIdentifierAuthority.
psia = GetSidIdentifierAuthority(pSid);

// Obtain sidsubauthority count.
dwSubAuthorities = *GetSidSubAuthorityCount(pSid);

// Compute buffer length.
// S-SID_REVISION- + identifierauthority- + subauthorities- + NULL
dwSidSize = (15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);

// Check provided buffer length.
// If not large enough, indicate proper size and setlasterror
if (*dwBufferLen < dwSidSize) {

*dwBufferLen = dwSidSize;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}

// Prepare S-SID_REVISION-.
dwSidSize = wsprintf(szTextualSid, TEXT("S-%lu-"), dwSidRev);

// Prepare SidIdentifierAuthority.
if ((psia->Value[0] != 0) || (psia->Value[1] != 0)) {

dwSidSize += wsprintf(szTextualSid + lstrlen(szTextualSid),
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(szTextualSid + lstrlen(szTextualSid),
TEXT("%lu"),
(ULONG) (psia->Value[5] ) +
(ULONG) (psia->Value[4] << 8) +
(ULONG) (psia->Value[3] << 16) +
(ULONG) (psia->Value[2] << 24));
}

// Loop through SidSubAuthorities.
for (dwCounter = 0; dwCounter < dwSubAuthorities; dwCounter++) {

dwSidSize += wsprintf(szTextualSid + dwSidSize, TEXT("-%lu"),
*GetSidSubAuthority(pSid, dwCounter));
}

return TRUE;
}

/*****************************************/
This calls GetComputerName() API, appends a $ and uses that string in
LookupAccountName() API. Also, it converts the binary SID to a textual SID
and prints it.

Try out this code.

Thanks,
Prabagar

"Andre Luis V. S. de Andrade" <al...@globo.com> wrote in message

news:emfipDQvAHA.1900@tkmsftngp05...

0 new messages