4

We have to use 3rd party DLL in our development process, unfortunately we can't modify it or specify how library have to work.

Original library developers introduced state mechanic in their methods via static variables.

E.g.:

void foo()
{
 static int a = 1;
 if (a == 1)
 {
  /* some init logic */
  a = 2;
 } 
}

Sometime we need bring the library to its original state.

Is there any way to reset static variables to their original values without any system "hacks"?

Our current solution is FreeLibrary/LoadLibrary, but we want to avoid it.

svk
  • 53
  • 1
  • 7
  • 2
    This is possible. I've done this in my project. Here I've posted how you can start it : http://stackoverflow.com/questions/4308996/finding-the-address-range-of-the-data-segment/4316804#4316804 – Nawaz Oct 20 '11 at 17:34

4 Answers4

5

Your current solution is the cleanest solution.

The only alternative is to work out where the variable is stored in the DLL and modify it directly. But that's a gross hack that is incredibly brittle. For example, if the 3rd party DLL changes then your hack may stop working with unpredictable effects.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

Unfortunately, no, that is not possible without some serious hackery. FreeLibrary/LoadLibrary is your only option.

redec
  • 577
  • 2
  • 13
1

The scope of static local variables is the same as automatic local variable (apart from the fact their value will be present the next time function is called). So no, you cannot change them without a hack.

ata
  • 8,853
  • 8
  • 42
  • 68
0

Maybe loading it with RTLD_LOCAL another time and only when it is loaded switch the pointers to the new library pointers and unload the old library. Unfortunately I don't know what the Win32 equivalent of RTLD_LOCAL (if there is one).

selalerer
  • 3,766
  • 2
  • 23
  • 33