C#:如何编组 TOKEN_GROUPS 以调用 LogonUserExEx() (或 LsaLogonUser())

发布于 2024-08-05 22:20:21 字数 1188 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹短裙飘 2024-08-12 22:20:21

您是否尝试过像这样定义TOKEN_GROUPS:

[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_GROUPS
{
    public UInt32 GroupCount;
    // Followed by this:
    [MarshalAs(UnmanagedType.ByValArray)] // <--
    public SID_AND_ATTRIBUTES[] Groups;
}

请参阅:TOKEN_GROUPS

have you tried defining TOKEN_GROUPS like this:

[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_GROUPS
{
    public UInt32 GroupCount;
    // Followed by this:
    [MarshalAs(UnmanagedType.ByValArray)] // <--
    public SID_AND_ATTRIBUTES[] Groups;
}

see: TOKEN_GROUPS

趁年轻赶紧闹 2024-08-12 22:20:21

如果您还没有这样做,请查看 http://pinvoke.net/

页面 LsaLogonUser 可能会有所帮助。

If you have not done so already, have a look on http://pinvoke.net/

The page for LsaLogonUser may help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文