21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

found, NULL will be returned, and GetLastError( ) will return ERROR_NONE_MAPPED. If<br />

any other kind of error occurs, SpcLookupName( ) will return NULL, and GetLastError( )<br />

will return the relevant error code.<br />

#include <br />

PSID SpcLookupName(LPCTSTR lpszSystemName, LPCTSTR lpszAccountName) {<br />

PSID Sid;<br />

DWORD cbReferencedDomainName, cbSid;<br />

LPTSTR ReferencedDomainName;<br />

SID_NAME_USE eUse;<br />

cbReferencedDomainName = cbSid = 0;<br />

if (LookupAccountName(lpszSystemName, lpszAccountName, 0, &cbSid,<br />

0, &cbReferencedDomainName, &eUse)) {<br />

SetLastError(ERROR_NONE_MAPPED);<br />

return 0;<br />

}<br />

if (GetLastError( ) != ERROR_INSUFFICIENT_BUFFER) return 0;<br />

if (!(Sid = (PSID)LocalAlloc(LMEM_FIXED, cbSid))) return 0;<br />

ReferencedDomainName = (LPTSTR)LocalAlloc(LMEM_FIXED, cbReferencedDomainName);<br />

if (!ReferencedDomainName) {<br />

LocalFree(Sid);<br />

return 0;<br />

}<br />

if (!LookupAccountName(lpszSystemName, lpszAccountName, Sid, &cbSid,<br />

ReferencedDomainName, &cbReferencedDomainName, &eUse)) {<br />

LocalFree(ReferencedDomainName);<br />

LocalFree(Sid);<br />

return 0;<br />

}<br />

LocalFree(ReferencedDomainName);<br />

return Sid;<br />

}<br />

The Win32 API function LookupAccountSid( ) is used to find the name that corresponds<br />

to a SID. You can use it to obtain information about a SID on either the local<br />

system or a remote system. While it might seem that mapping a SID to a name is a<br />

simple operation, LookupAccountSid( ) actually requires a large number of arguments<br />

to allow it to complete its work.<br />

LookupAccountSid( ) has the following signature:<br />

BOOL LookupAccountSid(LPCTSTR lpSystemName, PSID Sid,LPTSTR Name, LPDWORD cbName,<br />

LPTSTR ReferencedDomainName, LPDWORD cbReferencedDomainName,<br />

PSID_NAME_USE peUse);<br />

This function has the following arguments:<br />

lpSystemName<br />

String representing the name of the remote system on which to look up the SID.<br />

If you specify this argument as NULL, the lookup will be done on the local system.<br />

Getting User and Group Information on Windows | 377<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!