在用户注销时删除 HKEY_CURRENT_USER 值 [英] Delete HKEY_CURRENT_USER value on user log off

查看:47
本文介绍了在用户注销时删除 HKEY_CURRENT_USER 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 服务在 HKEY_CURRENT_USER 注册表项下为每个登录用户创建一个注册表值(用于 Excel 加载项)(通过调用 ImpersonateLoggedOnUser()RegSetValueEx()).我需要在用户注销时删除此注册表值,包括系统关闭.如果在注销时未将其删除,并且该软件由一个用户卸载,则注册表中的条目将保留给在服务生命周期内登录的任何其他用户,这会导致每次 Excel 启动时显示消息框错误因为它正在尝试加载一个不再存在的加载项.

考虑但拒绝了以下内容:

  • SetConsoleCtrlHandler() 因为没有任何用户正在注销的指示.
  • REG_OPTION_VOLATILE 因为它仅在创建密钥时有效,而我只是在创建一个值(没有彻底调查所以即使我创建了一个密钥也可能不是解决方案).

有没有其他机制可以解决这个问题?Windows 版本为 XP、Vista 和 7.

解决方案

既然你已经在使用服务,你的生活(应该)很轻松.事实上,您可以注册自己以接收 SERVICE_CONTROL_SESSIONCHANGE 事件.特别是,您需要查找 WTS_SESSION_LOGOFF 原因.

您必须在服务控制例程中注册这些事件,在启动时添加SERVICE_ACCEPT_SESSIONCHANGE.当事件是 SERVICE_CONTROL_SESSIONCHANGE 时,lpEventData 参数是一个指向 WTSSESSION_NOTIFICATION 结构的指针,其中包含有关当前终止的会话的信息(因此,关于用户注销).

查看 MSDN1, MSDN2, MSDN3 -包含感兴趣会话的 dwSessionId 的数据结构

查看此相关(但不重复)问题>

也就是说,我发现 João Augusto 解决方案更清洁;我会用它来解决类似的问题;但是,我想添加此解决方案以回答更广泛的问题(供将来参考读者)

另一种方法是使用 SENS,请查看此 MSDN 文章

A windows service creates a registry value (for an Excel add-in) under HKEY_CURRENT_USER registry key for each logged on user (by calling ImpersonateLoggedOnUser() and RegSetValueEx()). I need to delete this registry value when a user logs off, including system shutdown. If it is not deleted at log off, and the software is uninstalled by one user then the entry in the registry remains for any other user that logged on during the lifetime of the service which causes a message box error to be displayed each time Excel begins because it is attempting to load an add-in that no longer exists.

Considered but rejected the following:

  • SetConsoleCtrlHandler() because there is no indication of what user is logging off.
  • REG_OPTION_VOLATILE because it is effective only when creating keys and I am only creating a value (did not thoroughly investigate so may not have been solution even if I was creating a key).

Are there any other mechanisms that would provide a solution to this? Windows versions are XP, Vista and 7.

解决方案

Since you are already on a service, your life (should be) is easy. In fact you can register yourself to receive the SERVICE_CONTROL_SESSIONCHANGE event. In particular, you will want to look for the WTS_SESSION_LOGOFF reason.

You have to register for these events in your service control routine, at startup, adding SERVICE_ACCEPT_SESSIONCHANGE. When the event is SERVICE_CONTROL_SESSIONCHANGE, the lpEventData parameter is a pointer to a WTSSESSION_NOTIFICATION structure, with information about the session currently terminating (thus, about the user logging off).

Check out the details on MSDN1, MSDN2, MSDN3 - the data structure that contains the dwSessionId of the interesting session

Check out this related (but not duplicate) question too

That said, I find João Augusto solution cleaner; I would use that for a similar problem; however, I wanted to add this solution for having an answer to the wider question (for future reference readers)

EDIT: Another method is to use SENS, check this MSDN article

这篇关于在用户注销时删除 HKEY_CURRENT_USER 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆