CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2007
    Posts
    5

    data sharing between 2 c++ programs

    Hi,

    I would like to pass some values from a program to another program, in c++;

    for example if in the first program i have a variable called x which changes its value every time, and I want to take this value and pass it to the variable called y, which is in the second program. Is there an easy way to do that ? I tried with sockets, but i'm not that skilled and I want to try an easier method. With files maybe ? Some code would help me a lot.

    Note that x changes its value very quickly.

    Thank you

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: data sharing between 2 c++ programs

    Pipes may be what you're looking for.

    You can use shared memory, too. Actually, pipes are usually implemented using shared memory.

  3. #3
    Join Date
    Nov 2007
    Posts
    5

    Re: data sharing between 2 c++ programs

    Thank you for your advice, but can you help me with some piece of code ?
    I found this with google, but it copies an output from a shell, to a file:

    char line[100];
    in=popen("ls -l", "r");
    FILE * out = fopen("filename", "w");
    while(fgets(line, sizeof line, in))
    fprintf(out, "%s" line);
    fclose(out);

    What i want is to take the value of a variable from a first program and assign it to another variable from another program; both programs will run in the same time..

    Thanks

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: data sharing between 2 c++ programs

    If you're using popen() I presume you're on Linux. (At least, I don't think that's a cross-platform function....is it?)

    If so, you may find this useful:
    http://fscked.org/writings/SHM/shm.html

  5. #5
    Join Date
    Oct 2008
    Location
    Tel Aviv, Berlin, L.A.
    Posts
    23

    Re: data sharing between 2 c++ programs

    It's not (cross-platform).

    Smirk.
    ariell
    programming is understanding

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: data sharing between 2 c++ programs

    Unfortunately there is no 100% cross-platform IPC method I'm aware of except writing to files. However, if you're willing to wrap up the API differences behind an abstraction layer, every platform has the same conceptual methods-----shared memory, pipes, and sockets, for the most part. Shared memory is the fastest.

  7. #7
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: data sharing between 2 c++ programs

    Quote Originally Posted by ariell
    It's not (cross-platform).

    Smirk.
    http://msdn.microsoft.com/en-us/libr...4b(VS.80).aspx
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  8. #8
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: data sharing between 2 c++ programs

    Nice learning from expect.

    Inter Process Communication.

  9. #9
    Join Date
    Nov 2007
    Posts
    5

    Re: data sharing between 2 c++ programs

    I've managed to use IPC memory with functions like shmget, shmap which I found in a tutorial here : http://www.cs.cf.ac.uk/Dave/C/node27.html

    Thanks a lot for the idea!

  10. #10
    Join Date
    Oct 2008
    Location
    Tel Aviv, Berlin, L.A.
    Posts
    23

    Re: data sharing between 2 c++ programs

    Thanks for the link. I didn't know that MS spot.

    Best from the south.
    ariell
    programming is understanding

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured