Is it possible to start notepad from a service in the logged on user? #80071
-
I have a gRPC service that is hosted in a ASP Core application. This application is installed in as windows service. I have a user A in the computer, that log on automatically when the computer starts. The gRPC service receive commands from a remote client, for example, open the notedpad or another UI application. I would like to open this applications in the logged on user. However, when I send the command to open the notepad, the application is not opened. Instead of install the ASP core application as windows service, I have tried to create a scheduled task that starts when the user log on. In this case, it works. The problem is that the application opens a log windows, and I would like to avoid this. So to avoid to open the log window, I configure the scheduled task to start no matter if the user log on or not. In this case the log window of the ASP application is not opened, but then the notepad doesn't open. The behavior is the same than if I install my ASP application as windows service. So my question is: is there some way to open the notepad or any other UI application in the logged user from a windows service? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Beta Was this translation helpful? Give feedback.
-
This is not really a .NET question but really about Windows security and desktop model. You might get a good answer sooner asking on stackoverflow. |
Beta Was this translation helpful? Give feedback.
-
Yes, I know that perhaps it is more related with Windows than .NET, but I was wondering if it would be some way to do it with C# directly. For example i know that in C it is possible with the method CreatedProcessAsUserA(), so I was wondering if in .NET there would be something similiar like this. |
Beta Was this translation helpful? Give feedback.
-
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process Issue DetailsI have a gRPC service that is hosted in a ASP Core application. This application is installed in as windows service. I have a user A in the computer, that log on automatically when the computer starts. The gRPC service receive commands from a remote client, for example, open the notedpad or another UI application. I would like to open this applications in the logged on user. However, when I send the command to open the notepad, the application is not opened. Instead of install the ASP core application as windows service, I have tried to create a scheduled task that starts when the user log on. In this case, it works. The problem is that the application opens a log windows, and I would like to avoid this. So to avoid to open the log window, I configure the scheduled task to start no matter if the user log on or not. In this case the log window of the ASP application is not opened, but then the notepad doesn't open. The behavior is the same than if I install my ASP application as windows service. So my question is: is there some way to open the notepad or any other UI application in the logged user from a windows service? Thanks.
|
Beta Was this translation helpful? Give feedback.
-
Even CreateProcessAsUser / WithToken would not change the outcome - it needs to happen from within an interactive login session. Neither the Windows Service nor the background scheduled task are from an interactive login session. If all that prevents you from using a regular scheduled task is the fact you have a console showing up for your program and you do not want that, consider changing the subsystem from Console to Windows (or in csproj terms - a OutputType of WinExe). If this is not enough, then you will probably need another layer of IPC. Right now you have this which does not work:
It really only depends on what your specific needs are, but yes this is entirely about Windows details, and not about dotnet. If you need tight control about process creation (such as inheriting specific handles), then you may need to PInvoke one of the CreateProcess* APIs, but otherwise this is not about dotnet. |
Beta Was this translation helpful? Give feedback.
-
@fbrosseau thanks for the explanation, but I have some doubts about that. If I don't understand wrong, you say that from a scheduled task I can't open the notedpad because a scheduled task doesn't belong the interactive environment. But in my tests, I have create a console application that opens the notepad, I create an scheduled task that runs this application and the notepad is opened. With that I try to simulate to create a scheduled task that run a C# application that opens the notepad. However, if the scheduled task runs a C# pplication that is a ASP Net Core that hosts a gRPC service is this gRPC service who tries to open the notepad, it doesn't run the notepad. Well, really if I open the task manager, I can see the process notepad.exe is started, but it stopped immediately, I can't see the windows of the notepad, but at least I know that the process is started. So it seems that really the notepad is started, but it is closed immediatelly. But I don't understand why it happens with the ASP Net Core application it doesn't happen when I run a console application. But I have tried to run the ASP Net Core manually, in this case the gRPC the notepad is opened and it is not closed and also I can see the window. So it seems too that the problem is with ASP Net Core application and the scheduled task. I understand that perhaps it is not a .NET issue, that perhaps it is more about a Windows behavior, but how it happens with the ASP application and not with the console application, it makes me think that perhaps it is a problem related with Windows and ASP. In summary: 1.- ASP application running manually (double clicking the .exe file): it opens and keep opened the notepad. So it works as expected. In my case really I don't mind if I have to run manually the ASP application, but when I start the application, it is opened a powershell window with the logging, and I would like to avoid this. As you tell, it is possible if I change the output type of the application from console application to WinExe and this works. Thanks for the help. |
Beta Was this translation helpful? Give feedback.
Even CreateProcessAsUser / WithToken would not change the outcome - it needs to happen from within an interactive login session. Neither the Windows Service nor the background scheduled task are from an interactive login session.
If all that prevents you from using a regular scheduled task is the fact you have a console showing up for your program and you do not want that, consider changing the subsystem from Console to Windows (or in csproj terms - a OutputType of WinExe).
If this is not enough, then you will probably need another layer of IPC. Right now you have this which does not work: