CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2013
    Posts
    34

    2 programs, 1 dll sharing variable.

    hello again, just wondering if you guys could help me again.

    my goal:
    have 1 program handle the UI
    have that program store variables to a DLL
    have the 2nd program grab the stored variables to reform some number crunching, without interfering with the UI program, and once done, have it drop the answers back into that DLL, so that the UI can grab it when it's ready.

    I have made the 2 programs, + dll (sorry, there is a lot of code, so I won't post it, unless you have a theory, to wich I can post how I achieved, whatever it is your theory is about)

    What I've Achieved:

    the first program accesses the dll, and loads up a variable (and stays connected to the dll, so that the Dll instance doesn't reset)

    I've gotten the DLL to output the variable to make sure it's received it, and stored it to it's own global variable.

    the second program connects to the dll successfully, but when it tries to retrieve the data, it returns 0's (NULL's)

    My research:
    from what I've read, so long the dll is connected to a program, all additional programs will attach to the same instance.

    how do I make the global variable in the dll be accessible to both programs? (without resorting to saving it to the HDD Idealy)

    thank you!

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: 2 programs, 1 dll sharing variable.

    One way of doing this is with memory mapped files.
    See http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

    Another way is to use named data segments so that each process has access to the same named segment
    See http://msdn.microsoft.com/en-us/library/thfhx4st.aspx
    http://www.codeproject.com/Articles/...gment-in-a-DLL
    http://msdn.microsoft.com/en-us/library/sf9b18xk.aspx


    When I just want to share a few simple variables between processed, I tend to use named data segments.
    Last edited by 2kaud; March 17th, 2014 at 06:14 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: 2 programs, 1 dll sharing variable.

    Quote Originally Posted by peteandperry View Post
    store variables to a DLL
    Technically speaking, you cannot store variables to a DLL. A DLL is just a collection of program code that can be shared between applications.

    What you are looking for is globally allocated memory. Have a look here:
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Nobody cares how it works as long as it works

  4. #4
    Join Date
    Apr 2013
    Posts
    34

    Re: 2 programs, 1 dll sharing variable.

    2kaud & zerver.

    Thank you so much! look like any one of those will do the job perfectly!
    thanks

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: 2 programs, 1 dll sharing variable.

    Best regards,
    Igor

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: 2 programs, 1 dll sharing variable.

    @Codeplug:
    It's true, but it's also somewhat of a bogus argument. This will only protect you from unauthorised access from other windows users.
    Oops, did that guy forget to mention that you can apply the same type of security descriptors on the DLL as you can on the mapped file. If user-level restriction is needed, then both the shared section in a dll and the mapped file can be protected.

    But yes, shared memory has several issues, regardless of how you try to protect it.
    THe DLL shared section can be an issue on a terminal server with multiple users havign active desktop sessions on the same machine at the same time. But without care, even the mapped file will have issues with this.


    ANd with the right tools and capabilities, you can make any exe do just about anything you want. Give a capable user a proper debugger and a basic compiler, and they can do all sorts of nasty stuff if they get enough privileges on the machine.
    something to Always be aware of

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: 2 programs, 1 dll sharing variable.

    @Codeplug:
    It's true, but it's also somewhat of a bogus argument. This will only protect you from unauthorised access from other windows users.
    Oops, did that guy forget to mention that you can apply the same type of security descriptors on the DLL as you can on the mapped file. If user-level restriction is needed, then both the shared section in a dll and the mapped file can be protected.

    But yes, shared memory has several issues, regardless of how you try to protect it.
    THe DLL shared section can be an issue on a terminal server with multiple users havign active desktop sessions on the same machine at the same time. But without care, even the mapped file will have issues with this.


    ANd with the right tools and capabilities, you can make any exe do just about anything you want. Give a capable user a proper debugger and a basic compiler, and they can do all sorts of nasty stuff if they get enough privileges on the machine.
    something to Always be aware of

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