0

我只想打电话

Process.Start("notepad.exe");

服务何时启动;但它根本不起作用。在我选择在 Windows 服务管理器中启动服务后,没有立即调用记事本。

十分感谢。

[更新]

在登录选项卡中勾选以允许交互式桌面后,我使其工作。但我不知道这到底是什么意思?如果它总是要求我接受在交互式桌面检测面板中查看消息,我如何安排在任何计算机上运行任务?

4

1 回答 1

3

Windows 服务与标准进程不同,默认情况下它不能与用户桌面交互(这是 Windows 操作系统的规则),因此要启动一个进程并允许它与用户桌面交互,您必须标记与桌面交互选项...

请记住,从 Windows Vista 开始,服务在会话 0 下运行,每次服务尝试启动进程时,都会向用户显示一个面板,让用户选择是否要运行该进程;要克服这个限制(要求确认的面板),唯一的方法是直接使用Windows API的CreateProcessAsUser函数从服务启动进程......

看看我之前开发的这个函数,它使用了CreateProcessAsUser API,即使在 Vista/7 中也无需询问任何内容即可从服务启动进程:

    /// <summary>
    /// LaunchProcess As User Overloaded for Window Mode 
    /// </summary>
    /// <param name="cmdLine"></param>
    /// <param name="token"></param>
    /// <param name="envBlock"></param>
    /// <param name="WindowMode"></param>
    /// <returns></returns>
    private static bool LaunchProcessAsUser(string cmdLine, IntPtr token, IntPtr envBlock,uint WindowMode)
    {
        bool result = false;

        PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
        SECURITY_ATTRIBUTES saProcess = new SECURITY_ATTRIBUTES();
        SECURITY_ATTRIBUTES saThread = new SECURITY_ATTRIBUTES();
        saProcess.nLength = (uint)Marshal.SizeOf(saProcess);
        saThread.nLength = (uint)Marshal.SizeOf(saThread);

        STARTUPINFO si = new STARTUPINFO();
        si.cb = (uint)Marshal.SizeOf(si);

        //if this member is NULL, the new process inherits the desktop
        //and window station of its parent process. If this member is
        //an empty string, the process does not inherit the desktop and
        //window station of its parent process; instead, the system
        //determines if a new desktop and window station need to be created.
        //If the impersonated user already has a desktop, the system uses the
        //existing desktop.

        si.lpDesktop = @"WinSta0\Default"; //Default Vista/7 Desktop Session
        si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK;

        //Check the Startup Mode of the Process 
        if (WindowMode == 1)
            si.wShowWindow = SW_SHOW;
        else if (WindowMode == 2)
        { //Do Nothing
        }
        else if (WindowMode == 3)
            si.wShowWindow = 0; //Hide Window 
        else if (WindowMode == 4)
            si.wShowWindow = 3; //Maximize Window
        else if (WindowMode == 5)
            si.wShowWindow = 6; //Minimize Window
        else
            si.wShowWindow = SW_SHOW;


        //Set other si properties as required.
        result = CreateProcessAsUser(
        token,
        null,
        cmdLine,
        ref saProcess,
        ref saThread,
        false,
        CREATE_UNICODE_ENVIRONMENT,
        envBlock,
        null,
        ref si,
        out pi);

        if (result == false)
        {
            int error = Marshal.GetLastWin32Error();
            string message = String.Format("CreateProcessAsUser Error: {0}", error);
            Debug.WriteLine(message);

        }

        return result;
    }
于 2012-07-08T14:54:33.937 回答