The first call to GetTokenInformation (). What for?

A look at the MSDN documentation for GetTokenInformation () and Obtaining an example SID login , GetTokenInformation () needs to be called twice. The first call is to get the size of the buffer.

So, buffer size for what? Just say that I use TokenUser as the second parameter, I see that dwReturnLength returned by the first call is not the size of the TOKEN_USER structure.

Thanks in advance

+3
source share
5 answers

The structure TOKEN_USERcontains pointers (in particular, a pointer to SID, which itself has a variable length). These pointers should point somewhere. The API function expects the buffer to be large enough to hold not only the structure TOKEN_USER, but also everything that the structure indicates. The function tells you how much memory it needs for everything. It will be in adjacent memory.

+8
source

The full example from your second URL should clearly indicate how the length returned from the first call is used. You use this to allocate raw memory of this size - this is a variable ptg- and give it to PTOKEN_GROUPS for use in the second call.

// Get required buffer size and allocate the TOKEN_GROUPS buffer.

   if (!GetTokenInformation(
         hToken,         // handle to the access token
         TokenGroups,    // get information about the token groups 
         (LPVOID) ptg,   // pointer to TOKEN_GROUPS buffer
         0,              // size of buffer
         &dwLength       // receives required buffer size
      )) 
   {
      if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) 
         goto Cleanup;

      ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
         HEAP_ZERO_MEMORY, dwLength);

      if (ptg == 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 groups 
         (LPVOID) ptg,   // pointer to TOKEN_GROUPS buffer
         dwLength,       // size of buffer
         &dwLength       // receives required buffer size
         )) 
   {
      goto Cleanup;
   }
+4
source

:

TokenInformation [out, optional]

. , TokenInformationClass .

TokenInformationLength [in]

, TokenInformation. TokenInformation - NULL, .

ReturnLength [out]

, , TokenInformation. TokenInformationLength, .

TokenInformationClass TokenDefaultDacl DACL, , ReturnLength, sizeof (TOKEN_DEFAULT_DACL) DefaultDacl TOKEN_DEFAULT_DACL NULL.

, № 2, API . .

, .

, Win32 API. .

+4

SID , SID, .

+3
source

You can do this work on your first call if your buffer is large enough. Most codes that use these methods will first try with a fixed-size buffer, and then allocate a larger buffer if the call indicates that it needs more memory.

+1
source

Source: https://habr.com/ru/post/1763864/


All Articles