Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to share a pointer between an EXE and a DLL

How to share a pointer between an EXE and a DLL

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelptutorialworkspace
11 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Mircea NeacsuM Offline
    Mircea NeacsuM Offline
    Mircea Neacsu
    wrote on last edited by
    #1

    Apologies if the description is long; I just want to make the question clear(er). I have a WIN32 project in which there are some applications and some DLLs that may or may not use a framework. The framework itself is statically linked to apps and DLLs and it has an important data structure that should be unique. Currently, when a DLL is loaded it has no way of knowing if the process in which it was loaded uses the framework and if the data structure has been created or not. If DLL uses the framework and the EXE doesn't, the DLL can initialize the data structure and all is fine. If the EXE uses the framework and the DLL doesn't, the EXE will initialize the structure and all is fine again. The problem appears when both the DLL and the EXE use the framework and only the EXE should initialize the structure and the DLL should just somehow find the address and use the data structure. I know I could use a shared memory area but this is not inter-process communication so I'd rather use something simpler. I even thought of setting an environment variable with the address of the structure but somehow the solution seems cheesy. Do you have any suggestion? Is there any standard/better way of doing this?

    Mircea

    CPalliniC G L 3 Replies Last reply
    0
    • Mircea NeacsuM Mircea Neacsu

      Apologies if the description is long; I just want to make the question clear(er). I have a WIN32 project in which there are some applications and some DLLs that may or may not use a framework. The framework itself is statically linked to apps and DLLs and it has an important data structure that should be unique. Currently, when a DLL is loaded it has no way of knowing if the process in which it was loaded uses the framework and if the data structure has been created or not. If DLL uses the framework and the EXE doesn't, the DLL can initialize the data structure and all is fine. If the EXE uses the framework and the DLL doesn't, the EXE will initialize the structure and all is fine again. The problem appears when both the DLL and the EXE use the framework and only the EXE should initialize the structure and the DLL should just somehow find the address and use the data structure. I know I could use a shared memory area but this is not inter-process communication so I'd rather use something simpler. I even thought of setting an environment variable with the address of the structure but somehow the solution seems cheesy. Do you have any suggestion? Is there any standard/better way of doing this?

      Mircea

      CPalliniC Online
      CPalliniC Online
      CPallini
      wrote on last edited by
      #2

      Couldn't the executable simply call a function of the DLL, passing the address of the data?

      "In testa che avete, Signor di Ceprano?" -- Rigoletto

      In testa che avete, signor di Ceprano?

      Mircea NeacsuM 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        Apologies if the description is long; I just want to make the question clear(er). I have a WIN32 project in which there are some applications and some DLLs that may or may not use a framework. The framework itself is statically linked to apps and DLLs and it has an important data structure that should be unique. Currently, when a DLL is loaded it has no way of knowing if the process in which it was loaded uses the framework and if the data structure has been created or not. If DLL uses the framework and the EXE doesn't, the DLL can initialize the data structure and all is fine. If the EXE uses the framework and the DLL doesn't, the EXE will initialize the structure and all is fine again. The problem appears when both the DLL and the EXE use the framework and only the EXE should initialize the structure and the DLL should just somehow find the address and use the data structure. I know I could use a shared memory area but this is not inter-process communication so I'd rather use something simpler. I even thought of setting an environment variable with the address of the structure but somehow the solution seems cheesy. Do you have any suggestion? Is there any standard/better way of doing this?

        Mircea

        G Offline
        G Offline
        Graham Breach
        wrote on last edited by
        #3

        I'm certainly no expert at COM, but this sounds like the kind of situation where it would be a good fit.

        1 Reply Last reply
        0
        • CPalliniC CPallini

          Couldn't the executable simply call a function of the DLL, passing the address of the data?

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          Thanks for the suggestion but the DLL-EXE API is set in stone. With over 300 DLL modules changing the API is not easy/feasible. I have to find a different solution. I'll probably go with the environment variable; I'll just have to hold my nose while coding :-D

          Mircea

          V J 2 Replies Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            Thanks for the suggestion but the DLL-EXE API is set in stone. With over 300 DLL modules changing the API is not easy/feasible. I have to find a different solution. I'll probably go with the environment variable; I'll just have to hold my nose while coding :-D

            Mircea

            V Offline
            V Offline
            Victor Nijegorodov
            wrote on last edited by
            #5

            Rather than use the environment variable you could use the Registry.

            Mircea NeacsuM 1 Reply Last reply
            0
            • Mircea NeacsuM Mircea Neacsu

              Apologies if the description is long; I just want to make the question clear(er). I have a WIN32 project in which there are some applications and some DLLs that may or may not use a framework. The framework itself is statically linked to apps and DLLs and it has an important data structure that should be unique. Currently, when a DLL is loaded it has no way of knowing if the process in which it was loaded uses the framework and if the data structure has been created or not. If DLL uses the framework and the EXE doesn't, the DLL can initialize the data structure and all is fine. If the EXE uses the framework and the DLL doesn't, the EXE will initialize the structure and all is fine again. The problem appears when both the DLL and the EXE use the framework and only the EXE should initialize the structure and the DLL should just somehow find the address and use the data structure. I know I could use a shared memory area but this is not inter-process communication so I'd rather use something simpler. I even thought of setting an environment variable with the address of the structure but somehow the solution seems cheesy. Do you have any suggestion? Is there any standard/better way of doing this?

              Mircea

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I would have used a (third) dll for the (static) "data structure"; the equivalent of a "data repository".

              "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

              1 Reply Last reply
              0
              • V Victor Nijegorodov

                Rather than use the environment variable you could use the Registry.

                Mircea NeacsuM Offline
                Mircea NeacsuM Offline
                Mircea Neacsu
                wrote on last edited by
                #7

                Interesting idea! However I would have to take care of cleaning the registry when the app finishes. The environment variable just "evaporates" on exit. Do you see any technical advantage to using the registry?

                Mircea

                V 1 Reply Last reply
                0
                • Mircea NeacsuM Mircea Neacsu

                  Interesting idea! However I would have to take care of cleaning the registry when the app finishes. The environment variable just "evaporates" on exit. Do you see any technical advantage to using the registry?

                  Mircea

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #8

                  Delete a registry key or value is very simple. And (almost) no one else (except your app) would like to change it.

                  1 Reply Last reply
                  0
                  • Mircea NeacsuM Mircea Neacsu

                    Thanks for the suggestion but the DLL-EXE API is set in stone. With over 300 DLL modules changing the API is not easy/feasible. I have to find a different solution. I'll probably go with the environment variable; I'll just have to hold my nose while coding :-D

                    Mircea

                    J Offline
                    J Offline
                    JudyL_MD
                    wrote on last edited by
                    #9

                    Is the "framework" API set in stone? It sounds like it should be the one controlling the existence of this structure. You said it was statically linked to both the DLL and the EXE, hence two copies of the library, but you can force it to use a single shared memory location to hold this structure's pointer. Both the DLL and the EXE would ask the framework for a pointer and the framework would use some sort of mutual exclusion / atomic operation to synchronize its access to the shared memory location to either allocate or use the existing structure.

                    Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

                    Mircea NeacsuM 1 Reply Last reply
                    0
                    • J JudyL_MD

                      Is the "framework" API set in stone? It sounds like it should be the one controlling the existence of this structure. You said it was statically linked to both the DLL and the EXE, hence two copies of the library, but you can force it to use a single shared memory location to hold this structure's pointer. Both the DLL and the EXE would ask the framework for a pointer and the framework would use some sort of mutual exclusion / atomic operation to synchronize its access to the shared memory location to either allocate or use the existing structure.

                      Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

                      Mircea NeacsuM Offline
                      Mircea NeacsuM Offline
                      Mircea Neacsu
                      wrote on last edited by
                      #10

                      JudyL_MD wrote:

                      It sounds like it should be the one controlling the existence of this structure.

                      Absolutely right.

                      JudyL_MD wrote:

                      the framework would use some sort of mutual exclusion / atomic operation

                      Key words here are "some sort of". That was the purpose of my question to ask for recommendations for "some sort of" mechanism :)

                      Mircea

                      J 1 Reply Last reply
                      0
                      • Mircea NeacsuM Mircea Neacsu

                        JudyL_MD wrote:

                        It sounds like it should be the one controlling the existence of this structure.

                        Absolutely right.

                        JudyL_MD wrote:

                        the framework would use some sort of mutual exclusion / atomic operation

                        Key words here are "some sort of". That was the purpose of my question to ask for recommendations for "some sort of" mechanism :)

                        Mircea

                        J Offline
                        J Offline
                        JudyL_MD
                        wrote on last edited by
                        #11

                        The first thing that pops into my head is to force the EXE and DLL to make their initial framework calls from separate threads and have the framework use a named mutex. Not elegant spawning a thread just to make a single function call and immediately waiting for that thread to finish, but it would work.

                        Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups