2012-11-08 82 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