Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Lodeman   Members   -  Reputation: 759

Like
0Likes
Like

Posted 15 October 2013 - 11:27 AM

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



Sponsor:

#2 Washu   Senior Moderators   -  Reputation: 5042

Like
1Likes
Like

Posted 15 October 2013 - 11:29 AM

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.
ScapeCode - Blog | SlimDX


#3 Lodeman   Members   -  Reputation: 759

Like
0Likes
Like

Posted 15 October 2013 - 11:50 AM

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,



#4 nhatkthanh   Members   -  Reputation: 334

Like
0Likes
Like

Posted 15 October 2013 - 12:03 PM

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.


Edited by nhatkthanh, 15 October 2013 - 12:04 PM.


#5 dlan   Members   -  Reputation: 389

Like
1Likes
Like

Posted 15 October 2013 - 03:21 PM

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 : 

[DllImport(enginePath, CallingConvention = CallingConvention.Cdecl)]
public static extern void editor_draw_frame();

 

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 )


Edited by dlan, 15 October 2013 - 03:24 PM.


#6 Lodeman   Members   -  Reputation: 759

Like
0Likes
Like

Posted 15 October 2013 - 04:47 PM

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



#7 AverageJoeSSU   Members   -  Reputation: 516

Like
0Likes
Like

Posted 16 October 2013 - 12:01 AM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS