Unload an external unmanaged DLL in NUnit Tests

I am running C # binding tests for an unmanaged dll. how can I write tests that I make sure that the .dll is unloaded and loaded again for the next test so that no state in the dll code goes to the next test?

dll methods are imported with the DllImport attribute.

DECISION:

my dll does some initialization in a static constructor, so I have to call this initialization code after it unloads. so the code is 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;
        }
    }
+3
source share
1 answer

I have not tried this, so I don’t know if it works, but I would try calling FreeLibrary (GetModuleHandle (DLLNAME)) in a loop until it completes.

+1

All Articles