1

我已经创建了一个服务应用程序和一个 Windows 窗体应用程序,现在我想从服务中启动 Windows 应用程序。我知道在win7中由于服务隔离你不能直接这样做所以我使用'advapi32.dll'方法的'CreateProcessAsUser'但是它能够创建也出现在'任务管理器'中的进程但是UI不会被显示给用户。是什么原因 ?有人可以帮我解决这个问题吗?

好的,让我给出我写的代码

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Ansi, EntryPoint = "CreateProcessAsUser")] 

public static extern bool CreateProcessAsUser(IntPtr hToken,string lpApplicationName,string lpCommandLine,ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes,bool bInheritHandles,int dwCreationFlags,string lpEnvironment,string lpCurrentDirectory,ref STARTUPINFO lpStartupInfo,ref PROCESS_INFORMATION lpProcessInformation);


void LounchNewApplication() 

{


 try

  { 

      string strAppName = @"D:\Working\UAC Demo\Tester\bin\Debug\Tester.exe"; 

      string strAppPath = @"D:\Working\UAC Demo\Tester\bin\Debug\";

      PROCESS_INFORMATION lpProcessInformation = new PROCESS_INFORMATION(); 

      SECURITY_ATTRIBUTES lpProcessAttributes = new SECURITY_ATTRIBUTES(); 

      lpProcessAttributes.nLength = (uint)Marshal.SizeOf(lpProcessAttributes); 

      STARTUPINFO lpStartupInfo = new STARTUPINFO(); 

      lpStartupInfo.cb = Marshal.SizeOf(lpStartupInfo); 

      lpStartupInfo.lpDesktop = "WinSta0\\Default"; 
      IntPtr htoken = IntPtr.Zero; 

      LogonUser("myName", "DomineName", "password", 2, 0, out htoken); 


      if (!CreateProcessAsUser(htoken, strAppName, null, ref lpProcessAttributes,
         ref lpProcessAttributes, true, 0, null, strAppPath, ref lpStartupInfo,
         ref lpProcessInformation)) 

        {

          eventLogger.WriteEntry("Error in starting application", EventLogEntryType.Error); 

        }
     else
         eventLogger.WriteEntry("Application launched successfulll" EventLogEntryType.Information); 




      //CloseHandle(lpProcessInformation.hThread);



      //CloseHandle(lpProcessInformation.hProcess);

  }



  catch (Exception ex) 

  {

    eventLogger.WriteEntry(ex.Message,


     EventLogEntryType.Error); 

  }

}

我正在调用服务的 LounchNewApplication() 方法 OnStart 。

4

2 回答 2

1

您正在以用户身份启动进程,但在会话 0 中,这是非交互式的。不要使用 LogonUser 创建用户令牌。使用WTSQueryUserToken,传入要在其中创建进程的会话。此令牌具有正确的会话 ID。您可以使用WTSEnumerateSessions列出机器上的所有会话,或在服务处理程序中处理会话更改通知。

于 2009-10-21T19:23:43.303 回答
0

从技术上讲,服务必须标记为交互式,例如。sc config <servicename> type= interact.

服务不应该与控制台交互,并且绝对不能在服务启动中。幸运的是,这已在 Windows 2003 和 Vista、Windows 2008 和 Windows 7 中得到修复,越来越难以做出此类不当行为。

正确的方法是将您的应用程序分成服务和监控应用程序。监视器在用户会话上作为普通应用程序运行,并通过 IPC 与服务通信。

于 2009-10-21T19:24:44.097 回答