Node-Windows - Run GUI app on Logon screen

你。 提交于 2019-12-06 07:16:43

The answer is 'paexec'. psexec works exactly the same, but paexec is open-source, freely distributable, and usable in your apps for both personal and commercial applications.


Requirements:

Download paexec.exe .

Place it where you can execute it, OR cd to the folder you put it in, OR place it in the command path, OR add the folder you put it in to the command path, OR reference it by pathname (i.e. c:\folder\subfolder\paexec -your_options).


Recommended:

Execute this from a Windows Service (in my case an npm node-windows service). This allows you to run it as SYSTEM. There are other ways, but this one seems to work the best.


Example use:

paexec -d -s -x -w c:\path\to\your\app_folder cmd /c app.exe -your_options

How this works:

-d prevent paexec from waiting for app to exit, therefore allows non-blocking.

-s executes as SYSTEM user.

-x causes it to run on the logon screen

-w sets the current working directory

cmd /c yourappname.exe allows you to run a GUI interactive app with the previous options (it doesn't seem to work without opening the prompt first).


Addition considerations:

Using 'cmd /c', you can call a .bat or .cmd script and use 'start /b your_app.exe' to make the cmd.exe window go away (otherwise you are stuck with it on your logon screen until you close it or your app).

Your app will continue running after logging in, however it won't be running in the users login session, so if you want your app to do both, I recommend using 'taskkill /f /im your_app.exe' or other means to stop in and then running on the users login session.

You can run it on the user's desktop session via:

paexec -d -s -i 1 -w... (the rest is the same as above).

...assuming there is only one user logged in, they will be on Session 1. This is what the '-i 1' option does, is run it on session 1. If there is more than one user logged in (i.e. via Switch User etc.) they may have a different Session number, so just replace '-i 1' with '-i x' where 'x' is the users Session ID.

You can lookup the Session id via Windows command 'tasklist /v'.

If you're trying run this from a node.js service, you will need to use process_child built-in module and spawn a cmd prompt to run it from. There are lots of examples for this, so not tackling that here. The nice thing about running it from a Windows Service is it won't open a visible window, and you can provide i/o via the standard child_process.spawn() methods and events.


Security:

Running this from a Windows Service (i.e. via node-windows and child_process.spawn) allows you to run your GUI app not only on the logon screen, but at boot-time before login, GUI or not-- without any of the typical insecure hacks, like autologon to a special user account then locking the screen or logging out, etc. It is actually using a tool that mirrors MicroSoft/SysInternals own tool for doing this under MS's officially prescribed methods.

The weak-point here is only your app, so button it up and be safe!


Note: Obvi this is a complicated issue, which is probably why there don't seem to be any good answers-- and likely the few who have figured it out don't want to share. So here you go.

Enjoy!


PS: You could be really Evil with this. Please don't use it for nefarious purposes. That's exactly why they have made it so difficult. Be good, so we can all benefit from it's power.


Aside Regarding task names:

If you wish to refer to your node or nw.js app, and see it as yourappname.exe in tasklist and task manager, you can simply rename the node.exe and nw.exe executables to whatever you want them to show as (i.e. rename nw.exe to myapp.exe and it will show and be referred to as myapp.exe in Windows). This is assuming you have a local copy of node.exe etc. in your app folder... Don't do this with the globally installed executable!


One more thing:

If anyone has a better, tried and tested method, please add it as an answer!

If it works for me, I will gladly unaccept my answer and accept yours!!

Thank you so much for sharing your discovery! Here's another piece that may help others:

If you don't want to make a separate app to just start a service, you can use NSSM (https://nssm.cc/). Download it and run the following to create the service:

nssm install your-app-name c:\tmp\paexec.exe -s -x -w c:\path\to\your\app_folder cmd /c app.exe -your_options

Then, starting the service will launch paexec in blocking mode (so if the service is running, paexec is running, and hence, your app is running), with the same flags as jdmayfield's awesome answer.

If anyone has any suggestions on how to get remote desktop access working on Windows without having to switch between the service session and the user session, please share it!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!