我在帮助问题中找到了一些好的C#脚本"在Windows中捕获笔记本电脑盖关闭事件?" (之前发布的Stack Overflow问题)。不幸的是我无法将其转换为Powershell。我的Powershell弱点初步归功于在Windows PowerShell会话中使用Microsoft .NET Framework类以及Add-Type Cmdlet。这是因为我在这个领域没有任何正式的脚本帮助资源。我试图转换为Powershell的Stack Overflow脚本如下所示。
的问候,
标记
int ServiceMain(int argc, char** argv)
{
serviceStatusHandle = RegisterServiceCtrlHandlerExA(serviceName, (LPHANDLER_FUNCTION_EX) ServiceControlHandler, 0);
//...
lidcloseRegHandle = RegisterPowerSettingNotification(serviceStatusHandle, &GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_SERVICE_HANDLE);
//...
}
/**
* Event handler for windows service.
*/
void WINAPI ServiceControlHandler(DWORD controlCode, DWORD evtype, PVOID evdata, PVOID Context)
{
switch (controlCode)
{
//...
case SERVICE_CONTROL_POWEREVENT:
WriteToLog("Service Control: SERVICE_CONTROL_POWEREVENT builds and fwd the msg");
msg.control = SERVICE_CONTROL_POWEREVENT;
msg.event_type = (int) evtype;
msg.event_data = evdata;
//...
}
}