C ++のFindWindow()関数に問題があります。プログラムAとプログラムBの2つのプログラムを使用しています。どちらもネイティブコードのコンソールアプリケーションです。プログラムAは、intiとstringsを値で初期化します。プログラム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;
}
}