سؤال

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;
}

}
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top