.NET 中的 VMware 事件挂钩

发布于 2024-09-04 23:48:37 字数 1036 浏览 4 评论 0原文

我正在开发一个内部 .NET 应用程序,该应用程序将在虚拟机(使用 VMware)上运行,并且想知道是否有一种方法可以从虚拟机系统事件(如挂起、恢复等)获取通知

。有一个方便的方法吗?虚拟机安装了 VMware Tools,是否提供了用于挂钩事件的 .NET API?

编辑:特别是,我对系统刚刚恢复的时间感兴趣。我认为这与任何“常规”Windows 系统事件都不对应(毕竟,挂起和恢复虚拟机的全部意义在于 Windows 不知道发生了什么)。我错了吗?这会触发事件吗?

编辑2:我编写了这个快速控制台应用程序来挂钩我能想到的所有系统事件,并且当我挂起/恢复时什么也得不到:

static void Main(string[] args) {

    SystemEvents.DisplaySettingsChanged += (sender, e) => Console.WriteLine("Display settings changed");
    SystemEvents.EventsThreadShutdown += (sender, e) => Console.WriteLine("Events thread shutdown");
    SystemEvents.PowerModeChanged += (sender, e) => Console.WriteLine("Power mode changed");
    SystemEvents.SessionEnding += (sender, e) => Console.WriteLine("Session ending");
    SystemEvents.SessionSwitch += (sender, e) => Console.WriteLine("Session switch");
    SystemEvents.UserPreferenceChanging += (sender, e) => Console.WriteLine("User preference changing");

    Console.ReadLine();
}

I'm developing an in-house .NET application that will be run on a VM (with VMware), and want to know if there's a way to get notifications from VM system events (like suspending, resumed, etc.)

Anyone know of a convenient way to do that? The virtual machine has VMware Tools installed, does that provide a .NET API for hooking events?

EDIT: In particular, I'm interested in when the system has just resumed. I assumed that this doesn't correspond to any "regular" Windows system event (after all, the whole point of suspending and resuming a VM is that Windows has no idea what happened). Am I mistaken? Will that trigger an event?

EDIT 2: I wrote this quick console app to hook all the System Events I could think of, and get nothing when I suspend/resume:

static void Main(string[] args) {

    SystemEvents.DisplaySettingsChanged += (sender, e) => Console.WriteLine("Display settings changed");
    SystemEvents.EventsThreadShutdown += (sender, e) => Console.WriteLine("Events thread shutdown");
    SystemEvents.PowerModeChanged += (sender, e) => Console.WriteLine("Power mode changed");
    SystemEvents.SessionEnding += (sender, e) => Console.WriteLine("Session ending");
    SystemEvents.SessionSwitch += (sender, e) => Console.WriteLine("Session switch");
    SystemEvents.UserPreferenceChanging += (sender, e) => Console.WriteLine("User preference changing");

    Console.ReadLine();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

划一舟意中人 2024-09-11 23:48:37

您是否会监听任何特定于 vmware 的事件?否则,听起来您最好从 Windows 监听这些事件,


看看 WM_POWERBROADCAST -- http://www.pinvoke.net/default.aspx/user32.RegisterPowerSettingNotification" pinvoke.net/default.aspx/user32.RegisterPowerSettingNotification

are there any vmware-specific events you'd be listening for? Otherwise it sounds like you'd be better off listening for those events from Windows


Take a look at WM_POWERBROADCAST -- http://www.pinvoke.net/default.aspx/user32.RegisterPowerSettingNotification

荭秂 2024-09-11 23:48:37

好的,所以我已经放弃为此寻找一个简单的 .NET 挂钩,但如果其他人偶然发现了这个问题并想知道我是如何解决它的:

我的应用程序中有一个定期触发的计时器(每 10 秒) ,并将当前时间与上次时间进行比较。如果时间明显长于 10 秒,我认为计算机已睡眠或挂起,并根据需要刷新我的应用程序。

有点 hacky,但计时器不会增加程序的 CPU 或内存使用量,所以我认为这并不可怕。

OK, so I've given up on looking for an easy .NET hook for this, but if someone else stumbles on this and wants to know how I solved it:

I have a timer in my app that fires regularly (every 10 seconds), and compares the current time to the last time. If the time is appreciably longer than 10 seconds, I assume that the computer has either been asleep or suspended, and refresh my app as needed.

A little hacky, but the timer adds nothing to the program's CPU or memory usage, so I figure it's not horrible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文