0

I have a Windows Service and a UI application which are developed on .NET (C#). When I start windows service, windows service will start UI application corresponding with logon user => Successfully

In my application, I have a button to open a log file by default editor by coding like that:

// filePath is path to text file
// Example: C:\UserData\logs\abc.log
System.Diagnostics.Process.Start(filePath);

If I set default editor of log file is notepad or wordpad, it work ok.
But if I set default editor is Notepad++, It will show messagebox:

  1. Load langs.xml failed!
  2. Load stylers.xml failed!

Another, If I open this text file by Notepad++ by double-click, it work ok. These errors only show when I open file by C# code.

Enviroment:

  1. .NET framework 4.7.1
  2. Windows 10 32-bit
  3. Notepad++ 7.6.4 (32-bit)
  4. Application is started by Windows Service as same as Logon Session of active user.

enter image description here

How can I solve this issue?

GSP
  • 574
  • 3
  • 7
  • 34
  • 2
    is it .net standard or .net core? – Ulugbek Umirov Mar 13 '19 at 05:03
  • What version of Notepad++ are you using? – Ian Mercer Mar 13 '19 at 05:04
  • 1
    My psychic powers tell me that either your Windows Explorer and your C# application are running at different trust levels, or that the environment that Explorer creates when implicitly launching Notepad++ is missing from your `Process.Start` call. I'd recommend a tool like Process Monitor to check and compare what happens when you run the two scenarios. – Ian Kemp Mar 13 '19 at 05:12
  • Do you get the same result from a 32-bit C# app and also a 64-bit C# app? – Ben Voigt Mar 13 '19 at 05:14
  • 1
    take a look at https://superuser.com/questions/67128/notepad-is-unable-to-load-langs-xml-why **and** https://notepad-plus-plus.org/community/topic/14896/load-langs-xml-stylers-xml-failed as it stands this is not really a C# problem, its a notpad++ problem or environmental problem. – TheGeneral Mar 13 '19 at 05:24
  • @MichaelRandall These links do not explain why there is no error when file is opened from Explorer. There is definitely some issue with Process.Start as Ian Kemp has mentioned, it may be UseShellExecute setting or something else. – Ulugbek Umirov Mar 13 '19 at 05:30
  • @UlugbekUmirov, Ian Mercer I updated environment in question, please take a look on it. – GSP Mar 13 '19 at 06:50
  • @Ian Kemp This application is started from Windows Service. I think that is reason why I got this issue. I try to call this code from normal application (start by myself), it work OK. – GSP Mar 13 '19 at 06:58
  • @GSP If you had included that most vital information - namely that you're trying to run this from a Windows Service - then we would have been able to help you much sooner: https://stackoverflow.com/questions/5063731/is-there-any-way-to-start-a-gui-application-from-a-windows-service-on-windows-7 – Ian Kemp Mar 13 '19 at 07:12
  • @ Ian Kemp, Damien_The_Unbeliever I think it is not duplicate with your suggestion link – GSP Mar 13 '19 at 07:47
  • Why not - it's the same issue that's been asked about time and again. Services run in session 0, which is a non-interactive session and they don't launch interactive applications. And the solutions are the same - you run a separate process *in* the session you want to launch programs in (say set as a startup program with no UI of its own) that then talks to the service via some IPC mechanism and gets told when to launch programs. – Damien_The_Unbeliever Mar 13 '19 at 07:55
  • Which also lets you consider what should happen a) when nobody is logged in (no interactive sessions running at all) and b) when multiple users are logged in (so multiple interactive sessions to choose from) – Damien_The_Unbeliever Mar 13 '19 at 07:56
  • @Damien_The_Unbeliever In Windows Service, I only start UI application on OnSessionChange (Logon, SessionUnlock) and only I got active username successfully. And I started UI application successfully corresponding active user. The issue is start notepad++ on UI application – GSP Mar 13 '19 at 08:05
  • Yes, the service can receive those notifications. No, it doesn't change that *its running in session 0* and any process it launches will also be running in session 0. Those events don't magically move the service process *into* those sessions. – Damien_The_Unbeliever Mar 13 '19 at 08:10
  • @Damien_The_Unbeliever I use command prompt to check current sesison id of applicaiton process which is started by Windows service (query process /id:{process_id}. I see that application is in ID 1 (as same as explorer.exe) while windows service is in ID 0. Although application is started by windows service, application process is ran into user session (not sesison 0). Another If I open Task Manager, and expand active user on Users tab, I still see my application on it. – GSP Mar 13 '19 at 08:19

0 Answers0