Author : Nishant Sivakumar
Date : June 9, 2005
Info : Template class that makes it easy to use data allocated
in a different process. Useful when making inter-process
Contact : email@example.com
template<typename T> class CProcessData
If you pass in a dwProcessId of 0, the current process Id is used.
For the other arguments, see MSDN documentation for OpenProcess and
CProcessData(DWORD dwProcessId = 0, DWORD dwDesiredAccess = PROCESS_ALL_ACCESS,
DWORD flAllocationType = MEM_COMMIT, DWORD flProtect = PAGE_READWRITE)
m_hProcess = OpenProcess(dwDesiredAccess, FALSE,
dwProcessId ? dwProcessId : GetCurrentProcessId());
m_lpData = VirtualAllocEx(m_hProcess, NULL, sizeof T,
VirtualFreeEx(m_hProcess, m_lpData, NULL, MEM_RELEASE);
//WriteData is used to copy data to memory in the foreign process
BOOL WriteData(const T& data)
return (m_hProcess && m_lpData) ? WriteProcessMemory(m_hProcess, m_lpData,
(LPCVOID)&data, sizeof T, NULL) : FALSE;
//ReadData reads back data from memory in the foreign process
BOOL ReadData(T* data)
return (m_hProcess && m_lpData) ? ReadProcessMemory(m_hProcess, m_lpData,
(LPVOID)data, sizeof T, NULL) : FALSE;
//Templated ReadData that's used to read a specific data type from
//a memory address located in the foreign process
template<typename TSUBTYPE> BOOL ReadData(TSUBTYPE* data, LPCVOID lpData)
return m_hProcess ? ReadProcessMemory(m_hProcess, lpData,
(LPVOID)data, sizeof TSUBTYPE, NULL) : FALSE;
//Gets the address of the allocated memory in the foreign process
const T* GetData()
return (m_hProcess && m_lpData) ? (T*)m_lpData : NULL;
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com
where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket
as well as a programming book – Extending MFC applications with the .NET Framework
Nish's latest book C++/CLI in Action
published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.