Noviz
Noviz

Reputation: 59

C# loading C++ DLL - Problem on Exit

Ok so I have a C# user interface which uses a C++ DLL. The DLL is actually an OpenGL/SDL game. Once the game has finished it goes back to the C# UI. This all works nicely and as far as I know, correctly.

The problem comes when I try to exit the actual program. The C# form closes, however an error follows shortly which is pretty undescriptive. I assume it is something to do with the DLL, perhaps it is still open? How does one make sure that the DLL has closed properly? Or how do you close it all together?

I'm opening the DLL as follows:

    [DllImport("AsteroidGame.dll")]
    public static extern int EntryPoint();

    private void rungame()
    {
            EntryPoint();
    }

Thanks in advance.

EDIT

The error simply says:

vshost32.exe has stopped working

Upvotes: 2

Views: 1683

Answers (2)

Eamonn McEvoy
Eamonn McEvoy

Reputation: 8986

You cant unload an external assembly from c#, you can only unload the App Domain that loaded it. You could create an App Domain (http://msdn.microsoft.com/en-us/library/system.appdomain.aspx) then load the c++ assembly from here. When you are finished with the game, unload the App Domain.

Upvotes: 1

Alois Kraus
Alois Kraus

Reputation: 13535

The dll is unloaded by Windows when the application exits. During this process the static variables of your dll will be destroyed. If your game did not properly end and some loop is still sending events to e.g. a static class which in turn routes them into your C# UI you can get this sort of error.

At first you should check if you did call all cleanup routines of your game engine before you exit the C# UI. If that does not help you need to debug further into unmanaged code.

Yorus, Alois Kraus

Upvotes: 5

Related Questions