Solved

access memory from another process c plus plus

Posted on 2009-05-05
5
841 Views
Last Modified: 2013-12-14
Hi chaps, can anyone explain to me how the following could be achieved?  

I create a console ap which reads data into memory, there is an overall pointer that accesses the collection.  The collection is arrays that are on the heap and the collection is just pointers to the arrays thats basically it.

What I want to know and if it can be done how to do it is this:

I want to read the data into memory at the start of the day and just leave it open until I close the application.  I want to run a new console application ie another separate process and access the data thats already in memory.  Can this be done? and How?  Cheers

PS Im in native C++ not managed.
0
Comment
Question by:TheMoog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Accepted Solution

by:
subn0wa earned 250 total points
ID: 24307564
Just allocate memory with VirtualAllocEx on desired address and from another process do call of ReadProcessMemory.

Example below.
First run should be without any arguments, then use process id as first argument and address as second one.



#include <windows.h>
 
int _tmain(int argc, _TCHAR* argv[])
{
	wchar_t buff[65535/2];
 
	if(argc == 3)
		{
		HANDLE hProcess = OpenProcess(PROCESS_VM_READ ,FALSE, _wtoi(argv[1]));
		if(hProcess)
			{
			SIZE_T read = 0;
			LPVOID pDesired = (LPVOID)_wtoi(argv[2]);
			if(ReadProcessMemory(hProcess,pDesired,&buff,65535/2,&read))
				{
				wprintf(L"Success, data(%d): %s\n",read,buff);
				}
			else
				wprintf(L"Fail, bad adress %d\n",pDesired);
			CloseHandle(hProcess);
			
			}
		else wprintf(L"Failed to open process %d\n",_wtoi(argv[1]));
		return 0;
		}
 
	wprintf(L"I'm memory holder,i will wait until somebody closes me\n");
	LPVOID pMem = VirtualAllocEx(GetCurrentProcess(),0,65535,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
	if(pMem)
		{
		wprintf(L"Success\n");
		wsprintf(buff,L"Hello world from process %d. User memory located at %d",GetCurrentProcessId(),pMem);
		SIZE_T written = 0;
		WriteProcessMemory(GetCurrentProcess(),pMem,buff,wcslen(buff)*sizeof(wchar_t),&written);
		wprintf(L"%s\n",buff);
		Sleep(INFINITE);
		}
	else
		wprintf(L"Fail to allocate memory");
 
	
	return 0;
}

Open in new window

0
 

Author Comment

by:TheMoog
ID: 24308401
subn0wa thank you so much this has saved me so much arsing about.  It works perfectly :)
0
 
LVL 2

Expert Comment

by:avivshabo
ID: 24517908
Can you help me, I cant compile:
--------------------Configuration: memory - Win32 Debug--------------------
Compiling...
memory.cpp
C:\Documents and Settings\XPL.XPL-7B13B0B6955\memory.cpp(9) : error C2664: '_wtoi' : cannot convert parameter 1 from 'char *' to 'const unsigned short *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\XPL.XPL-7B13B0B6955\memory.cpp(13) : error C2664: '_wtoi' : cannot convert parameter 1 from 'char *' to 'const unsigned short *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\XPL.XPL-7B13B0B6955\memory.cpp(16) : error C2065: 'wprintf' : undeclared identifier
C:\Documents and Settings\XPL.XPL-7B13B0B6955\memory.cpp(23) : error C2664: '_wtoi' : cannot convert parameter 1 from 'char *' to 'const unsigned short *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\XPL.XPL-7B13B0B6955\memory.cpp(32) : error C2664: 'wsprintfA' : cannot convert parameter 1 from 'unsigned short [32767]' to 'char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
0
 
LVL 1

Expert Comment

by:subn0wa
ID: 24520500
try to change "w" function to their ansi analogs, _wtoi => atoi, wprintf => printf, etc.
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 25238879
Try it...
project->property->configuration property->charecter set-> use  unicode charecter set.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question