用C++判断Windows是否锁屏

点击查看代码
#include "windows.h"
#include "Windows.h"
#include "Wtsapi32.h"
#include <tchar.h>
#pragma comment(lib, "WtsApi32.lib")  
 
using namespace std;
 
typedef HDESK(WINAPI *PFNOPENDESKTOP)(LPSTR, DWORD, BOOL, ACCESS_MASK);
typedef BOOL(WINAPI *PFNCLOSEDESKTOP)(HDESK);
typedef BOOL(WINAPI *PFNSWITCHDESKTOP)(HDESK);
 
bool DLL_isScreenLocked()
{   
	BOOL bLocked = FALSE;
 
	static HMODULE hUser32 = LoadLibrary(_T("user32.dll"));
 
	if (hUser32)
	{
		static PFNOPENDESKTOP fnOpenDesktop = (PFNOPENDESKTOP)GetProcAddress(hUser32, "OpenDesktopA");
		static PFNCLOSEDESKTOP fnCloseDesktop = (PFNCLOSEDESKTOP)GetProcAddress(hUser32, "CloseDesktop");
		static PFNSWITCHDESKTOP fnSwitchDesktop = (PFNSWITCHDESKTOP)GetProcAddress(hUser32, "SwitchDesktop");
 
		if (fnOpenDesktop && fnCloseDesktop && fnSwitchDesktop)
		{
			HDESK hDesk = fnOpenDesktop("Default", 0, FALSE, DESKTOP_SWITCHDESKTOP);
 
			if (hDesk)
			{
				bLocked = !fnSwitchDesktop(hDesk);
				fnCloseDesktop(hDesk);
			}
		}
	}
 
	if (bLocked == FALSE)
	{
		return false;
	}
	else
	{
		return true;
	}
}

https://stackoverflow.com/questions/29326685/c-check-if-computer-is-locked/32115698#32115698

https://blog.csdn.net/steppedby/article/details/76887383?utm_medium=distribute.pc_relevant.none-task-blog-2

补充,自动关机服务程序,当用户处于锁定状态或者注销状态,会自动执行关机。

posted @ 2024-06-13 22:07  putiji  阅读(74)  评论(0编辑  收藏  举报