3

How to display a glut window inside Windows Form?

glutCreateWindow("Example") create another form,

glutCreateSubWindow(hwnd, 0, 0, 100, 100), where hwnd is handle to my main Window Form in C#, i get an AccessViolation Exception.

The Glut program is in a C++ DLL. My application is on C# WPF. I need to display glut view at my C# Form

C++ code:

extern "C"
    {
        __declspec(dllexport) int InitGlut(int hwnd, int top, int left, int width, int height)
        {
            glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
            glutInitWindowPosition(top,left);
            glutInitWindowSize(320,320);
            //glutCreateWindow("Example");
            glutCreateSubWindow(hwnd, top, left, width, height);
            glutDisplayFunc(renderScene);
            glutMainLoop();
            return 0;
        }
    }

C# code:

const string pathToDll = "../../../Release/MyDLL.dll";
[DllImport(pathToDll)]
public static extern int InitGlut(IntPtr hwnd, int top, int left, int width, int height);

private void Window_Loaded(object sender, RoutedEventArgs e)
{
     IntPtr hwnd = new WindowInteropHelper(Application.Current.MainWindow).Handle;
     InitGlut(hwnd, 0, 0, 100, 100);
}
EthanHunt
  • 473
  • 2
  • 9
  • 19
  • I'm not confident enough to post as an answer, but a couple of ideas... 1./ Try adding 'unsafe' to Window_Loaded and InitGlut in your C# code. 2./ Ensure that you're using 64-bit versions of all 3 elements (the GLUT library, your C++ code and your C# code). – John Wordsworth Apr 21 '11 at 20:24
  • You've got a signature mismatch in your code which will cause crashes on 64-bit operating systems. The C++ side is expecting hwnd to be an `int` (which is also wrong--they're pointer-sized), and C# is passing it in as an `IntPtr` (correct, but not what C++ is expecting). – ChrisV Apr 21 '11 at 21:05

1 Answers1

1

Looks like you're hosting a Win32 object in a WPF form. Yes, this requires workarounds.

Have you seen the WPF and Win32 Interoperation guide on MSDN?

http://msdn.microsoft.com/en-us/library/ms742522.aspx

You'll need to check out the HwndHost class, too:

http://msdn.microsoft.com/en-us/library/system.windows.interop.hwndhost.aspx

djdanlib
  • 21,449
  • 1
  • 20
  • 29
  • This is not exactly Win32 object. For example, i create a simple glut application from this tutorial (http://www.lighthouse3d.com/tutorials/glut-tutorial/initialization/). This is a Win32 console application, that create glut window with help of glutCreateWindow function. I change it to dll and call its functions from my WPF. – EthanHunt Apr 22 '11 at 10:19
  • GLUT is probably considered a Win32 object by WPF, since it is not WPF. – djdanlib Apr 25 '11 at 14:04