Solved

access memory from another process c plus plus

Posted on 2009-05-05
5
836 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
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 8

Expert Comment

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

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Alternative to codeshare.io 23 1,146
Unable to install breakpoint in Eclipse: Absent Line Number Information 2 654
error when i compile 5 644
FindStr works only with 1 char searches 2 83
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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

831 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