Hosting unmanaged Irrlicht into C# Winforms

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Dr_Asik
Posts: 18
Joined: Wed Jun 02, 2010 2:15 am

Hosting unmanaged Irrlicht into C# Winforms

Post by Dr_Asik »

I'm having trouble hosting unmanaged Irrlicht into a C# Winforms application. I've consulted the guide at http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=29571 , unfortunately it uses C++/CLI and my project has to be in C#. So basically here's my code on the C# side of things:

Code: Select all

        Thread gameLoopThread;

        public MainWindow() {
            InitializeComponent();
        }

        void MainWindow_Load(object sender, EventArgs e) {
            DllExport.init(pictureBox1.Handle, pictureBox1.Width, pictureBox1.Height);
            gameLoopThread = new Thread(new ThreadStart(GameLoop));
            gameLoopThread.Start();
        }

        void GameLoop() {
            CycleTimer.TriggerUpdate += CycleTimer_TriggerUpdate;
            CycleTimer.TriggerDraw += CycleTimer_TriggerDraw;
            CycleTimer.Start(30);
            while (true) {
                CycleTimer.Update();
            }
        }

        void CycleTimer_TriggerDraw(object sender, TimerEvent e) {
            DllExport.draw(e.SecondsElapsed);
        }

        void CycleTimer_TriggerUpdate(object sender, TimerEvent e) {
            DllExport.update(e.SecondsElapsed);
        }

        void MainWindow_FormClosing(object sender, FormClosingEventArgs e) {
            gameLoopThread.Abort();
        }
Where DllExport is a class that lists some methods the unmanaged C++ exposes:

Code: Select all

    static class DllExport {

        [DllImport("client.dll")]
        public static extern void init(IntPtr handle, int width, int height);

        [DllImport("client.dll")]
        public static extern void update(float secondsElapsed);

        [DllImport("client.dll")]
        public static extern void draw(float secondsElapsed);

    }
And CycleTimer is just the basic game loop that triggers update and draw events.

On the C++ side of things, this is what happens when init() is called:

Code: Select all

void EngineCore::init(HWND windowHandle, int width, int height)
{
	SIrrlichtCreationParameters params;
	params.DeviceType = EIDT_BEST;
	params.DriverType = EDT_OPENGL;
	params.WindowId = windowHandle;
	dimension2d<u32> windowSize(width, height);
	params.WindowSize = windowSize;

	device_ = createDeviceEx(params);
	driver_ = device_->getVideoDriver();
	sceneManager_ = device_->getSceneManager();
}
And this is the draw method:

Code: Select all

void EngineCore::draw()
{
	driver_->beginScene();
	sceneManager_->drawAll();
	driver_->endScene();
}
My problem is that on the call to drawAll() (or beginScene(), unclear), I get the following exception:

Code: Select all

An unhandled exception of type 'System.AccessViolationException' occurred in []

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Any help appreciated.
Dr_Asik
Posts: 18
Joined: Wed Jun 02, 2010 2:15 am

Post by Dr_Asik »

The weirdest thing about this is that it works fine on 2 laptops I've tested it on. It crashes on my main desktop. Will try updating to the latest Catalyst drivers and see if it fixes that.
Dr_Asik
Posts: 18
Joined: Wed Jun 02, 2010 2:15 am

Post by Dr_Asik »

Updating to the latest Catalyst did not fix it. However, switching from OpenGL to Direct3D9 did.

Weird. :?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, using those MS proprietary languages and libs, it's always a good idea to switch to D3D as well. Maybe you can find some old threads which will help you with these things, IIRC there have been similar attempts in the past. But I don't know at all about C# stuff.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

Dr_Asik,

If you need to use Irrlicht with C# you may take a look on my .net wrapper -- http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=38512 . Check examples how to use it.
Dr_Asik
Posts: 18
Joined: Wed Jun 02, 2010 2:15 am

Post by Dr_Asik »

So you're basically compiling Irrlicht with the /clr switch on + a few wrapper classes? Sorry I don't know much about C++/CLI, but I don't see how you can call in native C++ code without P/Invoke or COM interop.

In any case, in the case of my project, the game code will be entirely written in native C++. The C# code just provides the window. Yeah it makes no sense, I'm not behind this design choice. :wink:
Post Reply