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. run-time dll linking - getting a variable

run-time dll linking - getting a variable

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 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.
  • K Offline
    K Offline
    Kuniva
    wrote on last edited by
    #1

    Hi, my scenario is like this: I link a dll at run-time by calling LoadLibrary() and then I call GetProcAddress to get the location of a function within that DLL so i can call it. This works fine. But now I wanted to get the location of a variable, and address that, but it doesn't work. In my DLL I have a declaration as follows:

    extern "C" { char * szLogFile = "C:\\log.txt"; }

    So I try to address the variable from my program as follows:

    char * test = (char*)GetProcAddress(hinstDLL, "szLogFile");

    But when I then try to show the variable test using a messagebox I just get garbage so the pointer must be wrong. Any ideas whats wrong? Kuniva --------------------------------------------

    N 1 Reply Last reply
    0
    • K Kuniva

      Hi, my scenario is like this: I link a dll at run-time by calling LoadLibrary() and then I call GetProcAddress to get the location of a function within that DLL so i can call it. This works fine. But now I wanted to get the location of a variable, and address that, but it doesn't work. In my DLL I have a declaration as follows:

      extern "C" { char * szLogFile = "C:\\log.txt"; }

      So I try to address the variable from my program as follows:

      char * test = (char*)GetProcAddress(hinstDLL, "szLogFile");

      But when I then try to show the variable test using a messagebox I just get garbage so the pointer must be wrong. Any ideas whats wrong? Kuniva --------------------------------------------

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      I've seen this same problem. I can't recall why now. I suggest you add a set/get function to the DLL and use that to access the variable instead. BTW global variables are evil. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

      K 1 Reply Last reply
      0
      • N Neville Franks

        I've seen this same problem. I can't recall why now. I suggest you add a set/get function to the DLL and use that to access the variable instead. BTW global variables are evil. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

        K Offline
        K Offline
        Kuniva
        wrote on last edited by
        #3

        Heh, damn, I was kinda hoping for an explanation, I hate having to have to avoid the problem. But I guess I don't have much choice so thank you for your reply anyway. And as for the global variables are evil thing, so they keep telling me.. I don't see any reason why they should be evil in very small programs though :P And generally I believe it to be said by people who can't make sense of their own programs after a while ;) Also, no stack operations means more speed, weee. So, in short, global variables are evil, but fun! lol.. just messing. Kuniva --------------------------------------------

        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