Click here to Skip to main content
Click here to Skip to main content

CProcessData : A template class to ease up SendMessage calls across processes

By , 9 Jun 2005
 

Overview

CProcessData is a template class that makes it easy to use data allocated in a different process, and is useful when making inter-process SendMessage/PostMessage calls.

Example Scenario - 1

Imagine that you are sending a DTM_SETSYSTEMTIME message to a Date/Time picker control in a different process.

Without CProcessData

SYSTEMTIME systim;
//Populate systim

HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
LPVOID lpData = VirtualAllocEx(hProc, NULL, 
    sizeof SYSTEMTIME, MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(hProc, lpData, (LPCVOID)&systim, 
    sizeof SYSTEMTIME, NULL);            

DWORD dwResult = (DWORD)::SendMessage(hwnd, 
    DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&lpData);

if(dwResult == 0)
{
    DWORD err = GetLastError();
    //...
}

VirtualFreeEx(hProc, lpData, NULL, MEM_RELEASE);
CloseHandle(hProc);

Using CProcessData

SYSTEMTIME systim;
//Populate systim

CProcessData<SYSTEMTIME> data(pid);
data.WriteData(systim);

DWORD dwResult = (DWORD)::SendMessage(hwnd, 
    DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)data.GetData());
    
if(dwResult == 0)
{
    DWORD err = GetLastError();
    //...
}

Note only have you saved on lines of code, but you don't run the risk of forgetting to free the allocated memory or closing the process handle.

Example Scenario - 2

Imagine that you are retrieving toolbar info from a toolbar control in a different process.

Without CProcessData

HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
LPVOID lpData = VirtualAllocEx(hProc, NULL, 
    sizeof TBBUTTON, MEM_COMMIT, PAGE_READWRITE);      

::SendMessage(hwnd, TB_GETBUTTON, index, (LPARAM)lpData);

TBBUTTON tb;
ReadProcessMemory(hProc, lpData,(LPVOID)&tb, 
    sizeof TBBUTTON, NULL)

CUSTOMDATA cus;
ReadProcessMemory(hProc, (LPCVOID)tb.dwData, (LPVOID)&cus, 
    sizeof CUSTOMDATA, NULL)

VirtualFreeEx(hProc,lpData,NULL,MEM_RELEASE);
CloseHandle(hProc);

Using CProcessData

CProcessData<TBBUTTON> data(pid);

::SendMessage(hwnd, TB_GETBUTTON, index, 
    (LPARAM)data.GetData());

TBBUTTON tb;
data.ReadData(&tb);    

CUSTOMDATA cus;
data.ReadData<CUSTOMDATA>(&cus,(LPCVOID)tb.dwData);

Pretty neat, huh?

Class Reference

Constructor

CProcessData(DWORD dwProcessId = 0, DWORD dwDesiredAccess = PROCESS_ALL_ACCESS, DWORD flAllocationType = MEM_COMMIT, DWORD flProtect = PAGE_READWRITE)

If you pass in a dwProcessId of 0, the current process Id is used. For the other arguments, see MSDN documentation for OpenProcess and VirtualAllocEx.

WriteData

BOOL WriteData(const T& data)

WriteData copies data to memory in the foreign process

ReadData

BOOL ReadData(T* data)

ReadData reads back from the memory in the foreign process into data.

ReadData (template version)

template<typename TSUBTYPE> BOOL ReadData(TSUBTYPE* data, LPCVOID lpData)

Templated ReadData that's used to read a specific data type from a memory address located in the foreign process.

Full source listing

[Listing has been formatted to fit within 600 pixels. Actual source code (included as a download) is formatted for wider screens]

/*
    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
                 SendMessage/PostMessage calls.            
    Contact :    nish#voidnish.com
*/

//ProcessData.h

#pragma once

template<typename T> class CProcessData
{
public:    
    /*
        If you pass in a dwProcessId of 0, the current process Id is used.
        For the other arguments, see MSDN documentation for OpenProcess and
        VirtualAllocEx.
    */
    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());
        ASSERT(m_hProcess);
        if(m_hProcess)
        {
            m_lpData = VirtualAllocEx(m_hProcess, NULL, sizeof T, 
                flAllocationType, flProtect);
            ASSERT(m_lpData);
        }
    }

    ~CProcessData()
    {
        if(m_hProcess)
        {            
            if(m_lpData)
            {
                VirtualFreeEx(m_hProcess, m_lpData, NULL, MEM_RELEASE);
            }
            CloseHandle(m_hProcess);
        }
    }

    //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;
    }
private:
    T m_Data;
    HANDLE m_hProcess;
    LPVOID m_lpData;
};

History

  • June 16, 2005 : Small bug fix in destructor. (VirtualFreeEx and CloseHandle were in the wrong order)
  • June 10, 2005 : Article first published.
  • June 9, 2005 : Class written.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nish Sivakumar

United States United States
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalerrormemberswarup4-Jan-07 19:52 
data.ReadData(&tray,(LPCVOID)tb.dwData);
TRAYDATA' : illegal use of this type as an expression
 
why is tht, can i have the solution for tht,
QuestionScenario Example 1 problemsmemberGraemeS15-Aug-06 11:15 
This may be a little off topic, but I can't get your example scenario to work - i.e. I am unable to change the date in another process. I realise that it's not really what the article is about, but I'd appreciate any help on the matter. Thanks in advance.
GeneralAPI Hook OpenProcessmemberVitoto3-Jan-06 4:30 
Hi man, You code can help me ?
 
Openprocess Which within its parameters uses a PID, the this delivery the use of some of these APIs :
 
pid = FindWindow -> parameters
pid = GetHProcExe -> parameters
pid = GetWindowProcessID -> parameters
 
OpenProcess(reqdAccess, bInherit , pid )
 
When the application is cheat program, the will use in the PID the value of the PID my Game in memory.
 
From my application .Net I obtain the PID of the Game after launching it.
 
Is possible to interact with the GlobalHook to pass to him the PID to leak at the time of spying on all the programs that use the API Openprocess ?
 

Thank you.
 
Any ideas for make this code ?

GeneralNicely done!memberJörgen Sigvardsson28-Jul-05 11:57 
See subject. Smile | :)
 
--
An eye for an eye will only make the world blind.
GeneralRe: Nicely done!staffNishant Sivakumar14-Dec-05 11:08 
Thanks Jörgen Smile | :)
Generalcool articlemembermeir_rivkin4-Jul-05 5:42 
Suspicious | :suss: realy liked that article, 10x for posting it.
however, i couldn't figure out how to retrieve a running process handle in order to use it in SendMassage().
 
for example, i enumerate the processes running currently on my device till finding my target process.
i use OpenProcess() to get the handle but when i use it in SendMessage() it doesn't work.
 
any idea?Sniff | :^)
 
Programming can be a great fun.... (yeah right)
GeneralNice article!memberdmetzler3-Jul-05 17:17 

This was something I was looking to do while testing some code today.
 
I need to learn more about template classes, but your code is simple and effective.
 
Can you tell me how I would go about looking up string data in another application to find the text of a selected item in a listbox?
 
I would start by calling SendMessage(hWnd, LB_GETCURSEL, 0, 0); and using the result as the iSelItem. Then I would call SendMessage(hWnd, LB_GETTEXT, ...) to get the data. But how would I use your CProcessData class with a char[255] variable?
 
Thanks for the article! It has been really helpful Smile | :)
 
Don
 
Don Metzler
President & Chief Software Engineer
Barefoot Productions, Inc.
GeneralRe: Nice article!staffNishant Sivakumar3-Jul-05 18:30 
Hello Don,
 
You could try this code :-
 
int nSel = (int)::SendMessage(hWndLB,LB_GETCURSEL,0,0);
if(nSel != LB_ERR)
{	
	typedef TCHAR BUFFER[128];
	CProcessData<BUFFER> data(pid);
	::SendMessage(hWndLB,LB_GETTEXT,nSel,(LPARAM)data.GetData());
	TCHAR buff[129];
	data.ReadData((BUFFER*)buff);
	//buff now contains the selected item's text
}

GeneralRe: Nice article!memberdmetzler3-Jul-05 18:31 
Another question, how would I go about getting the text for a selected HTREEITEM in a tree control?
 
Using the TVM_ITEM for the message, and the TVITEM structure, it seems I would have to point the LPTSTR pszText field of the TVITEM field to memory allocated using the OpenProcess()/VirtualAllocEx() correct?
 
Thanks again,
 
Don
 
Don Metzler
President & Chief Software Engineer
Barefoot Productions, Inc.
GeneralVery Nice...memberPeregrine Falcon16-Jun-05 4:26 
I do a lot of system-level hacking with system-wide hooks and such, and this could be useful. I do have a couple suggestions, though:
  • Have you considered supporting older OS's (Win98, Win95)?
  • Have you considered using an assignment operator and a cast? This could let one use the class much more like a local object.

Generaldtor ordersussAnonymous15-Jun-05 4:06 
closes process handle, then uses just-closed handle in VirtualFree() ? Am I missing something ?
 
Jon
GeneralRe: dtor order and moresussAnonymous15-Jun-05 4:20 
If the member m_Data is used I'm missing it -- if it is used I'd move the class-high-use data ptr and process handle ahead of the contained object -- if m_Data is used, said object requires a default ctor.
 
Consider using bool / true / false instead BOOL / TRUE / FALSE, since BOOL is just an int and allows a boolean-ly-meaningless -3 (or at least confusing, tho strictly speaking it's not FALSE so it means TRUE, but folks who code TRUE == bVar get puzzled why their code fails).
 
Consider adding / using bool isOpenProcess() const { return NULL != m_hProcess ; } and comparable isValidPtr() -- then combining in a 3rd and use instead of scattering direct refs to members -- saves refactoring later.
 
Is there a reason that m_lpData isn't a T* ?
 
Nice class !
Jon
 

GeneralRe: dtor order and morestaffNishant Sivakumar15-Jun-05 18:55 
Hello Jon
 
The reason I used BOOL instead of bool was that ReadProcessMemory/WriteProcessMemory return BOOLs and I simply returned what they returned.
 
You are right - m_Data is not used anywhere - it was left in accidentally Blush | :O
 
Anonymous wrote:
Is there a reason that m_lpData isn't a T* ?
 
ReadProcessMemory, WriteProcessMemory, VirtualAllocEx all use an LPVOID, so I thought it best to keep it that way - and do a cast to T* in GetData (rather than having to cast in 3-4 other places).
GeneralRe: dtor orderstaffNishant Sivakumar15-Jun-05 18:40 
Anonymous wrote:
closes process handle, then uses just-closed handle in VirtualFree() ? Am I missing something ?

 
Ouch! That was a bad bug - fixing it now. Thank you.
GeneralNice work &amp;&amp; AnswerToQAboutExamplememberTom Archer12-Jun-05 1:05 
First, while I've seen the technique before in a couple of CP articles, it's definitely nice to have it in an easy-to-use class. Well done, Nish. Definitely better to make the client code as simple as possible.
 
Regarding a practical example that another reader asked for, I can think of one off the top of my head. Many times I'll run an app that displays data to which I need access - such as that displayed in a listview control. Unfortunately, there's no easy way to retrieve the data as obviously I can't just copy & paste the data. Nish's class will work perfectly should I run into that situation again.
 
Cheers,
Tom Archer - Archer Consulting Group

"So look up ahead at times to come, despair is not for us. We have a world and more to see, while this remains behind." - James N. Rowe
GeneralRe: Nice work &amp;&amp; AnswerToQAboutExamplestaffNishant Sivakumar15-Jun-05 19:00 
Thanks for the suggestion, Tom. Accessing List views, Tree views etc in a different process are all very good example situations for using this class.
GeneralOh my...memberYoSilver11-Jun-05 13:49 
... how neat!!! Excellent. Got my 5.
 
One always gets the deserved.

http://www.silveragesoftware.com/hffr.html
Update your source code with my tool HandyFile Find And Replace!
GeneralRe: Oh my...staffNishant Sivakumar15-Jun-05 19:00 
Thank you Smile | :)
GeneralGood WorkmemberMichael A. Barnhart11-Jun-05 11:54 
I really could have used this sometime back. My 5.
 
I do not mind getting old. It beats all the other options that I can think of.
GeneralRe: Good WorkstaffNishant Sivakumar15-Jun-05 19:00 
Thanks Michael.
GeneralLooks interesting, but...memberthe-unforgiven10-Jun-05 0:29 
Maybe it's my problem, but I'm missing a working sample...
GeneralRe: Looks interesting, but...staffNishant Sivakumar10-Jun-05 0:37 
the-unforgiven wrote:
Maybe it's my problem, but I'm missing a working sample...
 
I couldn't think of a simple example where an app sends a message to a control in a different app. I guess I am not imaginative enough Frown | :-(
GeneralRe: Looks interesting, but...memberthe-unforgiven10-Jun-05 0:46 
Nishant Sivakumar wrote:
I guess I am not imaginative enough
 
Well, me too...
GeneralRe: Looks interesting, but...memberBugByter11-Jun-05 1:43 
i think getting the elements of a listview is a nice example. should work like this, if i remember correctly.
GeneralRe: Looks interesting, but...memberGary R. Wheeler11-Jun-05 1:44 
This is useful any time you have to send a message to a control in another process. For example, almost all of the messages that operate on the contents of one of the common controls (list view, tree view, etc.) require an item structure of some kind.
 
You would apply this if you were trying to automate the other application, and you don't have control over the application itself. For example, I used something like this to automate Novell GroupWise, the e-mail client we use at work.
 

Software Zen: delete this;

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 10 Jun 2005
Article Copyright 2005 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid