Tôi đã nhận xét trong hầu hết những gì tôi biết. Tôi khá tự tin vấn đề phát sinh tại AttachThreadInput. Nó chỉ được thiết kế để hoạt động dưới 32bit, tôi nghĩ vậy. Tin tôi đi, nếu tôi có thể tự mình giải quyết, tôi sẽ rất vui. Tôi đọc tài liệu đầy đủ về các sự kiện trong Windows (here) và tôi không còn gần gũi với giải pháp nữa. Nếu bạn có bất kỳ ý tưởng nào tôi rất thích nghe chúng.C - win32: AttachThreadInput & SetFocus, 64bit: không có đầu mối
#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);
}
Bạn có chắc chắn lệnh gọi 'CreateProcess()' thành công không? Tất cả mọi thứ dường như xoay quanh điều này và mã của bạn làm điều tương tự (ngoài lời gọi 'GetLastError()') theo một trong hai cách. – MatthewD
'GetLastError()' trả về gì sau khi bạn gọi 'GetWindowThreadProcessId()'? Xem nhận xét dưới cùng trên trang này: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522%28v=vs.85%29.aspx – MatthewD
Có, 'CreateProcess()' là thành công. Notepad mở hoàn hảo, và các hàm SendInput nếu tôi tự mang nó tới nền trước. 'GetLastError()' trả về null sau 'GetWindowThreadProcessId (piProcessInfo.hProcess, NULL); ' – John