远程控制正在运行的应用程序 C++ WINAPI
我想创建一个可以控制指定运行进程的窗口的应用程序。我使用 WINAPI 获取所需应用程序的主窗口句柄,并通过 WINAPI 的 SendMessage
函数向其发送消息。但应用程序似乎没有对消息做出反应。这是我的代码:
#include <Windows.h>
struct handle_res {
HWND hwnd;
DWORD pid;
};
HWND vscode_hwnd;
DWORD vscode_thread_id;
BOOL CALLBACK enum_wnd_proc(HWND hwnd, LPARAM lparam)
{
handle_res* p = reinterpret_cast<handle_res*>(lparam);
DWORD pid{};
if (GetWindowThreadProcessId(hwnd, &pid)) {
if (p->pid == pid) {
p->hwnd = hwnd;
SetLastError(-1);
return FALSE;
}
}
return TRUE;
}
LRESULT CALLBACK wnd_proc(
HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam
)
{
SendMessageA(vscode_hwnd, msg, wparam, lparam);
switch (msg) {
case WM_DESTROY: { PostQuitMessage(0); } break;
default: return DefWindowProcA(hwnd, msg, wparam, lparam); break;
}
return 0;
}
int WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
// obtain the window handle of the vscode
STARTUPINFOA supinfo{};
PROCESS_INFORMATION procinfo{};
CreateProcessA(
"C:\\vscode\\code.exe",
NULL, NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS,
NULL, NULL,
&supinfo, &procinfo
);
handle_res res{};
res.pid = procinfo.dwProcessId;
EnumWindows(enum_wnd_proc, reinterpret_cast<LPARAM>(& res));
vscode_hwnd = res.hwnd;
vscode_thread_id = res.pid;
// creating window for this application
// to get messages and redirect them to the
// obtained vscode window handle
WNDCLASSA wndcls{};
wndcls.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndcls.lpfnWndProc = wnd_proc;
wndcls.hInstance = hInstance;
wndcls.lpszClassName = "Window";
if (!RegisterClassA(&wndcls)) {
return -1;
}
HWND winh = CreateWindowA(
"Window", "MyWindow",
WS_OVERLAPPEDWINDOW,
0, 0, 800, 600,
NULL, NULL,
hInstance, NULL
);
if (!winh) {
return -1;
}
ShowWindow(winh, nCmdShow);
MSG msg;
while (GetMessageA(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
CloseHandle(procinfo.hProcess);
CloseHandle(procinfo.hThread);
return 0;
}
我应该怎么做才能使正在运行的应用程序对消息做出反应?
I want to create an application that can control the window of specified running process. I'm using WINAPI to obtain the main window handle of desired application and the sending the messages to it via SendMessage
function from WINAPI. But it seems like the applicetion does not react to the messages. There's my code:
#include <Windows.h>
struct handle_res {
HWND hwnd;
DWORD pid;
};
HWND vscode_hwnd;
DWORD vscode_thread_id;
BOOL CALLBACK enum_wnd_proc(HWND hwnd, LPARAM lparam)
{
handle_res* p = reinterpret_cast<handle_res*>(lparam);
DWORD pid{};
if (GetWindowThreadProcessId(hwnd, &pid)) {
if (p->pid == pid) {
p->hwnd = hwnd;
SetLastError(-1);
return FALSE;
}
}
return TRUE;
}
LRESULT CALLBACK wnd_proc(
HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam
)
{
SendMessageA(vscode_hwnd, msg, wparam, lparam);
switch (msg) {
case WM_DESTROY: { PostQuitMessage(0); } break;
default: return DefWindowProcA(hwnd, msg, wparam, lparam); break;
}
return 0;
}
int WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
// obtain the window handle of the vscode
STARTUPINFOA supinfo{};
PROCESS_INFORMATION procinfo{};
CreateProcessA(
"C:\\vscode\\code.exe",
NULL, NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS,
NULL, NULL,
&supinfo, &procinfo
);
handle_res res{};
res.pid = procinfo.dwProcessId;
EnumWindows(enum_wnd_proc, reinterpret_cast<LPARAM>(& res));
vscode_hwnd = res.hwnd;
vscode_thread_id = res.pid;
// creating window for this application
// to get messages and redirect them to the
// obtained vscode window handle
WNDCLASSA wndcls{};
wndcls.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndcls.lpfnWndProc = wnd_proc;
wndcls.hInstance = hInstance;
wndcls.lpszClassName = "Window";
if (!RegisterClassA(&wndcls)) {
return -1;
}
HWND winh = CreateWindowA(
"Window", "MyWindow",
WS_OVERLAPPEDWINDOW,
0, 0, 800, 600,
NULL, NULL,
hInstance, NULL
);
if (!winh) {
return -1;
}
ShowWindow(winh, nCmdShow);
MSG msg;
while (GetMessageA(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
CloseHandle(procinfo.hProcess);
CloseHandle(procinfo.hThread);
return 0;
}
What should I do to make the running application react to the messages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论