I am doing tests on a c# binding for an unmanaged dll. how can i write the tests, that i make sure the .dll is unloaded and loaded again for the next test, so that no state in the dll code will pass on to the next test?
the dll methods are imported with the DllImport attribute.
SOLUTION:
my dll does some initalization in the static constructor, so i have to call this init code after unloading it again. so the code looks as follows:
private static int SDllHandle;
private static bool SInitializationRequired;
[SetUp]
public static void Init()
{
SDllHandle = LoadLibrary("my.dll");
if (SInitializationRequired)
{
//do some init code
SInitializationRequired = false;
}
}
[DllImport("kernel32")]
static extern int LoadLibrary(string lpLibFileName);
[DllImport("kernel32")]
static extern bool FreeLibrary(int hModule);
[TearDown]
public static void End()
{
//do some release code
while(FreeLibrary(SDllHandle))
{
SInitializationRequired = true;
}
}
I haven’t tried this so I don’t know if it works but I would try calling FreeLibrary(GetModuleHandle(DLLNAME)) in a loop until it fails.