在 Visual Basic 6 中使用 GetTokenInformation 确定用户是否为管理员
我使用 GetTokenInformation
作为确定当前线程是否以管理员身份运行的代码的一部分。
无论如何,我的 TOKEN INFORMATION 结构如下所示:
Private Type TOKEN_GROUPS
GroupCount As Long
Groups(500) As SID_AND_ATTRIBUTES
End Type
然后,我像这样调用 GetTokenInformation
:
res = GetTokenInformation(<Process Handle>, 2, <TOKEN_GROUPS>, _
<Token Info Length>, <Buffer Length)
第一次调用是获取缓冲区长度,然后再次调用它以获取令牌信息。
无论如何,当运行应用程序的帐户连接到域时,应用程序会突然崩溃。显然,大小
Groups(500) As SID.AND.ATTRIBUTES
不够,导致缓冲区溢出。我不知道为什么会这样(MSDN 说我应该提供一个 ANYSIZE_ARRAY
或 1)。将组的大小增加到 1000 可解决此问题。
作为快速修复,由于我不知道如何获得适当大小的组,因此我计划仅 ReDim 组,直到调用成功。
这是我的问题:
我有一个
On Error
子句,但是当发生缓冲区溢出时,On Error
无法捕获它,我的应用程序突然崩溃。这是为什么?给出下面的代码
Private Type TOKEN_GROUPS
GroupCount As Long
Groups() As SID_AND_ATTRIBUTES 'FAILING
'Groups(1000) As SID_AND_ATTRIBUTES DOES NOT FAIL
End Type
Dim X as TOKEN_GROUPS
ReDim Preserve X.Groups(1000) As SID_AND_ATTRIBUTES 'FAILING
res = GetTokenInformation(<Process Handle>, 2, <TOKEN_GROUPS>, <Token Info Length>, <Buffer Length)
res = GetTokenInformation(<Process Handle>, 2, <TOKEN_GROUPS>, <Token Info Length>, <Buffer Length)
为什么当我将 Groups 声明为 1000 时,GetTokenInformation
调用没有失败,但是当我声明“空”Groups 时,
并将其重新调整为 1000,它失败了吗?GetTokenInformation
调用没有失败()
I am using GetTokenInformation
as a part of the code that determines if the current thread is running as an Administrator.
Anyway, I have a structure for TOKEN INFORMATION that looks like this:
Private Type TOKEN_GROUPS
GroupCount As Long
Groups(500) As SID_AND_ATTRIBUTES
End Type
Then, I invoke GetTokenInformation
like so:
res = GetTokenInformation(<Process Handle>, 2, <TOKEN_GROUPS>, _
<Token Info Length>, <Buffer Length)
The first invocation is to get the Buffer Length, then I invoke it again to get the token information.
Anyway, the application will suddenly crash when the account that run the application is connected to a domain. Apparently, the size of the,
Groups(500) As SID.AND.ATTRIBUTES
is not enough and is causing a buffer overrun. I don't know why that is (MSDN says that I should provide an ANYSIZE_ARRAY
or 1). Increasing the size of the Groups to 1000 fixes the problem.
As a quick fix and since I don't have an idea on how to get the appropriate size of Groups, I am planning to just ReDim the Groups until the call succeeds.
Here's my question:
I have an
On Error
clause, but when the buffer overrun occurs, theOn Error
can't catch it and my app suddenly crashes. Why is that?Given the code below
Private Type TOKEN_GROUPS
GroupCount As Long
Groups() As SID_AND_ATTRIBUTES 'FAILING
'Groups(1000) As SID_AND_ATTRIBUTES DOES NOT FAIL
End Type
Dim X as TOKEN_GROUPS
ReDim Preserve X.Groups(1000) As SID_AND_ATTRIBUTES 'FAILING
res = GetTokenInformation(<Process Handle>, 2, <TOKEN_GROUPS>, <Token Info Length>, <Buffer Length)
res = GetTokenInformation(<Process Handle>, 2, <TOKEN_GROUPS>, <Token Info Length>, <Buffer Length)
Why is that when I declared Groups as 1000, the GetTokenInformation
call is not failing but when I declared an "empty" Groups()
and ReDim'd it to 1000, it is failing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想对
组
使用动态大小的数组,您将需要“自定义 API 调用封送”代码。基本上是几个 CopyMemory 和一个数组调整大小If you want to use dynamicly sized array for
Groups
you'll need "custom API call marshaling" code. Basicly couple of CopyMemory's and an array resize还有另一个问题这里似乎已经解决了 GetTokenInformation 调用。
从已接受的答案中复制:
There is another question here that seems to have solved the GetTokenInformation call.
Copied from the accepted answer: