如何在 Windows XP 上注销之前运行我的程序?
我正在寻找“RunOnceEx”的反版本。
RunOnceEx 确实在用户的 shell(桌面和任务栏)启动之前运行一些程序。 在 runonceex 完成之前,登录进度不会继续。
我想做完全相同的事情,但是在用户注销时。 当她/他注销时,所有正在运行的程序都会关闭,离开外壳(桌面和任务栏),然后“我希望我的程序此时会执行”,最后注销。
我认为这是可能的,因为“mobsync.exe”正在这样做。 但我找不到在哪里以及如何做。
I am looking for an inverse version of "RunOnceEx".
RunOnceEx does run some program, before the user's shell(desktop&taskbar) start. The login progress will not continue before the runonceex complete.
I want to do exact the same but on user logout.
When she/he logout, all running program shutdown, leaving shell(desktop&taskbar), then ""I wish my program will be execute this moment"", finally logout.
I think it is possible because the "mobsync.exe" is doing that. But I cannot find where and how to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
警告,正如此处所述、
gpedit.msc
将允许您为所有用户配置注销脚本。如果您仅需要为一个用户使用该脚本,则需要直接在注册表中声明它,无论是在
HKCU
还是HKLM
中。Warning, as said here,
gpedit.msc
will allow you to configure a logoff script for all users.If you need that script only for one user, you need to declare it directly in the registry, both in
HKCU
andHKLM
.要仅为当前用户运行此操作,您可以使用 WMI 在发生关闭/注销时获取信息。
您可以编写一个小型 C#(或任何其他可以使用 WMI 的语言)应用程序或 vbs 脚本来侦听 Win32_ComputerShutdownEvent WMI 事件。
可以在此问题中找到示例 C# 应用程序:从系统获取注销事件
To run this only for the current user, you can use WMI to get an information when a shutdown/logout occurs.
Either you write a small C# (or any other language that can use WMI) application or vbs script to listen on the Win32_ComputerShutdownEvent WMI event.
An example C# app can be found here in this question: Get Log off event from system
在谷歌上为我找到的第一个结果
要执行程序,您可以创建一个脚本来运行它并使用组策略来强制执行它。
在组策略编辑器中,导航至用户配置 --> Windows 设置 --> 脚本(登录/注销)
更多信息请点击此处
found in the first result on google for me
To execute a program you can create a script to run it and use group policy to enforce it.
In Group Policy Editor navigate to User Configuration-->Windows Settings-->Scripts (Logon/Logoff)
more information here
如果您希望正在运行的程序在注销时执行代码,那么您应该挂钩
WM_QUERYENDSESSION
消息并查找lParam
值ENDSESSION_LOGOFF
(0x80000000 )。测试此
lParam
值非常重要,因为其他值表示“强制关闭” - 即您的进程可能在您的代码被允许运行之前被终止。 事实上,大多数关闭/会话结束消息只是为了让您有机会运行最后一刻的清理代码,并且响应长时间运行的操作并不安全; 但这种特殊的组合应该没问题。注意:我从未尝试过实际运行单独的进程来响应
WM_QUERYENDSESSION
消息。 窗口管理器可能会不允许这样做,就像在关闭期间一样。 我想,尝试一下看看。如果您处于 .NET 环境中(未指定),更快的方法是将事件处理程序添加到
Microsoft.Win32.SystemEvents.SessionEnding
事件。If you want a running program to execute code on logoff, then you should hook the
WM_QUERYENDSESSION
message and look for anlParam
value ofENDSESSION_LOGOFF
(0x80000000).It's important to test for this
lParam
value because the other ones indicate a "forced close" - i.e. your process may be killed before your code is even allowed to run. In fact, most shutdown/session-end messages are only intended to give you an opportunity to run last-minute cleanup code and aren't that safe to respond to with long-running actions; but this particular combination should be OK.Note: I've never tried to actually run a separate process in response to the
WM_QUERYENDSESSION
message. It's possible that the window manager will disallow this, like it does during shutdown. Try it and see, I guess.If you're in a .NET environment (you didn't specify), a quicker way is to add an event handler to the
Microsoft.Win32.SystemEvents.SessionEnding
event.您需要的是 GINA 的实现。 您可以在 WlxIsLogoffOk 中运行自定义命令 这
创建正确的 GINA dll 后,您可以在此处注册它: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\@GinaDLL
是一个实现这可能适合您的需求(它提供了一个 Logoff 注册表项,您可以在其中指定命令):
http://wwwthep.phyk.uni-mainz.de/~ frink/newgina_pre09/readme.html
What you need is an implementation of GINA. You can run your custom commands in WlxIsLogoffOk function, which gets called when the user initiates a logoff
Once you create the proper GINA dll you can register it here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\@GinaDLL
Here is an implementation which may fit your needs (it provides a Logoff registry key where you could specify your command):
http://wwwthep.physik.uni-mainz.de/~frink/newgina_pre09/readme.html
正如 VonC 和 TFD 已经提到的,组策略编辑器只是操纵注册表的另一种方式。
只需使用 gpedit 进行您喜欢的更改(在“用户配置”-“Windows 设置”-“脚本”中),然后查看注册表中的
[HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\System\Scripts]
了解如何直接做到这一点。
在我的电脑上(挂在域中)还有一个隐藏文件夹
C:\WINDOWS\System32\GroupPolicy
,其中包含用户和计算机的子文件夹。 两者都有名为“关闭”和“启动”的附加子文件夹。 也许你也可以使用这些。As VonC and TFD already mentioned, the Group Policy Editor is just another way to manipulate the registry.
Just make with gpedit the changes (in Userconfig - Windows Settings - Scripts) you like and afterwards take a look in the registry at
[HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\System\Scripts]
to find out how you can do that directly.
Also on my PC (hanging in a domain) is a hidden folder
C:\WINDOWS\System32\GroupPolicy
with subfolders for user and machine. Both having additional subfolders called Shutdown and Startup. Maybe you can also use these ones.如果您需要简单的东西并为单个(或任何)用户工作,您可以使用 C++ 或 C# 制作一个简单的应用程序。
最简单的方法是在托盘中使用 C#(只需将托盘组件添加到表单中)并为 FormClosing 事件注册事件处理程序。 它看起来像这样:
因此您的应用程序将通过 Windows 或用户启动。 它会等待(使用一点内存),并在系统关闭或用户注销等时执行某些操作(通过检查上面的“CloseReason”)。
If you need something simple and working for a single (or any) user you can make a simple application in C++ or C# for example.
The simplest is having a C# in tray (by simply adding the tray component to the form) and register and event handler for the FormClosing event. It'd look like this:
So your application will be started with Windows or with the user. It'll wait (using a little bit of memory) and will do something when the system shuts down, or the user log off, etc (by checking "CloseReason" above).