嗨,在Windows 7上無法從Windows服務啓動桌面應用程序
我有Windows 7上的C#WCF Windows服務以具有管理權限的用戶身份登錄。 我想在服務啓動後啓動桌面應用程序。我發現的所有討論都是關於Windows工作站和桌面......我創建了一個單獨的線程,設置線程工作站和桌面,並嘗試啓動桌面應用程序。 當我使用asp.net開發服務器從VS2010啓動它時,代碼有效,但如果它是從服務啓動的,則沒有錯誤,但進程未啓動。我嘗試以不同的用戶身份啓動進程,並嘗試調用CreateProcessWithLogonW(以前將startInfo.lpDesktop設置爲「winsta0 \ default」)。在這兩種情況下,我都有processId返回,但無法看到桌面應用程序。 有人能幫我看看哪裏出了錯?
public class ExternalProcess
{
const int READ_CONTROL = 0x20000;
const int WRITE_DAC = 0x40000;
const int DESKTOP_WRITEOBJECTS = 0x80;
const int DESKTOP_READOBJECTS = 0x1;
private Process extProcess;
private string sFilePath = "";
[DllImport("user32.dll")]
private static extern bool SetThreadDesktop(IntPtr hDesktop);
[DllImport("user32.dll")]
static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags,bool fInherit, uint dwDesiredAccess);
[DllImport("user32.dll")]
private static extern IntPtr GetProcessWindowStation();
[DllImport("user32.dll")]
private static extern IntPtr OpenWindowStation(string lpszWinSta, bool fInherit, ACCESS_MASK dwDesiredAccess);
[DllImport("user32.dll")]
private static extern IntPtr SetProcessWindowStation(IntPtr hWinsta);
public bool StartProcess(string filePath)
{
sFilePath = filePath;
Thread t = new Thread(new ThreadStart(Thread_StartProcess));
t.Start();
return true;
}
private void Thread_StartProcess()
{
IntPtr hwinstaSave;
IntPtr hwinsta, hwinsta2;
IntPtr hdesk;
hwinstaSave = GetProcessWindowStation();
System.Console.WriteLine("GetProcessWindowStation Lasterror= " + Marshal.GetLastWin32Error().ToString());
System.Console.WriteLine("GetProcessWindowStation hwinstaSave= " + hwinstaSave.ToString());
//hwinsta = OpenWindowStation("winsta0", false, ACCESS_MASK.GENERIC_EXECUTE | ACCESS_MASK.DESKTOP_CREATEWINDOW | ACCESS_MASK.DESKTOP_CREATEMENU | ACCESS_MASK.DESKTOP_SWITCHDESKTOP | ACCESS_MASK.DESKTOP_WRITEOBJECTS);
hwinsta = OpenWindowStation("winsta0", false, ACCESS_MASK.WINSTA_ALL_ACCESS); // when call from windows service OpenWindowStation returns 0
System.Console.WriteLine("OpenWindowStation lasterror = " + Marshal.GetLastWin32Error().ToString());
System.Console.WriteLine("OpenWindowStation hwinsta= " + hwinsta.ToString());
hwinsta2 = SetProcessWindowStation(hwinsta);
System.Console.WriteLine("SetProcessWindowStation lasterror = " + Marshal.GetLastWin32Error().ToString());
System.Console.WriteLine("SetProcessWindowStation hwinsta2= " + hwinsta2.ToString());
hdesk = OpenDesktop("default", 0, true, READ_CONTROL | WRITE_DAC | DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS);
System.Console.WriteLine("OpenDesktop lasterror = " + Marshal.GetLastWin32Error().ToString());
System.Console.WriteLine("OpenDesktop hdesk= " + hdesk.ToString());
bool Success = SetThreadDesktop(hdesk);
System.Console.WriteLine("SetThreadDesktop lasterror = " + Marshal.GetLastWin32Error().ToString());
System.Console.WriteLine("SetThreadDesktop Success= " + Success.ToString());
try
{
extProcess = new Process();
extProcess.StartInfo.FileName = sFilePath;
extProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
if (extProcess.Start())
System.Console.WriteLine("Process started ");
else
System.Console.WriteLine("Not started!");
}
catch (Win32Exception e)
{
System.Console.WriteLine("Start {0} failed. Error: " + e.Message);
}
}
}
完整的源代碼示例工作任何有關它的最終解決方案? – Kiquenet 2013-07-11 11:37:37