Issue Launching UI Application from windows service in c# Windows
-
I am getting problem when i launch a UI Application from windows Service Application. My requirement is while starting up of the service i need to run a UI Application.for Example i have taken notepad.exe. The below code is the code i have implemented to run the process. Process p = new Process(); p.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe"; bool bretval = p.Start(); Problem here is: It is running the process(notepad.exe) in background. I am not able to see it. I have gone through several Articles and i felt that WINDOWS service will supress the UI. Is there any Work around or any other Option so that my requirement can be obtained. Pls help me in this regard.
-
I am getting problem when i launch a UI Application from windows Service Application. My requirement is while starting up of the service i need to run a UI Application.for Example i have taken notepad.exe. The below code is the code i have implemented to run the process. Process p = new Process(); p.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe"; bool bretval = p.Start(); Problem here is: It is running the process(notepad.exe) in background. I am not able to see it. I have gone through several Articles and i felt that WINDOWS service will supress the UI. Is there any Work around or any other Option so that my requirement can be obtained. Pls help me in this regard.
-
You can try to activate "interact with desktop" for your service. However on Vista or later this flag become obsolete. Generally a service should not have to do anything with UI, because they also can run if nobody is logged in.
Greetings Covean
I Thank you for your awesome reply. It helped me and it worked out. As u told that this option cannot be implemented in Vista. I will tell you my requirement which is, I have a USB Device, when ever it is plugged in i need to Open my Application(.exe). It is just like iTunes Application for iPod. I have code for Detecting my USB Device. So i thought of implementing the device detecting code in windows service and if the device is found i can launch my application. Can you tell me any work around or any other way to Implement this senario so that it will work even in Vista. Regards, Prasad.
-
I Thank you for your awesome reply. It helped me and it worked out. As u told that this option cannot be implemented in Vista. I will tell you my requirement which is, I have a USB Device, when ever it is plugged in i need to Open my Application(.exe). It is just like iTunes Application for iPod. I have code for Detecting my USB Device. So i thought of implementing the device detecting code in windows service and if the device is found i can launch my application. Can you tell me any work around or any other way to Implement this senario so that it will work even in Vista. Regards, Prasad.
I would try the following way. In your service handler function wait for the SERVICE_CONTROL_SESSIONCHANGE event. (You have to say the system that your service wants this notification (SetServiceStatus with SERVICE_ACCEPT_SESSIONCHANGE)). After you received the SERVICE_CONTROL_SESSIONCHANGE (WTS_SESSION_LOGON) event, get the users new session id from the WTSSESSION_NOTIFICATION structure and "save" it. If you want to start the new process you have to do the following steps. Use WTSQueryUserToken to get the primary access token. Now try to acquire TOKEN_QUERY, TOKEN_DUPLICATE, and TOKEN_ASSIGN_PRIMARY for this token by using SetTokenInformation. Last Step: CreateProcessAsUser to start your process. I'm short in time so I couldn't test it but I think this should do the work.
Greetings Covean
-
I would try the following way. In your service handler function wait for the SERVICE_CONTROL_SESSIONCHANGE event. (You have to say the system that your service wants this notification (SetServiceStatus with SERVICE_ACCEPT_SESSIONCHANGE)). After you received the SERVICE_CONTROL_SESSIONCHANGE (WTS_SESSION_LOGON) event, get the users new session id from the WTSSESSION_NOTIFICATION structure and "save" it. If you want to start the new process you have to do the following steps. Use WTSQueryUserToken to get the primary access token. Now try to acquire TOKEN_QUERY, TOKEN_DUPLICATE, and TOKEN_ASSIGN_PRIMARY for this token by using SetTokenInformation. Last Step: CreateProcessAsUser to start your process. I'm short in time so I couldn't test it but I think this should do the work.
Greetings Covean
Thank you Covean, Sorry,I could not completely Implement the suggestion given by you. Here is what i implemented. PROCESS_INFORMATION pi; STARTUPINFO si =new STARTUPINFO(); System.IntPtr hToken; string commandLine, userPart, domainPart, password, lpCurrentDirectory; // Initialize structs si.cb = Marshal.SizeOf(si); userPart = "Prasad"; password = "pass"; domainPart = ""; commandLine = ""; lpCurrentDirectory = @"D:\DemoApplication\bin\Debug"; // Get the user token if (NativeWin32Methods.LogonUser(userPart, domainPart, password, (int)LOGON_TYPE.LOGON32_LOGON_BATCH, (int)LOGON_PROVIDER.LOGON32_PROVIDER_DEFAULT, out hToken)) { // Create structs SECURITY_ATTRIBUTES saProcessAttributes = new SECURITY_ATTRIBUTES(); SECURITY_ATTRIBUTES saThreadAttributes = new SECURITY_ATTRIBUTES(); // Now create the process as the user if (!NativeWin32Methods.CreateProcessAsUser(hToken, appPath, commandLine, ref saProcessAttributes, ref saThreadAttributes, false, 0, IntPtr.Zero, lpCurrentDirectory, ref si, out pi)) { // Throw exception throw new Exception("Failed to CreateProcessAsUser"); } } If you can tell me the modified one i would be very much thank ful to you. Prasad.
-
Thank you Covean, Sorry,I could not completely Implement the suggestion given by you. Here is what i implemented. PROCESS_INFORMATION pi; STARTUPINFO si =new STARTUPINFO(); System.IntPtr hToken; string commandLine, userPart, domainPart, password, lpCurrentDirectory; // Initialize structs si.cb = Marshal.SizeOf(si); userPart = "Prasad"; password = "pass"; domainPart = ""; commandLine = ""; lpCurrentDirectory = @"D:\DemoApplication\bin\Debug"; // Get the user token if (NativeWin32Methods.LogonUser(userPart, domainPart, password, (int)LOGON_TYPE.LOGON32_LOGON_BATCH, (int)LOGON_PROVIDER.LOGON32_PROVIDER_DEFAULT, out hToken)) { // Create structs SECURITY_ATTRIBUTES saProcessAttributes = new SECURITY_ATTRIBUTES(); SECURITY_ATTRIBUTES saThreadAttributes = new SECURITY_ATTRIBUTES(); // Now create the process as the user if (!NativeWin32Methods.CreateProcessAsUser(hToken, appPath, commandLine, ref saProcessAttributes, ref saThreadAttributes, false, 0, IntPtr.Zero, lpCurrentDirectory, ref si, out pi)) { // Throw exception throw new Exception("Failed to CreateProcessAsUser"); } } If you can tell me the modified one i would be very much thank ful to you. Prasad.