サービス アプリケーションと 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);
}
}
サービスの LonchNewApplication() メソッド OnStart を呼び出しています。