1.一开始,决定通过keybd_event()来模拟键盘
keybd_event(VK_CONTROL,MapVirtualKey(VK_CONTROL,0),0,0); keybd_event(VK_MENU,MapVirtualKey(VK_MENU,0),0,0); keybd_event(VK_DELETE,MapVirtualKey(VK_DELETE,0),0,0); keybd_event(VK_DELETE,MapVirtualKey(VK_DELETE,0),KEYEVENTF_KEYUP,0); keybd_event(VK_MENU,MapVirtualKey(VK_MENU,0),KEYEVENTF_KEYUP,0); keybd_event(VK_CONTROL,MapVirtualKey(VK_CONTROL,0),KEYEVENTF_KEYUP,0); 发现只能模拟ctrl+alt两个键的效果,然而其他的两个键的都可以模拟比如win+d。不知道是不是keybd_event()只能模拟两键还是因为ctrl+alt+delete的特殊性,望高手告之,在此谢过。
2.运用PostMessage(HWND_BROADCAST, WM_HOTKEY, 0, MAKELONG(MOD_ALT | MOD_CONTROL, VK_DELETE));
来发送虚拟键盘,但是在winNT以后的系统里我们还有很多事情要做,比如:OpenDesktop()、OpenInputDesktop()、GetThreadDesktop()、SetThreadDesktop()、CloseDesktop()、GetUserObjectInformation()
代码如下:
#include "windows.h"
BOOL simulateAltControlDel();
void main()
{
simulateAltControlDel();
}
BOOL simulateAltControlDel()
{
HDESK hdeskCurrent;
HDESK hdesk;
HWINSTA hwinstaCurrent;
HWINSTA hwinsta; // Save the current Window station
hwinstaCurrent = GetProcessWindowStation();
if (hwinstaCurrent == NULL) return FALSE;
// Save the current desktop //
hdeskCurrent = GetThreadDesktop(GetCurrentThreadId());
if (hdeskCurrent == NULL) return FALSE;
hwinsta = OpenWindowStation("winsta0", FALSE,
WINSTA_ACCESSCLIPBOARD | WINSTA_ACCESSGLOBALATOMS | WINSTA_CREATEDESKTOP | WINSTA_ENUMDESKTOPS | WINSTA_ENUMERATE | WINSTA_EXITWINDOWS | WINSTA_READATTRIBUTES | WINSTA_READSCREEN | WINSTA_WRITEATTRIBUTES);
if (hwinsta == NULL) return FALSE; // // Set the windowstation to be winsta0 //
if (!SetProcessWindowStation(hwinsta)) return FALSE;
hdesk = OpenDesktop("Winlogon", 0, FALSE,
DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL | DESKTOP_JOURNALPLAYBACK | DESKTOP_JOURNALRECORD | DESKTOP_READOBJECTS | DESKTOP_SWITCHDESKTOP | DESKTOP_WRITEOBJECTS);
if (hdesk == NULL) return FALSE;
if (!SetThreadDesktop(hdesk)) return FALSE; PostMessage(HWND_BROADCAST,WM_HOTKEY,0,MAKELPARAM(MOD_ALT|MOD_CONTROL,VK_DELETE));
if (!SetProcessWindowStation(hwinstaCurrent)) return FALSE;
if (!SetThreadDesktop(hdeskCurrent)) return FALSE;
if (!CloseWindowStation(hwinsta)) return FALSE;
if (!CloseDesktop(hdesk)) return FALSE; return TRUE;}
偶一开始试验了发现不成功,后来冰河大哥告诉我说OpenDesktop("Winlogon", ......)本身需要LocalSystem权限,果然如此,把它注册成服务,然后效果实现。相信如何注册成服务不用我说了吧。ok,我们想要的功能实现了。