calling an array from another program

hi, I want to put "hello world" in memory and I want to print it out in another console. how can I do this?




1
2
3
4
5
6
7
  in one console i want to put
 
 a$="hello world";

and in another console i want to print "a$" like 

cout <<a$<<endl;
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/193939/
closed account (E0p9LyTq)
How to implement interprocess communication (two apps sending data back and forth) depends on the operating system.

Interprocess Communications ( Windows )
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx

Interprocess communication on Mac OS X
https://lists.apple.com/archives/cocoa-dev/2003/Jan/msg01952.html
(I can't guarantee the validity of this information, I don't use a Mac.)

PDF Interprocess Communication - Advanced Linux Programming (PDF)
http://advancedlinuxprogramming.com/alp-folder/alp-ch05-ipc.pdf
(I don't use Linux)
I don't know which OS you are using but this is the solution for windows.

This programs has the memory we want to read.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <chrono>
#include <thread>
#include <fstream>


using namespace std;
int main() {

	std::ofstream memoryFile("C:\\Users\\Josh\\Desktop\\memoryProcess.txt", std::ofstream::out);
	if (!memoryFile) {
		cout << "Error\n"; return EXIT_FAILURE;
	}

	const size_t charSize = 15;

	const char* memory = new const char[charSize]{ "THIS IS A TEST" };

	//store the address and charsize in a file
	memoryFile << charSize << endl << (void*)memory << endl;


	//while (true) {
	//	std::cin.get();

	//	cout << "Value contains: " << memory << '\n';

	//	//cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	//}

	memoryFile.close();
	delete[] memory;

	cin.get();
	return 0;
}


This programs read that memory from the other program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <Windows.h>


using namespace std;

int main() {
	std::ifstream memoryAddress("C:\\Users\\Josh\\Desktop\\memoryProcess.txt", std::ifstream::in);
	if (!memoryAddress) {
		cerr << "Error, file not found.\n";
		return EXIT_FAILURE;
	}
	size_t address;
	size_t charCount;

	char* membuff;

	memoryAddress >> charCount;
	memoryAddress >> std::hex >> address;

	membuff = new char[charCount];

	HWND window = FindWindow(NULL, "c:\\users\\josh\\documents\\visual studio 2015\\Projects\\Read Adress\\Debug\\Read Adress.exe");
	DWORD procID = 0; 
	DWORD threadID = GetWindowThreadProcessId(window, &procID);
	HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, NULL, procID);

	LPCVOID pAddress = (const void*)address;

	if (ReadProcessMemory(process, pAddress, membuff, charCount, NULL)) {
		cout << "Success! The memory was processed correctly.\n\n";
		std::cin.get();
	}
	else {
		cout << "Memory wasn't processed correctly...\n";
		std::cin.get();
		return EXIT_FAILURE;
	}

	cout << "The memory address contains the value: " << membuff << '\n';

	memoryAddress.close();

	delete[] membuff;
	std::cin.get();
	return 0;
}


Note I'm using a file to keep track of which memory address one would like to access.
Also the file locations might need to be changed for you and also you need the window name to access the process handler that stores the variable.
I see only two ways.

Interprocess communication, which by looking at what rabster wrote is WAY easier in Unix.

Or you can write to a file. The name of the variable you use can be the filename. This idea is the most simple, but is really slow and I suggest you don't do it.
Topic archived. No new replies allowed.