远程控制正在运行的应用程序 C++ WINAPI

发布于 2025-01-15 12:40:39 字数 2466 浏览 2 评论 0原文

我想创建一个可以控制指定运行进程的窗口的应用程序。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文