Skomentowałem w większości tego, co wiem. Jestem dość pewny, że problem pojawia się w AttachThreadInput. Miałem na myśli tylko funkcję 32-bitową. Uwierz mi, gdybym mógł rozwiązać to sam, byłbym szczęśliwy. Przeczytałem pełną dokumentację zdarzeń w Windows (here) i nie jestem bliżej rozwiązania. Jeśli masz jakieś pomysły, chciałbym je usłyszeć.C - win32: AttachThreadInput & SetFocus, 64bit: bez wskazówki
#include <stdio.h>
#include <windows.h>
int main()
{
//Structure prereqs for CreateProcess
STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));
siStartupInfo.cb = sizeof(siStartupInfo);
if(CreateProcess("c:\\windows\\notepad.exe", "", 0, 0, FALSE, CREATE_DEFAULT_ERROR_MODE, 0, 0, &siStartupInfo, &piProcessInfo) == FALSE)
{
GetLastError();
}
Sleep(1000);
//Target thread, I can't seem to get this to return anything !0
DWORD dwTargetThread = GetWindowThreadProcessId(piProcessInfo.hProcess,NULL);
//For example:
//if(dwTargetThread == 0) return -1;
//Print debugging info
if (GetCurrentThreadId() == dwTargetThread) return -1; else printf("\nMy thread: %u\n\npiProcessInfo.hThread: %u\n\nDWORD dwTargetThread: %u\n\nunsigned int dwTargetThread: %u", GetCurrentThreadId(), piProcessInfo.hThread,dwTargetThread, GetWindowThreadProcessId(piProcessInfo.hProcess,NULL));
//I've tried using piProcessInfo.hThread for AttachTo but I can't cast it to a DWORD as it's 64bit
AttachThreadInput(GetCurrentThreadId(),dwTargetThread,TRUE);
printf("\n\nAttached...\n");
Sleep(1000);
//Set the focus & bring to foreground
SetFocus(piProcessInfo.hProcess);
printf("Focus set...\n");
Sleep(1000);
SetForegroundWindow(piProcessInfo.hProcess);
printf("Brought to foreground...\n");
Sleep(1000);
//I know I shouldn't use PostMessage for keyboard input but it's just for the example
PostMessage(piProcessInfo.hProcess, WM_CHAR, 'g', 0);
printf("Message queued\n");
//No better than SetForegroundWindow:
//SetWindowPos(piProcessInfo.hProcess, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
Czy jesteś pewien, że wywołanie 'CreateProcess()' się powiodło? Wszystko wydaje się być zależne od tego i twój kod robi to samo (poza wywołaniem 'GetLastError()'). – MatthewD
Co zwraca funkcja "GetLastError() po wywołaniu funkcji" GetWindowThreadProcessId() "? Zobacz dolny komentarz na tej stronie: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522%28v=vs.85%29.aspx – MatthewD
Tak, 'CreateProcess()' zakończyło się pomyślnie. Notatnik otwiera się idealnie, a funkcja SendInput działa, jeśli ręcznie wprowadzę ją na pierwszy plan. 'GetLastError()' zwraca null po 'GetWindowThreadProcessId (piProcessInfo.hProcess, NULL);' – John