0

Update static variable outside of the file without modifying the file in which the static variable is declared in C lang.

Proj1 creates dll. Proj1 has abc.h file and it is defined as below :

static BOOl stvar = False;//declared as global static variable

func1()
{
    stvar= TRUE;
}

func2()
{
    if(stvar == TRUE)
    {
        ....
    }
    else
    {
        func1();  //call to func1 sets STVAR = TRUE;
    }
}

Proj2 Creates exe. It has cprog1.c file. cprog1.c file is defined as follows:

cprogfunc1()
{
    func2(); //call to func2 sets STVAR = TRUE;
}

cprogfunc2()
{
    stvar = FALSE;
    func2();
}

We are setting stvar to false in cprogfunc2() to make it execute else block in func2() of abc.h file. But the value we set in cprogfunc2() under cprog1.c is not reflected in abc.h file. We are updating static variable outside its declaration because we cannot modify anything under proj1. So please suggest some ways to update static variable from cprog1.c file without modifying abc.h/Proj1. If that is not possible suggest any workaround. Thanks.

Solutions already tried :

  1. Making stvar non static - not possible since we can not modify abc.h file
  2. Using pointers - did not work
Toby
  • 9,696
  • 16
  • 68
  • 132
Rash
  • 1
  • Your question is very unclear, please don't use code-formatting for things that arne't code, and clearly indicate which code is in which file and how you are compiling – M.M Apr 14 '17 at 12:12
  • If you're asking whether two different .exe programs can share a static variable... not like this they can't – M.M Apr 14 '17 at 12:12

1 Answers1

0

By definition, stvar was made static in order to limit accessibility to it, meaning the ideal way to tinker with it from the outside is to create an API for it (or indeed make it global, not static). Since editing Proj1 is out of the question, we are left with a bad situation.

What you could do is to reset Proj1's state by freeing the dll and loading it again, as mention here.

Community
  • 1
  • 1
Itamar G
  • 125
  • 1
  • 4
  • Thank you for your response. I tried to get dll's handle, passed it to FreeLibrary, loaded the library and then updated static variable. But the change is still not retained. Am i doing it in the right way ? @Ekadanta – Rash Apr 17 '17 at 12:23
  • You will not be able to actually update the static variable. If the static is in the .h file, it means your own object file is getting a copy of it, and messing around with it will do nothing to affect the library's static variable state. The solution offered is to free the library and load it up again, assuming this resets the bool in question to the desired state (False). If you're sure this is what you did and it is not working - is it possible that the default state of stvar is actually True and some other function in the code sets it to false? Please share more on your attempt entailed. – Itamar G Apr 17 '17 at 15:32
  • As it turns out, the above is not a reliable solution - as releasing a shared object does not guarantee the OS will release it from memory. When reopening it, it may not be reinitialized. See [here](https://stackoverflow.com/questions/24467404/dlclose-doesnt-really-unload-shared-object-no-matter-how-many-times-it-is-call) – Itamar G Jul 04 '17 at 12:44