2012-06-06 21 views
7

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); 
} 
+0

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

+0

'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

+0

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

Trả lời

6

GetWindowThreadProcessId nhận làm đầu vào xử lý cửa sổ. Bạn đang vượt qua một xử lý quá trình đó là một nhịp hoàn toàn khác nhau. Đương nhiên điều này dẫn đến thất bại. Các cuộc gọi tiếp theo tới SetFocus, SetForegroundWindow, PostMessage và SetWindowPos cũng phạm sai lầm tương tự.

Sử dụng EnumWindows hoặc FindWindow để giữ tay cầm cửa sổ Notepad.

AttachThreadInput hoạt động với ID luồng. Bạn đã cố gắng để vượt qua một xử lý cho các chức năng và trong một xử lý 64 bit xử lý là 64 bit rộng và thread ID vẫn còn 32 bit rộng. Tất nhiên, AttachThreadInput hoạt động hoàn toàn dưới 64 bit. Bài học để tìm hiểu ở đây là các phôi biểu thị các lỗi lập trình. Nếu bạn cần phải truyền các tham số cho kiểu thích hợp thì thường có nghĩa là bạn đang truyền sai thứ đó cho hàm. Tránh đúc.

+0

Cảm ơn bạn rất nhiều! Tôi đã có quyền này: 'HANDLE hTargetWindow = FindWindow (NULL," Untitled - Notepad ");' và bây giờ 'GetWindowThreadProcessId (hTargetWindow, NULL);' hoạt động rất đẹp. – John

+0

Loại đúng là HWND thay vì HANDLE. SetForegroundWindow có những hạn chế được mô tả chính xác trong tài liệu của nó. Tôi nghĩ tôi đã trả lời câu hỏi bạn hỏi. –

+0

Ngay sau khi tôi đăng tải, tôi đã kiểm tra tài liệu và bây giờ tôi đã làm việc. Cảm ơn bạn rất nhiều lần vì sự giúp đỡ của bạn, và sự điều chỉnh HANDLE/HWND! – John

Các vấn đề liên quan