2012-11-08 83 views
0

我用C寫的下面的程序:獲取令牌信息

#include "stdafx.h" 
#include <Windows.h> 

void main() 
{ 
    char buffer[1000]; 
    int size = sizeof(buffer); 
    PDWORD required_size; 

    printf("----Application Privileges----\n\n"); 
    printf("In this program, we are going to obtain information about the application privileges\n\n"); 

    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); 

    printf("Press enter to exit the program"); 
    getchar(); 

} 

現在,我是比較新的使用令牌。當我嘗試執行該程序時,出現以下錯誤:

運行時檢查失敗#3 - 正在使用變量'required_size'而未初始化。

請問我該如何解決這個問題?我想要做的是向用戶顯示有關當前進程的令牌權限的信息。

我不確切知道GetTokenInformation方法中的最後一個變量(ReturnLength [out])是什麼。我嘗試閱讀msdn文檔,但不明白它的用法。

+0

對不起,我編輯的前一個問題,忘了更改標籤:■ – Matthew

+0

嘗試初始化它! – CCoder

回答

1

檢查example and detailed explanation我再次給你。你首先需要找到緩衝區的長度。然後將您的緩衝區初始化爲您獲得的TOKEN_PRIVILEGES結構的大小。下面是做初始化行:

BYTE* pBuffer = new BYTE[dwLen]; 
+0

感謝您的回答:)我有一點問題來顯示緩衝區中檢索到的信息。出於某種原因,屏幕上只顯示無意義的垃圾信息。現在,我只想顯示令牌信息。請多多包涵。我自己正在學習所有這些東西,這就是爲什麼有時我很難理解某些概念。 – Matthew

+0

如果初始化並嘗試循環訪問權限後仍然給您帶來麻煩,請編輯您的問題併發布新代碼。然後我們可以嘗試解決這個問題。 –

3

required_size參數是一個「out」參數,表示它從函數返回信息給你(即一個額外的返回值)。你應該把它傳遞給現有的DWORD變量的地址,並且它填充了那裏的數據,但是你傳遞了一個未初始化的指針,它試圖通過它。

您的代碼應該是這樣的:

DWORD required_size; 
GetTokenInformation(..., &required_size); // Pass address of required_size 
// required_size now contains the required size of the data buffer