No announcement yet.

Handle values under Windows

  • Filter
  • Time
  • Show
Clear All
new posts

  • Handle values under Windows

    Does anyone know what range of values Windows chooses from when
    creating a handle value ?

    I ask, because while trying to discover why NT4 was randomly failing
    to render images in my PBDLL6 program, I found that LoadBitmap() was
    generating bitmap handles beyond the range of a LONG (2,835,677,702
    was one such). This was easily cured by using a DWORD to reference the
    loaded bitmap.

    By comparison, Win98 SE has never yet (well, in 6 weeks' use) created
    a bitmap handle that couldn't be referenced correctly by a LONG.

    It seems however that dialog handles *can* always be represented by a
    LONG, borne out for example by DDT's "DIALOG NEW ... TO hDlg&".

    Either there's an underlying rule, or you have to check the range of
    possible handle values for each type of object you reference. The Win32
    SDK doesn't have the answer (AFAICS) - it just says HBITMAP in this case,
    which leads to Windows header files, of which there are 315 in my VC++6
    \Include folder... (the pointed-to WINUSER.H does not it seems contain
    a definition of HBITMAP).

    Anyway for the general case, there must be an easier way - any thoughts ?



    Zippety Software, Home of the Lynx Project Explorer
    My e-mail

  • #2

    I experienced this problem with Windows 2000.

    Windows 95, 98 NT 3.5 and before use LONGS and the handle will
    "always" be a positive number.

    Windows 2000 (and likely later versions of NT) use DWORD.

    What this means is that when you use a LONG to hold a handle value
    , if the highest BIT is set your LONG will be a negative value.

    As long as your code assumes that a handle can be negative, then
    you should be OK. Where problems occur is when you test for a
    valid handle with code like :

    IF hWnd&>0 THEN 
    END IF
    ' or
    IF hFont&>0 THEN
    END IF
    Your code should assume the handle can be negative, so use :

    IF hWnd&<>0 THEN 
    END IF
    ' or
    IF hFont&<>0 THEN
    END IF
    I had code that worked OK with WIN95, 98 and it failed on 2000
    when creating Fonts. The reason was simply I was testing for
    greater than zero, rather than not-equal to.

    Chris Boss
    Computer Workshop
    Developer of "EZGUI"


    • #3
      Thanks Chris, that's exactly how it came about in my application.

      I guess that LONGs (rather than DWORDs) are used to represent handle values
      in the general case so that comparisons with %INVALID_HANDLE_VALUE (-1) can
      be performed (although as it happens this is not the case with LoadBitmap(),
      which returns %NULL on failure).

      However, I can't help feeling it would be simpler all round if handles were
      always DWORDs, and %INVALID_HANDLE_VALUE was defined as its DWORD equivalent.
      No big thing though.



      Zippety Software, Home of the Lynx Project Explorer
      My e-mail