从Windows服务启动GUI应用程序 - 窗口不会出现 [英] Launching GUI App from Windows Service - Window Does Not Appear

查看:1298
本文介绍了从Windows服务启动GUI应用程序 - 窗口不会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的Windows服务,将推出在
的OnStart()服务的方法指定一个exe。启动EXE得到推出它只
给出的内存,但它并不在浏览器中显示的售后服务。



我试图发动的calc.exe 显示在内存中的exe文件,但
犯规进入我的资源管理器视图(IE)。



下面是我的代码启动exe文件在调用onStart()方法。

 过程PR =新处理(); 
pr.StartInfo.FileName =的calc.exe;
pr.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
pr.StartInfo.CreateNoWindow = FALSE;
pr.Start();
// pr.WaitForExit();


解决方案

在其他会议在Vista上运行或服务后与应用直接启动从服务在同一会话默认情况下启动。开始在其他场次的应用是可能的 - 你必须要找到用户会话ID和使用CreateProcessAsUser。



如果多个用户登录,你需要开始你的计划,你必须找到所有会话的ID的所有用户。



下面是示例代码:

  INT会话= Win32.WTSGetActiveConsoleSessionId(); 
如果(会话== 0xFFFFFFFF的)
{
返回FALSE;
}

IntPtr的userToken;
布尔解析度= Win32.WTSQueryUserToken(会话,出userToken);
如果
{
this.log.WriteEntry(错误WTSQueryUserToken)(RES!);
返回FALSE;
}

路径字符串=的getPath();
串DIR = Path.GetDirectoryName(路径);
Win32.STARTUPINFO SI =新Win32.STARTUPINFO();
si.lpDesktop =winsta0\\default;
si.cb = Marshal.SizeOf(SI);

Win32.PROCESS_INFORMATION PI =新Win32.PROCESS_INFORMATION();
Win32.SECURITY_ATTRIBUTES SA =新Win32.SECURITY_ATTRIBUTES();
sa.bInheritHandle = 0;
sa.nLength = Marshal.SizeOf(SA);
sa.lpSecurityDescriptor = IntPtr.Zero;

如果(!Win32.CreateProcessAsUser(userToken,//用户令牌
路径,// exexutable路径
的String.Empty,//参数
REF SA,/ /工艺安全属性(无)
裁判SA,//线程安全属性(无)
假,//继承手柄?
0,//创建标志
IntPtr.Zero //环境变量
目录,//新工艺
参考SI的当前目录,//启动信息
OUT PI))//在PI
接收处理信息{
INT错误= Marshal.GetLastWin32Error();
this.log.WriteEntry(错误CreateProcessAsUser:+误差);
返回FALSE;
}


I have written a simple windows service which will launch a exe specified in the onstart() method of the service. After starting the service the exe got launched it only presents in the memory but it doesnt show in the explorer.

I'm trying to launch a calc.exe from my code.it shows the exe in the memory but it doesnt comes into my view(i.e) in the explorer.

Below is my code to launch the exe in the onStart() method

    Process pr=new Process();
    pr.StartInfo.FileName="calc.exe";
    pr.StartInfo.WindowStyle=ProcessWindowStyle.Maximized;
    pr.StartInfo.CreateNoWindow=false;
    pr.Start();
//  pr.WaitForExit();

解决方案

Services run in other session on Vista or later and applications started directly from services are started in the same session by default. Starting applications in other sessions is possible - you have to find the id of the user session and use CreateProcessAsUser.

If more than one user is logged in and you need to start your program for all users you must find the ids of all sessions.

Here is sample code:

int session = Win32.WTSGetActiveConsoleSessionId();
if (session == 0xFFFFFFFF)
{
    return false;
}

IntPtr userToken;
bool res = Win32.WTSQueryUserToken(session, out userToken);
if (!res)
{
    this.log.WriteEntry("Error WTSQueryUserToken");
    return false;
}

string path = GetPath();
string dir = Path.GetDirectoryName(path);
Win32.STARTUPINFO si = new Win32.STARTUPINFO();
si.lpDesktop = "winsta0\\default";
si.cb = Marshal.SizeOf(si);

Win32.PROCESS_INFORMATION pi = new Win32.PROCESS_INFORMATION();
Win32.SECURITY_ATTRIBUTES sa = new Win32.SECURITY_ATTRIBUTES();
sa.bInheritHandle = 0;
sa.nLength = Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = IntPtr.Zero;

if (!Win32.CreateProcessAsUser(userToken,       // user token
                                path,           // exexutable path
                                string.Empty,   // arguments
                                ref sa,         // process security attributes ( none )
                                ref sa,         // thread  security attributes ( none )
                                false,          // inherit handles?
                                0,              // creation flags
                                IntPtr.Zero,    // environment variables
                                dir,            // current directory of the new process
                                ref si,         // startup info
                                out pi))        // receive process information in pi
{
    int error = Marshal.GetLastWin32Error();
    this.log.WriteEntry("Error CreateProcessAsUser:" + error);
    return false;
}

这篇关于从Windows服务启动GUI应用程序 - 窗口不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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