WinAPI C - 来自 REDMON_USER 和 REDMON_SESSIONID 的 RunAsUser

发布于 2024-09-02 12:23:10 字数 625 浏览 2 评论 0原文

我安装了 PostScript 打印机驱动程序并设置了 REDMON (redmonnt.dll),用于将 postscript 输出重定向到我的程序。在我相当简单的 c 程序中,我从 STDIN 捕获数据,并且能够成功地将其保存到 .ps 文件中。该文件看起来不错。

但是,我想启动 gsview.exe 来查看该文件。如果我调用 ShellExecute,它在 Windows 7 中会因权限问题而失败。看来我的程序是在不同的用户帐户(LOCAL SERVICE)下调用的。因此,我正在寻找一种在特定用户名(启动打印作业的用户)下运行 gsview.exe 的方法,该用户名也可以在名为 REDMON_USER 的变量以及 SESSIONID 中供程序使用。

:在给定用户名和会话 ID 的情况下,启动程序所需的最少 WinAPI 调用是多少?

任何 C/C++、.NET 代码示例都会非常有帮助。

编辑:我想要完成的事情与 redrunee (来自 redmonee)非常相似。我不想使用 redrunee,因为它会打开一个控制台窗口一小会儿。

笔记: 1) 该程序由打印机服务以[LOCAL SERVICE]帐户调用。 2)第一个参数Username(REDMON_USER),实际上指向当前正在查看屏幕的用户

I installed a PostScript printer driver and have setup REDMON (redmonnt.dll) for redirecting postscript output to my program. In my rather simple c program I capture the data from STDIN and I am able to successfully save it into a .ps file. The file looks OK.

However, I want to start gsview.exe for viewing the file. If I call ShellExecute it fails in Windows 7 because of permission issues. It seems that my program is called under a different user account (LOCAL SERVICE). So I am looking for a way to run gsview.exe under a specific username (the user who initiated the print job) which is available to the program in a variable called REDMON_USER along with the SESSIONID as well.

Q: What are the minimum WinAPI calls required to start a program given a username and a sessionid?

Any code examples in C/C++, .NET would be very helpful.

EDIT: What I am trying accomplish is something very similar to redrunee (from redmonee). I don't want to use redrunee because it opens about a console window for a brief moment.

Note:
1) The program is called by the printer service as [LOCAL SERVICE] account.
2) The first parameter Username (REDMON_USER), in effect, points to the user currently looking at the screen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

街角卖回忆 2024-09-09 12:23:10

查看 CreateProcessAsUser

另请参阅 CreateProcessWithLogonWCreateProcess

它们是从 CreateProcessAsUser

EDIT 链接的,以回复 OP 的评论。

请遵循此线程。

我在这里逐字复制此内容,以防原始链接停止工作:

相同的代码在 Vista 上适用于我们
在 XP 等上。该服务运行为
本地系统。

  1. 使用WTSGetActiveConsoleSessionId获取当前活动的ID
    控制台上的 Windows 会话(即
    机器键盘和显示器,如
    反对 WTS 会议)。

  2. 使用 WTSQueryUserToken 获取该会话的令牌。

  3. 使用 DuplicateTokenEx(hToken,MAXIMUM_ALLOWED,NULL,SecurityIdentification,TokenPrimary,
    &hTokenDup) 来复制该令牌。

  4. 使用 CreateEnvironmentBlock 创建一个您将身处的环境
    传递给进程。

  5. 将 CreateProcessAsUser 与复制的令牌和创建的令牌一起使用
    环境。实际上,我们使用
    CreateProcessAsUserW,因为 A
    版本有一些错误
    旧系统。

  6. 不要忘记对各种令牌等进行 CloseHandle,并且
    破坏环境阻止
    环境。

谢谢您efratian

附言。呵呵,Windows编程的乐趣,好久没做了。现在我记得为什么了。唯一接近甚至更差的记录是 OpenSSH 编程。

Look at CreateProcessAsUser.

Also look at CreateProcessWithLogonW and CreateProcess.

They are linked from the CreateProcessAsUser

EDIT In reply to comments by OP.

Follow advice from this thread.

I am copying this here verbatim, in case the original link stops working:

The same code works for us on Vista as
on XP, etc. The service is running as
the Local System.

  1. use WTSGetActiveConsoleSessionId to get the ID of the current active
    Windows session at the console (i.e.
    the machine keyboard and display, as
    opposed to WTS sessions).

  2. use WTSQueryUserToken to get the token for that session.

  3. use DuplicateTokenEx(hToken,MAXIMUM_ALLOWED,NULL,SecurityIdentification,TokenPrimary,
    &hTokenDup) to duplicate that token.

  4. use CreateEnvironmentBlock to create an environment that you will be
    passing to the process.

  5. use CreateProcessAsUser with the duplicated token and the created
    environment. Actually, we use
    CreateProcessAsUserW, since the A
    version had some sort of bug on some
    older systems.

  6. Don't forget to CloseHandle on the various tokens, etc, and to
    DestroyEnvironmentBlock the
    environment.

Thank you efratian.

PS. Oh joy of Windows programming, did not do it for quite a while. Now I remember why. The only thing that is close or even worse documented is OpenSSH programming.

踏雪无痕 2024-09-09 12:23:10

文档描述了“以用户身份运行”功能,这似乎正是你想要的:

以用户身份运行旨在通过 RedRun 在本地启动 GUI 程序,例如 GSview。

The documentation describes the "Run as User" feature, which seems to be exactly what you want:

Run as User is intended for launching a GUI program such as GSview locally via RedRun.

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