DLL for rendering within MFC/C#

stoneCold.net

18-01-2006 00:36:58

Hi, I'm glad to be registered in the addons forum now as well as in the normal ogre forum :wink: .

Now to the reason why I'm here...
The last days I installed MS Vstudio8 Express and played a little bit with it and I really like C# with the .NET framework 2.0 (everyone who has seen it will agree :wink: toolstrip rocks 8) ).
I even like it that much that I'd consider to write my game editors with C# now instead of good old MFC.

My first approach is that I created a Win32 DLL which holds the Ogre rendering code, and with C# I try to access this DLL.
This is the way I did it before with MFC and it should? work with C# too?

This is the code I used to dllimport in C#:public class o4D_core
{
[DllImport("o4D_core.dll")]
public static extern void o4D_create(IntPtr hwnd);
[DllImport("o4D_core.dll")]
public static extern void o4D_render();
[DllImport("o4D_core.dll")]
public static extern void o4D_rotate();
};

...and MFC:#define DllImport __declspec(dllimport)

class DllImport o4D_core
{
public:
o4D_core(void);
~o4D_core(void);
// Accessors
static o4D_core* GetInstance();
void o4D_create(HWND hwnd);
void o4D_render();
void o4D_rotate();
};

As you see I need to give the DLL a HWND when using MFC, which is? called IntPtr in C#.

But when I doo4D_core.o4D_create(pictureBox1.Handle); //for example
I often! get memory errors and so on, but sometimes it works.

Furthermore I don't use Ogre Root to update the renderwindow, because therefore I have the function "o4D_render" in both cases (MFC and C#).
Which is declared in the DLL as follows:void o4D_core::o4D_render()
{
mWindow->update();
}


Everything works simply great in MFC but in C# it's more coincidence if the "o4D_create" function completes without errors and the "o4D_render" function never worked, not even by chance.

My first thought is that in C# I do DllImport the classes functions seperately which I do not in MFC, but I found no other solution to DllImport a whole class into C#.

[EDIT]: Here's the runtime error I get:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I would be very pleased if someone can give me some information or even a clear solution, many thanks in advance.

stoneCold.net :wink:

rastaman

18-01-2006 18:38:53

I was under the impression that PInvoke could only be use with a C like API, in other words calls to function not in c++ classes and maybe not even in a namespace. Not sure on that.

How do you instance the c++ class o4D_core? When you call o4D_create how do you specify what instance to use?

stoneCold.net

18-01-2006 19:08:12

How do you instance the c++ class o4D_core? When you call o4D_create how do you specify what instance to use?
In MFC I use a special function in the class o4D_core...o4D_core* o4D_core::GetInstance()
{
if(m_Inst)
return m_Inst;
else
return m_Inst = new o4D_core;
}


but in C# I have no idea how I have to handle classes correctly, so the above code was just a lucky approach to get it working.
I really wondered that it sometimes worked, because I already know that I have to create a own instance in C# too...o4D_core mCore = new o4D_core(); //for example
but this doesn't work with the above (and beyond) declaration of the class, I think I am missing just a attribute or something? (and a default constructor too, sure :wink: ), so can anyone help me with this two problems? And isn't there a way to DllImport the whole class instead of using single DllImports, I ask this because I never saw anything (for C#) but such seperate imports everywhere (google, other forums)public class o4D_core
{
[DllImport("o4D_core.dll")]
public static extern void o4D_create(IntPtr hwnd);
[DllImport("o4D_core.dll")]
public static extern void o4D_render();
[DllImport("o4D_core.dll")]
public static extern void o4D_rotate();
};


[edit]: here is the error I get when I try to do it with the "new o4D_core();" approach:o4D_core mCore = new o4D_core();
mCore.o4D_create(this.Handle);

error CS0176: Static member 'o4D_core.o4D_create(System.IntPtr)' cannot be accessed with an instance reference; qualify it with a type name instead

Many, many thanks in advance.
stoneCold.net

rastaman

18-01-2006 19:34:26

well with a google search: PInvoke c++ classes
I found this wich may explain the problem. read the 4 post by "Willy Denoyette".
http://www.dotnet247.com/247reference/msgs/3/18778.aspx