2012-11-08 114 views
1

时,我有写在C下面的代码片段这是应该检索有关程序权限令牌信息:Ç - 缓冲区打印垃圾字符获得令牌信息

char buffer[1000]; //Declaring the buffer where the token information will be stored 
int size = sizeof(buffer); //Storing the size of the buffer 
DWORD required_size; //A variable storing return information from the GetTokenInformation() method 

HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, GetCurrentProcessId()); //Opening the current process 
HANDLE token; //Creating a handle for the token 
OpenProcessToken(process, TOKEN_ADJUST_PRIVILEGES, &token); //Opening the process token 

GetTokenInformation(token, TokenPrivileges, buffer, size, &required_size); //Obtaining the token information 
printf("The following information was obtained with regards to the token privileges: \n\n"); 
printf("%s\n\n", buffer); 

的主要问题是,当我尝试打印'缓冲区'的内容,只有垃圾信息打印在屏幕上:■我该如何解决这个问题?

回答

3

这是打印垃圾,因为你没有的数据不是C字符串,它只是原始的二进制数据。在TokenPrivileges的情况下,数据实际上是TOKEN_PRIVILEGES结构。

因此,您应该将字节缓冲区转换为指向TOKEN_PRIVILEGES结构的指针。请注意,它以弹性阵列成员结尾 - Privileges数组将包含可变数目的数组元素,这就是为什么您必须查询总大小并使用足够大的字节缓冲区,而不是仅在数组中分配TOKEN_PRIVILEGES堆栈的大小不足以容纳多个条目。