Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

access memory from another process c plus plus

Posted on 2009-05-05
5
Medium Priority
?
857 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 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

972 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