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. Global Variable Question

Global Variable Question

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 3 Posters 0 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.
  • J Offline
    J Offline
    JSadleir
    wrote on last edited by
    #1

    Hi Coders I have a Visual C++ 2005 solution made up of two projects with lots of source fles (containing functions only) that produce two separate dlls. In one source file I calculate a value in a variable and I want that variable to have global scope across the two projects. Is that possible? The value in the variable is needed by all the other functions in the two projects to work properly. I have tried declaring the variable in header file and before the call to dllmain but neither approach works. I can't use a macro because the I have to recalculate the value each time the dlls are used. Any suggestions would be much appreciated. Regards Jeremy

    C 1 Reply Last reply
    0
    • J JSadleir

      Hi Coders I have a Visual C++ 2005 solution made up of two projects with lots of source fles (containing functions only) that produce two separate dlls. In one source file I calculate a value in a variable and I want that variable to have global scope across the two projects. Is that possible? The value in the variable is needed by all the other functions in the two projects to work properly. I have tried declaring the variable in header file and before the call to dllmain but neither approach works. I can't use a macro because the I have to recalculate the value each time the dlls are used. Any suggestions would be much appreciated. Regards Jeremy

      C Offline
      C Offline
      Chris Gao
      wrote on last edited by
      #2

      Hi Jeremy, I didn't catch your question very well. If u r talking about two dlls that are called by one executable module, which means that they are running in the same process, I would like you try to declare a variable in the exe file, pass a pointer to it to both dlls and reference this variable to share info. If they are running in different processes, I would suggest you use some ipc apporach, for example Create/OpenFileMapping thing. Regards, Chris

      J 1 Reply Last reply
      0
      • C Chris Gao

        Hi Jeremy, I didn't catch your question very well. If u r talking about two dlls that are called by one executable module, which means that they are running in the same process, I would like you try to declare a variable in the exe file, pass a pointer to it to both dlls and reference this variable to share info. If they are running in different processes, I would suggest you use some ipc apporach, for example Create/OpenFileMapping thing. Regards, Chris

        J Offline
        J Offline
        JSadleir
        wrote on last edited by
        #3

        Thanks Chris That's right I have two dlls that are called by one executable (Excel). I wanted to keep the variable out of Excel/VBA and just pass it between functions within both dlls. I'm very new to programming. Regards Jeremy

        Richard Andrew x64R C 2 Replies Last reply
        0
        • J JSadleir

          Thanks Chris That's right I have two dlls that are called by one executable (Excel). I wanted to keep the variable out of Excel/VBA and just pass it between functions within both dlls. I'm very new to programming. Regards Jeremy

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          I can see this solution: In the first DLL, define a function that returns a pointer to your variable. In the other DLL, dynamically load the first DLL with a call to LoadLibrary, and call the aforementioned function. Rich

          J 1 Reply Last reply
          0
          • J JSadleir

            Thanks Chris That's right I have two dlls that are called by one executable (Excel). I wanted to keep the variable out of Excel/VBA and just pass it between functions within both dlls. I'm very new to programming. Regards Jeremy

            C Offline
            C Offline
            Chris Gao
            wrote on last edited by
            #5

            Hi Jeremy, I am new to programming too. I think it is really cool to code something in MS Office. I mean that. Well, I think neither of my suggestions work in your case. Em... is your variable accessed very frequently? I mean, how about use registry table to story this variable? The following example really do very well in registry access stuff. http://www.codeproject.com/system/CRegistry.asp[^] Regards, Chris

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              I can see this solution: In the first DLL, define a function that returns a pointer to your variable. In the other DLL, dynamically load the first DLL with a call to LoadLibrary, and call the aforementioned function. Rich

              J Offline
              J Offline
              JSadleir
              wrote on last edited by
              #6

              Thanks Rich That sounds like the way to do it. I'll have a go at it. The variable may, however, revert to null once the function that performs the calculation goes out of scope. Regards Jeremy

              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