2012-11-08 71 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堆棧的大小不足以容納多個條目。