0

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

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
Martin Moser
  • 6,219
  • 1
  • 27
  • 41
  • What exactly are you trying to do? Draw to the desktop? Capture the desktop? What? – Marc Gravell Mar 10 '09 at 10:04
  • No, the call to Graphics.FromHwnd is buried deeply in the DevExpress code, and I'm trying to find out what I may do wrong so that this call returns null. Later that DevExpress-control depends on that object, and so the NullReferenceException is occuring. – Martin Moser Mar 10 '09 at 11:02

2 Answers2

0

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.

IAbstract
  • 19,551
  • 15
  • 98
  • 146
0

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
  • 31,970
  • 14
  • 96
  • 151