C++: access variable using memory address

Discussion in 'Mac Programming' started by moonman239, Jan 24, 2015.

  1. moonman239, Jan 24, 2015
    Last edited: Jan 24, 2015

    moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    Suppose I want to build a C++ program that can manipulate another program's data. My program knows where this data is located and what kind of data it's supposed to be dealing with (int, string, etc.). How do I tell my program to access and change this data?

    ----------

    I think I found my answer:

    http://stackoverflow.com/questions/6016156/accessing-memory-of-other-applications-c

    I'll have to use an API that OS X Yosemite supports; given Apple's obsession with security, I doubt any such API exists.

    Edit: Another answerer says I could also try launching the program in question from within my program.
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    Assuming you control the code in both processes, the most straight-forward way is to use the shared memory system calls. You create a shared memory area and then map that area into the memory space of both processes. Then any modifications made by one process will show up in the other process. Synchronization is going to be up to you.

    The system calls are calls like shmat(), shmget(), shm_open(), etc. Depending on what kind of interface you're looking for to create the shared memory area. There are man pages which describe those functions and probably docs in the Apple Documentation set.

    If you aren't writing the code for both processes then you are going to need to go through the Darwin kernel to get access.
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    Thanks. Would you mind posting how to go through the kernel? It may be online, but it'd be nice for the searcher who comes across this thread to have all this information in one place.
     

Share This Page