我已经创建了一个服务应用程序和一个 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 。