I have a notebook with PID: 2860
#include <iostream>
#include <windows.h>
#include <psapi.h>
using namespace std;
HWND SendIt (DWORD dwProcessID){
HWND hwnd = NULL;
do {
hwnd = FindWindowEx(NULL, hwnd, NULL, NULL);
DWORD dwPID = 0;
GetWindowThreadProcessId(hwnd, &dwPID);
if (dwPID == dwProcessID) {
cout<<"yay:"<<hwnd<<":pid:"<<dwPID<<endl;
PostMessage(hwnd,WM_KEYDOWN,'A',1);
}
} while (hwnd != 0);
return hwnd;
}
int main()
{
SendIt(2680);
return 0;
}
and the notebook should write A, but nothing will happen.
I tried a message on it WM_DESTROYand it works, but it WM_KEYDOWNdoes not work.
I also did GetLastError(), and it prints error 2 ERROR_FILE_NOT_FOUND.
Why does this not work, and can it be fixed?
source
share