Using a C# form on windows

I’m wondering if someone already tried to use a window handle coming from a C# UserControl in order to use OpenGLES in a C# application. I’m currently building a UserControl in C++/CLI, all my steps are successful (eglGetDisplay, eglInitialize, eglChooseConfig, eglCreateWindowSurface, eglCreateContext) until  eglMakeCurrent, where I get a BAD_ALLOC error code.

What could make eglMakeCurrent fail ?

Thanks in advance,



I have to reckon that we do not use C# and we really do not know why eglMakeCurrent is failing. Possibly the context is invalid. Are you talking about WinCE or our Windows PCEmulation libraries?





Can you please share the code that makes this happen ? Also the different threads if any.

I think you probably are not using the handles properly - Note that the device contexts are valid only within certain contexts in C#.


You can try the below approach:


- Do your paint handler like the below:


private void pictureBox_Paint(object sender, PaintEventArgs e)


IntPtr hdc;

hdc = e.Graphics.GetHdc();

if(egl is not initialised)


   //STEP1 - One time - Use the above HDC context for eglinitialisation


//STEP2 - Periodic draw to the window - use the above HDC again


//FINAL STEP - End the context - outside this, HDC is not valid so cannot be used






Let me know if you see issues with this approach.


Thanks for all the answers!
I already used C# handles in order to initialize DirectX rendering, and if I remember well, I used something like (hwnd) Control.Handle.IntPtr. I was not aware of valid or not device context.

I'll check this in a few days, back to work, and upload a csproj.

@carlos, I'm currently testing WindowsEmulation library.
earthquakeproof2009-02-07 11:07:04