Using a pointer in other program

Ok, let's assume that i've written this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
#include <sstream>
#include <string>

template <typename T>
std::string convertPointerToStringAddress(const T* obj)
{
  int address(reinterpret_cast<int>(obj));
  std::stringstream ss;
  ss << address;
  return ss.str();
}

void main()
{
	bool* bptr1 = new bool (true);
	std::cout<<convertPointerToStringAddress(bptr1);

	while(*bptr1)
	{

	}
}


And I want to close this program with other program by using the address of that pointer(bptr1) like this:
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

#include <iostream>
#include <sstream>
#include <string>

template <typename T>
T* convertAddressStringToPointer(const std::string& address)
{
  std::stringstream ss;
  ss << address;
  int tmp(0);
  if(!(ss >> tmp)) throw std::runtime_error("Failed - invalid address!");
  return reinterpret_cast<T*>(tmp);
}

void main()
{
	std::string s;
	std::cin>>s;

	bool* b = convertAddressStringToPointer<bool>(s);
	(*b) = !(*b);

	std::cin.get();
}


I will put the address that has shown in previous program as input. But i got the access violation error. Is there a way to tell the program to trust the other one? If not how can i do such a thing?
closed account (S6k9GNh0)
http://www.go4expert.com/forums/showthread.php?t=8461

You can't do stuff like you described because that's an obvious security problem. The OS will not allow it. The above example probably shows something like setting up an area of memory during runtime that's accessible from other locations through shared memory. There might also be some funky linkage hack you could find as well although I'm not sure.

Also my best friend, Boost Libraries, has a nifty little library just for this task: http://www.boost.org/doc/libs/1_47_0/doc/html/interprocess.html (or more specifically: http://www.boost.org/doc/libs/1_47_0/doc/html/interprocess/some_basic_explanations.html#interprocess.some_basic_explanations.sharing_information )

If you want more massive functionality, I would probably just use sockets and a basic protocol.
Last edited on
Thank's that sure helped a lot ;)
Topic archived. No new replies allowed.