英文:
Log on to user in lock screen with simulating key strokes using SendInput?
问题
I want to use SendInput() function to login to my account like I would do in person; press enter, press the password keys, press enter again.
The purpose of this is I wrote a remote connection program that I can get screenshots to check if everything's doing ok. The program starts with task scheduler so when my pc reboots I can still send commands and get screenshots. But I want to add a login option remotely so if my pc reboots I can remotely send a signal to log in.
It works as intended on the desktop, but when I lock my user with Win+L, it doesn't do anything on the lock screen, neither on the desktop when I log on to check.
Here's the code:
void sendPassword(){
//Map to store keys as char-WORD
QMap<QString, WORD> keyMap;
keyMap["Enter"] = 0x0D;
keyMap["0"] = 0x30; keyMap["1"] = 0x31; keyMap["2"] = 0x32; keyMap["3"] = 0x33;
keyMap["4"] = 0x34; keyMap["5"] = 0x35; keyMap["6"] = 0x36; keyMap["7"] = 0x37;
keyMap["8"] = 0x38; keyMap["9"] = 0x39;
keyMap["A"] = 0x41; keyMap["B"] = 0x42; keyMap["C"] = 0x43; keyMap["D"] = 0x44;
keyMap["E"] = 0x45; keyMap["F"] = 0x46; keyMap["G"] = 0x47; keyMap["H"] = 0x48;
keyMap["I"] = 0x49; keyMap["J"] = 0x4A; keyMap["K"] = 0x4B; keyMap["L"] = 0x4C;
keyMap["M"] = 0x4D; keyMap["N"] = 0x4E; keyMap["O"] = 0x4F; keyMap["P"] = 0x50;
keyMap["Q"] = 0x51; keyMap["R"] = 0x52; keyMap["S"] = 0x53; keyMap["T"] = 0x54;
keyMap["U"] = 0x55; keyMap["V"] = 0x56; keyMap["W"] = 0x57; keyMap["X"] = 0x58;
keyMap["Y"] = 0x59; keyMap["Z"] = 0x5A;
//Get the password from a file.
QFile passFile("password.txt");
passFile.open(QIODevice::ReadOnly);
QString password = QString::fromUtf8(passFile.readAll()).split("\r\n")[0].split("\n")[0];
passFile.close();
//Initialize an inputs array.
INPUT* inputs = new INPUT[password.length()*2+4] {};
//Add the Enter button to the inputs array, key down and up.
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = keyMap["Enter"];
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wVk = keyMap["Enter"];
inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;
for(int i=0; i<password.length(); i++){
//Add the password to the inputs array character by character, key down and up.
inputs[i*2+2].type = INPUT_KEYBOARD;
inputs[i*2+2].ki.wVk = keyMap[(QString)password[i]];
inputs[i*2+3].type = INPUT_KEYBOARD;
inputs[i*2+3].ki.wVk = keyMap[(QString)password[i]];
inputs[i*2+3].ki.dwFlags = KEYEVENTF_KEYUP;
}
//Add the Enter button to the inputs array, key down and up.
inputs[password.length()*2+2].type = INPUT_KEYBOARD;
inputs[password.length()*2+2].ki.wVk = keyMap["Enter"];
inputs[password.length()*2+3].type = INPUT_KEYBOARD;
inputs[password.length()*2+3].ki.wVk = keyMap["Enter"];
inputs[password.length()*2+3].ki.dwFlags = KEYEVENTF_KEYUP;
//SendInput, send the inputs array.
SendInput(password.length()*2+4, inputs, sizeof(INPUT));
//Delete the inputs array.
delete[] inputs;
}
If this has no way to work, then how can I reach my purpose?
英文:
I want to use SendInput() function to login to my account like I would do in person;
press enter, press the password keys, press enter again.
The purpose of this is I wrote a remote connection program that I can get screenshots to check if everything's doing ok. The program starts with task scheduler so when my pc reboots I can still send commands and get screenshots. But I want to add a login option remotely so if my pc reboots I can remotely send signal to log in.
It works as intended in desktop but when I lock my user with win+L, It doesn't do anything in lock screen neither in desktop when I log on to check.
Here's the code
void sendPassword(){
//Map to store keys as char-WORD
QMap<QString, WORD> keyMap;
keyMap["Enter"] = 0x0D;
keyMap["0"] = 0x30; keyMap["1"] = 0x31; keyMap["2"] = 0x32; keyMap["3"] = 0x33;
keyMap["4"] = 0x34; keyMap["5"] = 0x35; keyMap["6"] = 0x36; keyMap["7"] = 0x37;
keyMap["8"] = 0x38; keyMap["9"] = 0x39;
keyMap["A"] = 0x41; keyMap["B"] = 0x42; keyMap["C"] = 0x43; keyMap["D"] = 0x44;
keyMap["E"] = 0x45; keyMap["F"] = 0x46; keyMap["G"] = 0x47; keyMap["H"] = 0x48;
keyMap["I"] = 0x49; keyMap["J"] = 0x4A; keyMap["K"] = 0x4B; keyMap["L"] = 0x4C;
keyMap["M"] = 0x4D; keyMap["N"] = 0x4E; keyMap["O"] = 0x4F; keyMap["P"] = 0x50;
keyMap["Q"] = 0x51; keyMap["R"] = 0x52; keyMap["S"] = 0x53; keyMap["T"] = 0x54;
keyMap["U"] = 0x55; keyMap["V"] = 0x56; keyMap["W"] = 0x57; keyMap["X"] = 0x58;
keyMap["Y"] = 0x59; keyMap["Z"] = 0x5A;
//Get password from file.
QFile passFile("password.txt");
passFile.open(QIODevice::ReadOnly);
QString password = QString::fromUtf8(passFile.readAll()).split("\r\n")[0].split("\n")[0];
passFile.close();
//Initialize inputs array.
INPUT* inputs = new INPUT[password.length()*2+4] {};
//Add enter button to inputs array, key down and up.
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = keyMap["Enter"];
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wVk = keyMap["Enter"];
inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;
for(int i=0; i<password.length(); i++){
//Add password to inputs array char by char, key down and up.
inputs[i*2+2].type = INPUT_KEYBOARD;
inputs[i*2+2].ki.wVk = keyMap[(QString)password[i]];
inputs[i*2+3].type = INPUT_KEYBOARD;
inputs[i*2+3].ki.wVk = keyMap[(QString)password[i]];
inputs[i*2+3].ki.dwFlags = KEYEVENTF_KEYUP;
}
//Add enter button to inputs array, key down and up.
inputs[password.length()*2+2].type = INPUT_KEYBOARD;
inputs[password.length()*2+2].ki.wVk = keyMap["Enter"];
inputs[password.length()*2+3].type = INPUT_KEYBOARD;
inputs[password.length()*2+3].ki.wVk = keyMap["Enter"];
inputs[password.length()*2+3].ki.dwFlags = KEYEVENTF_KEYUP;
//SendInput, send inputs array.
SendInput(password.length()*2+4, inputs, sizeof(INPUT));
//Delete inputs array.
delete[] inputs;
}
If this has no way to work, then how can I reach my purpose?
答案1
得分: 2
The problem is that when you lock the screen, Windows changes which session is displayed for security reasons. 用户模式服务和驱动程序在Session 0中运行,而首次登录的用户在Session 1中运行。实际上,sendInput()
是将按键发送到您的桌面,而不是以不同会话运行的锁定屏幕:
https://learn.microsoft.com/en-us/previous-versions/bb756986(v=msdn.10)
您可以尝试通过创造性地使用CreateProcessAsUserA()
并在lpStartupInfo
参数中指定要使用的桌面(具体而言,设置STARTUPINFOA
的lpDesktop
)来解决这个限制。这将涉及创建一个专门用于解锁屏幕的小子程序,然后让父进程使用CreateProcessAsUserA()
运行子进程。
作为一个更简单(可能更安全)的替代方案,您是否尝试使用远程桌面?
英文:
The problem is that when you lock the screen, Windows changes which session is displayed for security reasons. The usermode services and drivers runs in Session 0, and the first user to login runs in Session 1. Essentially, sendInput()
is sending the keystrokes to your desktop rather than the lock screen which is running in a different session:
https://learn.microsoft.com/en-us/previous-versions/bb756986(v=msdn.10)
You may be able to work around this limitation with creative use of CreateProcessAsUserA()
and specifying the desktop to use in the lpStartupInfo
parameter (specifically, set the lpDesktop
of the STARTUPINFOA
). This will involve having a small child program whose sole purpose is to unlock the screen, and have your parent process run the child process with CreateProcessAsUserA()
.
As a simpler (and probably more secure) alternative, have you tried using Remote Desktop?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论