views:

295

answers:

2

I'm currently investigating a problem with a 3rd party component (DevExpress) in my application. My issue is quite similar to this one DevExpress KB article. I get the same exception with more less the same stacktrace. So I used .NET Reflector to find out, what may be null in this scenario, and the only object which is a candiate to be null is Graphics. This is created with Graphics.FromHwnd(IntPtr.Zero). Because I don't have a broad knowledge about GDI, I would like to know if somebody can tell me possible scenarios when this may return null...

I tried to reproduce it in a scenario where windows is out of GDI handle's, but then I'm getting a "out of handles" - exception at least once, which is not the case in the issue I'm investigating

tia, Martin

A: 

First of all, you are using the IntPtr structure and initializing it to zero. Then you are passing this handle to the Graphics class. Unless you have a window with a handle == 0, a null value is exactly what I would expect.

dboarman
A: 

From what I recall, an HWND of 0 (effectively IntPtr.Zero) is a handle to the current user session's desktop. It can be used to measure various metrics of the system and it should be a valid value for Graphics.FromHwnd.

However, GDI+ is so full of bugs and the .NET integration with it is so unstable it's very possible there's nothing wrong with your code or DevExpress other than it just uses GDI+.

Paul Alexander