C#:如何编组 TOKEN_GROUPS 以调用 LogonUserExEx() (或 LsaLogonUser())
LogonUserExEx() 和 LsaLogonUser() 都接受 PTOKEN_GROUPS pTokenGroups 参数。我无法为此参数正确编组我的结构。
我定义了以下结构:
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_GROUPS
{
public UInt32 GroupCount;
// Followed by this:
public SID_AND_ATTRIBUTES[] Groups;
}
[StructLayout(LayoutKind.Sequential)]
public struct SID_AND_ATTRIBUTES
{
public IntPtr Sid;
public UInt32 Attributes;
}
然后,在我的代码中,我设置如下结构:
win32.TOKEN_GROUPS tg = new win32.TOKEN_GROUPS();
tg.GroupCount = 2;
tg.Groups = new win32.SID_AND_ATTRIBUTES[2];
tg.Groups[0].Attributes = win32.SE_GROUP_ENABLED;
win32.ConvertStringSidToSid("S-1-5-32-546", out (tg.Groups[0].Sid)); // guests TEST
tg.Groups[1].Attributes = win32.SE_GROUP_ENABLED;
win32.ConvertStringSidToSid("S-1-5-32-547", out (tg.Groups[1].Sid)); // power users TEST
这一切似乎都有效(ConvertStringSidToSid 两次都返回 TRUE)。那我想要 将其转换为 IntPtr,我可以将其传递给实际的 API 函数。为此,我尝试:
IntPtr pGroups = Marshal.AllocHGlobal(Marshal.SizeOf(tg))
其次是:
Marshal.StructureToPtr(tg, pGroups, false);
但是这里出了问题,因为抛出了“参数不正确”的异常。有谁知道为什么会发生这种情况?非常感谢您的帮助。
Both LogonUserExEx() and LsaLogonUser() accept a PTOKEN_GROUPS pTokenGroups parameter. I am having trouble marshalling my structure correctly for this parameter.
I have the following structures defined:
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_GROUPS
{
public UInt32 GroupCount;
// Followed by this:
public SID_AND_ATTRIBUTES[] Groups;
}
[StructLayout(LayoutKind.Sequential)]
public struct SID_AND_ATTRIBUTES
{
public IntPtr Sid;
public UInt32 Attributes;
}
Then, in my code I am setting up the structure like this:
win32.TOKEN_GROUPS tg = new win32.TOKEN_GROUPS();
tg.GroupCount = 2;
tg.Groups = new win32.SID_AND_ATTRIBUTES[2];
tg.Groups[0].Attributes = win32.SE_GROUP_ENABLED;
win32.ConvertStringSidToSid("S-1-5-32-546", out (tg.Groups[0].Sid)); // guests TEST
tg.Groups[1].Attributes = win32.SE_GROUP_ENABLED;
win32.ConvertStringSidToSid("S-1-5-32-547", out (tg.Groups[1].Sid)); // power users TEST
This all seems to work (the ConvertStringSidToSid returns TRUE both times). Then I want
to turn this into an IntPtr which I can pass to the actual API function. To do this I try:
IntPtr pGroups = Marshal.AllocHGlobal(Marshal.SizeOf(tg))
Followed by:
Marshal.StructureToPtr(tg, pGroups, false);
Something goes awry here however since an exception is thrown with "The Parameter Is Incorrect". Does anyone have an idea why this is happening? Many thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过像这样定义TOKEN_GROUPS:
请参阅:TOKEN_GROUPS
have you tried defining TOKEN_GROUPS like this:
see: TOKEN_GROUPS
如果您还没有这样做,请查看 http://pinvoke.net/ 的
页面 LsaLogonUser 可能会有所帮助。
If you have not done so already, have a look on http://pinvoke.net/
The page for LsaLogonUser may help.