C - win32: AttachThreadInput & SetFocus, 64bit: no hint

I commented on most of what I know. I am sure that the problem occurs in AttachThreadInput. I think it is designed to function up to 32 bits. Believe me, if I could solve it myself, I would be happy. I read the full documentation for events on Windows ( here ), and I'm no closer to the solution. If you have any ideas, I would love to hear them.

#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 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 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); } 
+7
source share
1 answer

GetWindowThreadProcessId gets as an input window handle. You pass a handle to the process, which is a completely different rhythm. Naturally, this leads to failure. Subsequent calls to SetFocus, SetForegroundWindow, PostMessage, and SetWindowPos make the same error.

Use EnumWindows or FindWindow to get a Notepad window handle.

AttachThreadInput works with thread identifiers. You tried to pass a function descriptor, and in 64-bit handlers - 64 bits, and thread identifiers - 32 bits. Of course AttachThreadInput works fine under 64 bit. The lesson you will learn here is that casts indicate programming errors. If you need to enter parameters in the appropriate type, this usually means that you are passing the wrong function to the function. Avoid casting.

+6
source

All Articles