C++ -- FindWindow win32 API 总是失败!

发布于 2024-10-08 12:42:19 字数 2534 浏览 0 评论 0原文

我在 C++ 中使用 FindWindow() 函数时遇到问题。我正在使用两个程序——程序 A 和程序 B。两者都是本机代码中的控制台应用程序。 程序 A 用值初始化 int i 和 string s。程序 B 使用程序 A 运行时显示的地址从程序 A 的内存中读取它们。 目前我只对读取“i”的值感兴趣。

我无法让 FindWindow() 工作,但我不知道为什么:/我没有做过太多 win32 api 编程,所以我在这个隔间中很新。

计划 A:

#include <Windows.h>
#include <string>
#include <iostream>

using namespace std;

int main() {
    SetConsoleTitle(L"PROGRAM_A");

    string s = "Kuken\0";
    int i = 12345;
    char choice;

    int* ptr_i = &i;
    string* ptr_s = &s;

    cout << "ADDRESSES: \n";
    cout << "Int i: " << ptr_i << "\n";
    cout << "String s: " << ptr_s << "\n\n";

    cout << "INITIAL VALUES: \n";
    cout << "Int i: " << i << "\n";
    cout << "String s: " << s << "\n\n";

    cout << "***Read/Modify this process memory with programB and view new values! \n\n";


    while (true) {
        cout << "Print values of i and s? y/n \n";
        cin >> choice;
        switch (choice) {
        case 'y': 
            cout << "i: " << *ptr_i << "\n";
            cout << "s: " << *ptr_s << "\n";
            break;
        default:
        break;
        }

    }

    return 0;
    }

计划 B:

#include <Windows.h>
#include <iostream>
#include <string>



int main() {
    HWND handle_temp;
    unsigned long pid;
    int buffer[1];
    std::wstring name = L"PROGRAM_A";

    int temp;
    int* ptr_i;
    std::string* ptr_s;

    std::cout << "Type the address of i in programA: ";
    std::cin >> std::hex >> temp;
    std::cout << "\n";
    ptr_i = (int*)temp;

    std::cout << "Type the address of s in programA: ";
    std::cin >> std::hex >> temp;
    std::cout << "\n\n";
    ptr_s = (std::string*)temp;

    handle_temp = FindWindow(NULL,name.c_str());
    if (!FindWindow(NULL,name.c_str())) {
        std::cout << "Error: Did not find window \n";
        std::cout << "src: " << ptr_i << "\n";
    }
    GetWindowThreadProcessId(handle_temp,&pid);
    HANDLE handle_prgmA = OpenProcess(PROCESS_VM_READ,0,pid);



    if (ReadProcessMemory(handle_prgmA,ptr_i,&buffer,4,NULL)) {
        std::cout << buffer[0];
    }
    else {
        std::cout << "Could not read memory";

}

CloseHandle(handle_prgmA);

while (true) {
    std::cin >> temp;
}

}

I am having problem with FindWindow() function in C++. I am using two programs -- Program A and program B. Both are console-applicaitons in native code.
Program A initialises int i and string s with values. Program B reads them from Program A's memory using addresses shown when program A runs.
Currently I am only interested in reading the value of 'i'.

I cannot get the FindWindow() to work though and I do not know why :/ I havn't done much win32 api programming so I' pretty new in this compartment.

PROGRAM A:

#include <Windows.h>
#include <string>
#include <iostream>

using namespace std;

int main() {
    SetConsoleTitle(L"PROGRAM_A");

    string s = "Kuken\0";
    int i = 12345;
    char choice;

    int* ptr_i = &i;
    string* ptr_s = &s;

    cout << "ADDRESSES: \n";
    cout << "Int i: " << ptr_i << "\n";
    cout << "String s: " << ptr_s << "\n\n";

    cout << "INITIAL VALUES: \n";
    cout << "Int i: " << i << "\n";
    cout << "String s: " << s << "\n\n";

    cout << "***Read/Modify this process memory with programB and view new values! \n\n";


    while (true) {
        cout << "Print values of i and s? y/n \n";
        cin >> choice;
        switch (choice) {
        case 'y': 
            cout << "i: " << *ptr_i << "\n";
            cout << "s: " << *ptr_s << "\n";
            break;
        default:
        break;
        }

    }

    return 0;
    }

PROGRAM B:

#include <Windows.h>
#include <iostream>
#include <string>



int main() {
    HWND handle_temp;
    unsigned long pid;
    int buffer[1];
    std::wstring name = L"PROGRAM_A";

    int temp;
    int* ptr_i;
    std::string* ptr_s;

    std::cout << "Type the address of i in programA: ";
    std::cin >> std::hex >> temp;
    std::cout << "\n";
    ptr_i = (int*)temp;

    std::cout << "Type the address of s in programA: ";
    std::cin >> std::hex >> temp;
    std::cout << "\n\n";
    ptr_s = (std::string*)temp;

    handle_temp = FindWindow(NULL,name.c_str());
    if (!FindWindow(NULL,name.c_str())) {
        std::cout << "Error: Did not find window \n";
        std::cout << "src: " << ptr_i << "\n";
    }
    GetWindowThreadProcessId(handle_temp,&pid);
    HANDLE handle_prgmA = OpenProcess(PROCESS_VM_READ,0,pid);



    if (ReadProcessMemory(handle_prgmA,ptr_i,&buffer,4,NULL)) {
        std::cout << buffer[0];
    }
    else {
        std::cout << "Could not read memory";

}

CloseHandle(handle_prgmA);

while (true) {
    std::cin >> temp;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

摇划花蜜的午后 2024-10-15 12:42:19

这根本无法按照您希望的方式工作。即使 FindWindow 调用成功:该窗口不是由您的控制台程序创建的。相反,Windows 有一个单独的服务器进程负责控制台窗口的创建,以便多个进程可以共享一个控制台窗口。

相反,我建议您允许直接输入进程 ID,例如从程序管理器获取进程 ID 后。如果您确实想通过窗口标题查找进程,则需要使用 CreateWindow

编辑:您可以使用EnumProcesses 在所有进程列表中查找您的进程。

This cannot work at all the way you want it to work. Even if the FindWindow call would succeed: the window is not created by your console program. Instead, Windows has a separate server process taking care of console window creation, so that multiple processes can share a single console window.

Instead, I recommend that you allow direct entering of the process ID, e.g. after obtaining it from the program manager. If you really want to find a process by window title, you need to use CreateWindow in process A.

Edit: You can use EnumProcesses to find your process in the list of all processes.

2024-10-15 12:42:19

IIRC,FindWindow 需要消息循环才能运行。
由于控制台窗口没有您控制的消息循环,因此它不起作用。

IIRC, FindWindow requires a message loop to function.
Since console windows don't have a message loop that you control, it does not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文