Thread: Cross-process data transfer

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Cross-process data transfer

    I'm trying to retrieve an LV_ITEM structure from a list view control of another process, but I am having trouble with all this virtual address space business. I understand that any pointers sent between processes are irrelevant, and I don't see how else this is possible.

    I have of course considered VirtualAllocEx() but it seems much too complex.

    Is there a simple way I can obtain this data?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Can you use a MailSlot or Pipe? I favour pipes myself.

    http://msdn.microsoft.com/library/de...unications.asp
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You could always inject a DLL into the target process. This is exactly what Jeff Richter does in "Advanced Windows" when manipulating a listview belonging to another application.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I've read a lot about that, Eibro, but at this stage it's a bit of an overkill. Maybe later on.

    I'll give it a shot Adrian.

    On another note, I have a tree view control that lists all the top level windows, (using EnumWindows()), and I wanted to store each corresponding HWND in the control's memory. Problem is, I'm already using the lParam member of the item for a bit mask.

    My idea is to use the lParam member as a pointer to an instance of a class (allocated with new), that would contain members for the bit mask, the HWND, and anything else I might need. Is this the most efficient method?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Is this the most efficient method?

    Works for me.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    The Data Exchange section of MSDN only seems to list methods involving a two way conversation between two processes. Piping, mail slotting, DDE, Clipboard, and atoms all require some form of cooperation. I'm looking to simply pass a memory location to the target process and then read, interprocessly, from that location. I think I'll give VirtualAllocEx() a try.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    YES!!!!!!!

    I did it! And this is amazing: it compiled and worked the first time round. No errors, nothing! Here's the code:

    Code:
    HWND hFind=NULL;
    HWND hChildFind=NULL;
    
    hFind=FindWindow("DC++",NULL);
    
    LPVOID ipcAddr1;
    LPVOID ipcAddr2;
    DWORD dwProcId;
    HANDLE hProc;
    LV_ITEM lvi;
    char *chText=new char[512];
    
    lvi.mask=LVIF_TEXT;
    lvi.iItem=0;
    lvi.iSubItem=0;
    
    hChildFind=FindWindowEx(hFind,NULL,"SysListView32",NULL);
    GetWindowThreadProcessId(hChildFind,&dwProcId);
    		
    hProc=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcId);
    
    ipcAddr1=VirtualAllocEx(hProc,NULL,sizeof(LV_ITEM),MEM_COMMIT,PAGE_READWRITE);
    ipcAddr2=VirtualAllocEx(hProc,NULL,512,MEM_COMMIT,PAGE_READWRITE);
    
    lvi.pszText=(char *)ipcAddr2;
    lvi.cchTextMax=511;
    		
    WriteProcessMemory(hProc,ipcAddr1,(LPVOID)&lvi,sizeof(lvi),NULL);
    
    SendMessage(hChildFind,LVM_GETITEM,NULL,(LPARAM)ipcAddr1);
    		
    ReadProcessMemory(hProc,ipcAddr2,(LPVOID)chText,511,NULL);
    
    MessageBox(NULL,chText,"Notify",MB_OK);
    
    delete blah blah
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I've confirmed that this method also works for changing aspects of the list view control.

    Here's another issue: previously, when retrieving the text of a different-process edit control, I did the following:

    Code:
    char *chText=new char[512];
    
    hEdit=FindChildWindow(...);
    
    SendMessage(hEdit,WM_GETTEXT,511,(LPARAM)chText);
    It worked, but according to everything I've just learnt, it shouldn't. chText points to a random memory location in the virtual memory space of the other process, so how could it work?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    When doing something like this I normally just send a message with the pointer to the data (which is in proc1) in LPARAM, and the process id (of proc1) in WPARAM. Proc2 then does a ReadProcessMemory call to get the data it wants into it's own buffers.

    May not be the most efficient way, but it works, and works quite well. You can transfer several megabytes this way very easily.

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Wouldn't that require me to have designed the other process, or to have injected a DLL?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Yes, to communicate between processes in the way I have describe you need to have written both processes. However, you can still use ReadProcessMemory and WriteProcessMemory of a process in the system that you do not own, you just can't use them on system processes and services. All other processes (eg. any application launched from the desktop, or at startup) can have it's entire memory read by using ReadProcessMemory without you having to allocate any memory there first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  2. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  3. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. speakers
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 03-31-2002, 08:57 AM