Custom Control Loses Device Context

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using VS 2003 my project uses COM interop and the Tao Framework.

I have a custom control derived from UserControl. It implements many
functions similar to the Tao's SimpleOpenGLControl, for example I also have a
MakeCurrent() method. Inside of the overriden method for CreateParams() I
also set the CS_OWNDC bit so that my control has a private DC. During
instantiation of my control I make a call to User32.dll to GetDC(IntPtr
handle) passing this.Handle as the argument and hold that as a member
variable, m_hDC, of my class. Then using Tao I then get the Rendering
Context with a m_hRC = Wgl.wglCreateContext(m_hDC);

My application is to take data from a COM object's Connection Point.
Basically it looks like this ReceivedData(SAFEARRAY(BYTE)* buffer). Using VS
to create the event handler for me I get in C#

private void myObj_ReceivedData(ref Array buffer)


My custom control has a method called SetData(byte[] data) so inside of my
event handler the code looks like this

private void myObj_ReceivedData(ref Array buffer)
{
myGlCtrl.SetData((byte[])buffer);
}


Now here is my problem. When the event handler is triggered my control
throws out errors. When I step into SetData() I fall into a position where
I'm watching the m_hDC and it all of the sudden doesn't match the
User32.GetDC(this.Handle) function call. When this happens I can't
successfully MakeCurrent. When I make calls to Marshal.GetLastWin32Error()
I've had it return #6, "The handle is invalid." and also #170, "The requested
resource is in use." I'm not sure which handle the error is referring to but
I do know that my control's Handle is constant and did not change.

Shouldn't the device context remain the same value since it is a private DC?

I also know that my control does draw. Because as a test I was at first
using a timer on my application that randomly filled a byte[] at every 500ms.
Inside the timer1_Tick(...) event handler I made the
myGlCtrl.SetData(m_data) call and it drew fine. But when I switched my
application to the COM object as the data source things went bad.

Is there another step I need to perform when getting data from the COM object?

Any help will be greatly appreciated.
Thanks in advance,

Dennis
 
It appears that my COM object's connection Point is coming in on a seperate
thread other than my UI thread.

Since this is using OpenGL that is invalid for my rendering context. There
can be only one ACTIVE rendering context per thread.

I believe I've found the solution. I had to use modify my code slightly so
that my SetData routine did no GL commands and only manipulated the data.
Then I used Invoke to get the other thread to start my Rendering on the UI
thread.

I hope this may help someone else in the future. I now I've spent to much
time with it.


Dennis

Dennis said:
I'm using VS 2003 my project uses COM interop and the Tao Framework.

I have a custom control derived from UserControl. It implements many
functions similar to the Tao's SimpleOpenGLControl, for example I also have a
MakeCurrent() method. Inside of the overriden method for CreateParams() I
also set the CS_OWNDC bit so that my control has a private DC. During
instantiation of my control I make a call to User32.dll to GetDC(IntPtr
handle) passing this.Handle as the argument and hold that as a member
variable, m_hDC, of my class. Then using Tao I then get the Rendering
Context with a m_hRC = Wgl.wglCreateContext(m_hDC);

My application is to take data from a COM object's Connection Point.
Basically it looks like this ReceivedData(SAFEARRAY(BYTE)* buffer). Using VS
to create the event handler for me I get in C#

private void myObj_ReceivedData(ref Array buffer)


My custom control has a method called SetData(byte[] data) so inside of my
event handler the code looks like this

private void myObj_ReceivedData(ref Array buffer)
{
myGlCtrl.SetData((byte[])buffer);
}


Now here is my problem. When the event handler is triggered my control
throws out errors. When I step into SetData() I fall into a position where
I'm watching the m_hDC and it all of the sudden doesn't match the
User32.GetDC(this.Handle) function call. When this happens I can't
successfully MakeCurrent. When I make calls to Marshal.GetLastWin32Error()
I've had it return #6, "The handle is invalid." and also #170, "The requested
resource is in use." I'm not sure which handle the error is referring to but
I do know that my control's Handle is constant and did not change.

Shouldn't the device context remain the same value since it is a private DC?

I also know that my control does draw. Because as a test I was at first
using a timer on my application that randomly filled a byte[] at every 500ms.
Inside the timer1_Tick(...) event handler I made the
myGlCtrl.SetData(m_data) call and it drew fine. But when I switched my
application to the COM object as the data source things went bad.

Is there another step I need to perform when getting data from the COM object?

Any help will be greatly appreciated.
Thanks in advance,

Dennis
 
Back
Top