According to the documentation For GetTokenInformation, if the function fails you can retrieve more information via a call to GetLastError
.
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
So you need to implement some checking for the extended error:
if (!GetTokenInformation(h_Token, TokenUser, &tp, cb, &dw_TokenLength))
{
int lastError = GetLastError();
// Should be a switch, of course. Omitted for brevity
if (lastError == ERROR_INSUFFICIENT_BUFFER)
{
//
}
}
As a general rule of thumb when using WinAPI functions that have varying buffer requirements, you typically
- Call the function with a NULL buffer to determine the buffer size needed (in this case, returned in the
ReturnLength
parameter) - Allocate a buffer of the indicated size
- Call the function again, passing the allocated buffer, to obtain the information