gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:10:32 32 4
gpt4 key购买 nike

我在使用 C++ 中的 FindWindow() 函数时遇到问题。我正在使用两个程序——程序 A 和程序 B。它们都是 native 代码的控制台应用程序。程序 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;
}

}

最佳答案

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

相反,我建议您允许直接输入进程 ID,例如从项目经理处获得后。如果你真的想通过窗口标题找到一个进程,你需要使用CreateWindow在进程A中。

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

关于C++——FindWindow win32 API 总是失败!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4484714/

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