locked
How to tell a GDI Handle from a Memory Handle? RRS feed

  • Question

  • I hope this question makes sense, but is there a way to tell whether a handle is a GDI Object Handle or a Global Memory Handle?  I have a Handle that I know is either a Global Memory Handle or a GDI Object Handle. Is there a way to tell which it is?


    Thursday, June 28, 2012 11:32 AM

Answers

  • Besides don't lose track of what it is in the first place?  No.

    More specifically, you might be able to tell based on the actual numeric value of the handle, but you would be relying on undocumented, implementation specific information which would very likely change in future versions of Windows.

    • Marked as answer by Jesse Jiang Monday, July 9, 2012 7:25 AM
    Thursday, June 28, 2012 1:56 PM
  • This test seems to work:

    if ((ULONG) handle & 0xF0000003)
      // is some other handle
    else
      // is memory handle

    • Marked as answer by Jesse Jiang Friday, July 13, 2012 2:41 AM
    Thursday, July 12, 2012 11:28 AM

All replies

  • I hope this question makes sense, but is there a way to tell whether a handle is a GDI Object Handle or a Global Memory Handle?  Thanks.

    I don't think so. As per MSDN, The handle can be ccess token, console input buffer, console screen buffer, event, file, file mapping, job, mailslot, mutex, pipe, printer, process, registry key, semaphore, serial communication device, socket, thread, or waitable timer. 

    The GetHandleInformation  inform about HANDLE_FLAG_INHERIT or HANDLE_FLAG_PROTECT_FROM_CLOSE only. 


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    • Proposed as answer by Jesse Jiang Friday, June 29, 2012 6:13 AM
    • Marked as answer by Jesse Jiang Monday, July 9, 2012 7:24 AM
    • Unmarked as answer by Jesse Jiang Monday, July 9, 2012 7:24 AM
    Thursday, June 28, 2012 11:47 AM
  • Sorry I wasn't clear: I have a Handle that I know is either a Global Memory Handle or a GDI Object Handle. Is there a way to tell which it is?
    Thursday, June 28, 2012 11:50 AM
  • Besides don't lose track of what it is in the first place?  No.

    More specifically, you might be able to tell based on the actual numeric value of the handle, but you would be relying on undocumented, implementation specific information which would very likely change in future versions of Windows.

    • Marked as answer by Jesse Jiang Monday, July 9, 2012 7:25 AM
    Thursday, June 28, 2012 1:56 PM
  • Thanks for the reply.  I first tried putting a GlobalSize(hMem) inside a __try/__except but it appears to be uncatchable if it is an invalid memory handle.

    Calling GetDIBits does seem to work, but I am concerned that it might actually be a valid memory handle even if GetDIBits returns non-zero.

    Thursday, June 28, 2012 2:38 PM
  • On 6/28/2012 10:38 AM, Neil.W wrote:

    Calling GetDIBits does seem to work, but I am concerned that it might actually be a valid memory handle even if GetDIBits returns non-zero.

    Well, that's a fatal flaw in your plan, isn't it? It is theoretically possible that at a given point in time there exist in the system two valid handles with the same numerical value - one HGDIOBJ and one HGLOBAL. Then, if all you know is this numerical value, there would be no way to tell which of the two you were supposed to use.

    So, you should really design your program in a way that it doesn't lose track of what kind of handle it's holding.


    Igor Tandetnik

    Thursday, June 28, 2012 7:25 PM
  • Hi,

    Would you mind letting me know the result of the suggestions? If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, July 4, 2012 7:35 AM
  • This test seems to work:

    if ((ULONG) handle & 0xF0000003)
      // is some other handle
    else
      // is memory handle

    • Marked as answer by Jesse Jiang Friday, July 13, 2012 2:41 AM
    Thursday, July 12, 2012 11:28 AM
  • How about GetObjectType? If it returns some OBJ_... constant it is a GDI Object. If it returns zero it is a Global Memory Object.
    Monday, July 23, 2012 9:26 PM