Click to See Complete Forum and Search --> : [RESOLVED] call GetTokenInformation cause error


halfman
March 30th, 2006, 07:56 AM
Code as follow:

HANDLE hToken;

if(!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken))
{
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
return kcy_err;
}
}
//didn't return,above two function are success
DWORD dwUserSize = 0;
GetTokenInformation(hToken, TokenUser, NULL, 0, &dwUserSize);

//GetTokenInformation return FALSE,call GetLastError get error code is 122,what's the mean?

SuperKoko
March 30th, 2006, 10:02 AM
122 is the code for ERROR_INSUFFICIENT_BUFFER.

TokenInformation
[out] Pointer to a buffer the function fills with the requested information. The structure put into this buffer depends upon the type of information specified by the TokenInformationClass parameter, as shown in the following table.

NULL The function returns ERROR_INSUFFICIENT_BUFFER and stores the size required for the buffer in ReturnLength. The caller can then allocate a buffer with the required size and pass the address of the buffer as TokenInformation in another call to this function.

MSDN is not exact here.
In fact, the function doesn't return ERROR_INSUFFICIENT_BUFFER, but returns FALSE and set the last error to ERROR_INSUFFICIENT_BUFFER.

halfman
March 30th, 2006, 07:18 PM
But,how to resolve the problem?

SuperKoko
March 31st, 2006, 04:37 AM
But,how to resolve the problem?

Hey, you want a TOKEN_USER!
So, pass a pointer to it:

TOKEN_USER tkUser;
GetTokenInformation(hToken, TokenUser, &tkUser, sizeof(tkUser), &dwUserSize);