Advice on creating a dll from C++ project and use it in C# winForms project

Started by
5 comments, last by AverageJoeSSU 10 years, 6 months ago

Hi All,

Straight to the point: I have an OpenGL C++ application which features things like a scene graph, scene nodes, some custom made GUI widgets that allow the user to place scene nodes and adjust their properties. However, using this custom GUI doesn't seem like the ideal way to develop an editor in the quickest way possible. It's rather cumbersome upon adding in new features. Ideally I'd like to use Windows Forms to quickly prototype new editor functions.

I'm currently wondering how I would best somehow wrap up this C++ code into perhaps a .dll, and then reference this in a C# project using Windows Forms, I am also wondering what requirements I need to keep in mind to make this migration as smooth as possible.

The main question I'm having most trouble with at the moment is how to embed my scene rendering into a windows form. Is such a thing possible, or would I need to create my window by calling some of the C++ code?

Any advice or helpful tips would be greatly appreciated!

Thanks

Advertisement
Assuming your rendering code is decoupled from the window, i.e. it simply takes a handle to the window, you can retrieve window handles from WinForms.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Thank you for your reply!

So I could essentially do something such as:

1) Create a new WinForms "main" form, retrieve its window handle

2) Using my C++ .dll (which I have yet to create, that is also new for me), set the "target window handle" to that which I just retrieved in my WinForms app, then create the OpenGL context in this window

3) Call the .dll's main render code and do my thing

And then to adjust my properties, I'd create things like buttons, textboxes, and call the .dll's scene editing functions?

This is just to get a bit of an overview before jumping into this.

Cheers,

Have a look at CsGL http://csgl.sourceforge.net/ I used this a couple years ago for our game, where the simulation codes is written in C++. The codes were use for both the game and on the tool side. On the tool side, it was exported to DLLs, and our C# tool would import it. The tool rendering were done on the tool side with CsGL.

I have made something similar for my game editor, here how it goes :

I created a custom panel class derived from the existing panel control, that allow me to override function like the OnPaint function. I pass the panel handle to a native function in my C DLL that do all the opengl setup stuff.

In my panel OnPaint function, I add a call to my native render function, which draw one frame each time it's called. Now, a simple call to myPanel.Invalidate() will automatically re-render the game scene on the panel.

If I need to have a steady 60fps directly in editor, I call myPanel.Invalidate() in the Application_Idle event of the mainform. I manage the 60fps limit directly into the native render function.

About the wrapping, in the C# side, I declare each native functions like this :

[source]

[DllImport(enginePath, CallingConvention = CallingConvention.Cdecl)]

public static extern void editor_draw_frame();

[/source]

don't forget to export properly your functions in your C++ DLL, otherwise .NET won't find them. (a mistake I made at first smile.png )

That's a good explanation, I have the information I sought smile.png
Thanks!

Why not WPF? One of my first blog posts in my retired blog was on this very subject. At the time I chose WPF because it was new.

Edit: http://deadbeefgames.com/blog/?p=86

There it is, code included.

------------------------------

redwoodpixel.com

This topic is closed to new replies.

Advertisement