Article View: borland.public.cppbuilder.nativeapi
Article #2258Both OpenThreadToken & OpenProcessToken Fails
From: Maurice Anderson
Date: Sat, 17 Mar 2007 22:31
Date: Sat, 17 Mar 2007 22:31
116 lines
4881 bytes
4881 bytes
Hello,
For the purposes of connecting to a remote registry, I wrote the function
below to check if the current loginID has a certain priviledge, in this case
SE_TCB_NAME.
The problem is OpenThreadToken fails with "An attempt was made to reference
a token that does not exist". And OpenProcessToken fails with "Access
Denied". Any ideas? Is my code up to par with what I am trying to
accomplish(connecting to the remote registry)? Any improvement suggestions?
Thanks
bool __fastcall TRegIniFilePlus::IsTokenPriviledges(char* Priviledge)
{
unsigned long dwLength, dwPrivilegeNameSize, dwDisplayNameSize, dwLangId;
PTOKEN_GROUPS ptg = NULL;
UCHAR InfoBuffer[1000];
PTOKEN_PRIVILEGES ptgPrivileges = NULL;
LUID setcbnameValue;
bool Status = false;
HANDLE hToken;
UCHAR ucPrivilegeName[500];
UCHAR ucDisplayName[500];
String DisplayName;
if(!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, true, &hToken))
{
PrintError(GetLastError());
Form1->Memo1->Lines->Add(FLastError + " OpenThreadToken:
Failed");
if(OpenProcessToken(GetCurrentProcess(),
TOKEN_READ, &hToken))
{
PrintError(GetLastError());
Form1->Memo1->Lines->Add(FLastError + " OpenProcessToken:
Failed");
return false;
}
}
if (!GetTokenInformation(
hToken, // handle to the access
token
TokenGroups, // get information about the
token's groups
(LPVOID) ptgPrivileges, // pointer to TOKEN_GROUPS buffer
0, // size of buffer
&dwLength // receives required buffer
size
))
{
Status = false;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
goto Cleanup;
ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, dwLength);
if (ptgPrivileges == NULL)
goto Cleanup;
}
// Get the token group information from the access token.
if (!GetTokenInformation(
hToken, // handle to the access
token
TokenGroups, // get information about the
token's groups
(LPVOID) ptgPrivileges, // pointer to TOKEN_GROUPS buffer
dwLength, // size of buffer
&dwLength // receives required buffer
size
))
{
Status = false;
goto Cleanup;
}
for(unsigned int i = 0; i < ptgPrivileges->PrivilegeCount; i ++ )
{
dwPrivilegeNameSize = sizeof ucPrivilegeName;
dwDisplayNameSize = sizeof ucDisplayName;
LookupPrivilegeName( NULL,
&ptgPrivileges->Privileges[i].Luid,
ucPrivilegeName, &dwPrivilegeNameSize );
LookupPrivilegeDisplayName( NULL, ucPrivilegeName,
ucDisplayName, &dwDisplayNameSize, &dwLangId );
// printf( "%40s (%s)\n", ucDisplayName, ucPrivilegeName );
DisplayName = *ucDisplayName;
Form1->Memo1->Lines->Add(DisplayName);// + " ---- " +
ucPrivilegeName);
}
if ( !LookupPrivilegeValue( NULL, SE_TCB_NAME, &setcbnameValue ) )
{
// puts( "LookupPrivilegeValue" );
ShowMessage(" LookupPrivilegeValue ");
Status = false;
goto Cleanup;
}
Cleanup:
// Free the buffer for the token groups.
if (ptg != NULL)
HeapFree(GetProcessHeap(), 0, (LPVOID)ptg);
return Status;
}
Technical Details
Message-ID:
<45fc6c49$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.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-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,314803bcf5c6fe3,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Maurice Anderson" <mauriceanderson@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Both OpenThreadToken & OpenProcessToken Fails Date: Sat, 17 Mar 2007 15:31:38 -0700 Lines: 118 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3028 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 64.30.203.223 Message-ID: <45fc6c49$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1174170697 64.30.203.223 (17 Mar 2007 15:31:37 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.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