gpt4 book ai didi

c++ - 在 C++ 中调用 AttachThreadInput 时获取 ERROR_INVALID_PARAMETER

转载 作者:行者123 更新时间:2023-11-30 05:07:29 30 4
gpt4 key购买 nike

我在调用 AttachThreadInput() 时得到一个 ERROR_INVALID_PARAMETER 错误代码。这是代码块:

HWND hwnd = MYFINDWINDOW::FindWindowA(NULL,"GameWindowB");
if(hwnd==NULL)
{
printf("%s\n","Error: Please start the game! Cannot find window error...exiting");
exit(-1);
}

DWORD lasterror=GetLastError();
printf("Last Error code: %lu\n-------------\n",lasterror);
DWORD procid;
DWORD curthreadid=GetCurrentThreadId();
MYPROCESS::GetWindowThreadProcessId(hwnd,&procid);
printf("GameWindowB process id: %d\n",procid);
printf("Current Thread's process id: %lu\n",curthreadid);
BOOL att=ATTACH::AttachThreadInput(curthreadid,procid,TRUE);
lasterror=GetLastError();
if(!att)
{
printf("Error: Could not attach with game windowB ...exiting: %d\n",att);
printf("Last error reported: %lu\n",lasterror);
exit(-1);
}

然后...这是执行该代码块时的输出:

Last Error code: 0-------------GameWindowB process id: 17100Current thread's process id: 17644Error: Could not attach with game windowB...exiting: 0Last error reported: 87

我正在使用以下编译器版本:

Microsoft (R) C/C++ 优化编译器版本 16.00.30319.01

最佳答案

FindWindow() 成功 之后调用 GetLastError() 是没有意义的,该值未定义。仅当 Win32 函数失败时才调用 GetLastError(),并且如果该函数被记录为通过 GetLastError()< 报告错误.

但是,在任何情况下,您都会收到 ERROR_INVALID_PARAMETER,因为您将进程 ID 传递给 AttachThreadInput()需要线程 ID 的地方:

BOOL WINAPI AttachThreadInput(
_In_ DWORD idAttach,
_In_ DWORD idAttachTo,
_In_ BOOL fAttach
);

Parameters

idAttach [in]

The identifier of the thread to be attached to another thread. The thread to be attached cannot be a system thread.

idAttachTo [in]

The identifier of the thread to which idAttach will be attached. This thread cannot be a system thread.

A thread cannot attach to itself. Therefore, idAttachTo cannot equal idAttach.

fAttach [in]

If this parameter is TRUE, the two threads are attached. If the parameter is FALSE, the threads are detached.

GetWindowThreadProcessId()在其返回值而非输出参数中报告线程 ID

DWORD WINAPI GetWindowThreadProcessId(
_In_ HWND hWnd,
_Out_opt_ LPDWORD lpdwProcessId
);

Parameters

hWnd [in]

Type: HWND

A handle to the window.

lpdwProcessId [out, optional]

Type: LPDWORD

A pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not.

Return value

Type: DWORD

The return value is the identifier of the thread that created the window.

试试这个:

HWND hwnd = MYFINDWINDOW::FindWindowA(NULL,"GameWindowB");
if (!hwnd)
{
printf("%s\n","Error: Please start the game! Cannot find window error...exiting");
exit(-1);
}

DWORD curthreadid = GetCurrentThreadId();
DWORD gamethreadid = MYPROCESS::GetWindowThreadProcessId(hwnd, NULL);

printf("GameWindowB thread id: %lu\n", gamethreadid);
printf("Current thread id: %lu\n", curthreadid);

if (!ATTACH::AttachThreadInput(curthreadid, gamethreadid, TRUE))
{
DWORD lasterror = GetLastError();
printf("Error: Could not attach with game windowB ...exiting\n");
printf("Last error reported: %lu\n", lasterror);
exit(-1);
}

...

// don't forget this when you are done....
ATTACH::AttachThreadInput(curthreadid, gamethreadid, FALSE);

关于c++ - 在 C++ 中调用 AttachThreadInput 时获取 ERROR_INVALID_PARAMETER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47399429/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com