2

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);
}
2
  • 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). Commented Apr 21, 2011 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). Commented Apr 21, 2011 at 21:05

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is not exactly Win32 object. For example, i create a simple glut application from this tutorial (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.
GLUT is probably considered a Win32 object by WPF, since it is not WPF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.