WinAPI C-来自REDMON_USER和REDMON_SESSIONID的RunAsUser [英] WinAPI C - RunAsUser from REDMON_USER and REDMON_SESSIONID

查看:199
本文介绍了WinAPI C-来自REDMON_USER和REDMON_SESSIONID的RunAsUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

但是,我想启动gsview.exe来查看文件.如果我调用ShellExecute,则由于权限问题,它将在Windows 7中失败.看来我的程序是在其他用户帐户(本地服务)下调用的.因此,我正在寻找一种在特定用户名(启动打印作业的用户)下运行gsview.exe的方法,该用户名也可以在名为REDMON_USER的变量中与SESSIONID一起用于程序.

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.

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

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

使用C/C ++ 、. NET编写的任何代码示例都将非常有帮助.

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

我正在尝试完成的工作与redrunee非常相似(来自redmonee).我不想使用redrunee,因为它会在大约一会儿的控制台窗口中打开.

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.

注意: 1)打印机服务将该程序作为[本地服务]帐户调用. 2)实际上,第一个参数用户名(REDMON_USER)指向当前正在查看屏幕的用户

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

推荐答案

查看还要查看CreateProcessWithLogonWCreateProcess.

它们从CreateProcessAsUser链接

They are linked from the CreateProcessAsUser

编辑.回复OP的评论.

遵循此线程.

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

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

与Vista相同的代码对我们有效 在XP等上运行.服务以 本地系统.

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

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

  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).

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

use WTSQueryUserToken to get the token for that session.

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

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

使用CreateEnvironmentBlock创建您将要使用的环境 传递给流程.

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

将CreateProcessAsUser与重复的令牌和已创建的令牌一起使用 环境.实际上,我们使用 CreateProcessAsUserW,因为A 版本在某些方面存在某种错误 较旧的系统.

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.

别忘了对各种令牌等使用CloseHandle,并 销毁环境 环境.

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

谢谢您法语.

PS.哦,Windows编程的乐趣,好一阵子没做.现在我记得为什么.唯一接近甚至更差的文献是OpenSSH编程.

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.

这篇关于WinAPI C-来自REDMON_USER和REDMON_SESSIONID的RunAsUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆