Thread: Differenciating Handles

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Differenciating Handles

    How can I detirmen what kind of handle hHandle is? (Window handle, File handle, ect...) Is there some kind of API for this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    From the function which returned it in the first place?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    No, I know nothing about this handle only that it exists and that it is valid.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    There are no API's to reference the Windows internal representation of a handle. There is no need to reference the internal representation. You give Windows the handle and Windows locates the data associated with the handle. Handles are just a way to reference items that are managed by the OS.

    But anyway there are 13 different handle types defined in windows.h. So, you have a 1 out 13 chance of getting the right handle type.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    There is more than thirteen:

    HBITMAP
    HBRUSH
    HCOLORSPACE
    HDC
    HDESK
    HFONT
    HICON
    HKEY
    HMONITOR
    HTERMINAL
    HWINEVENTHOOK
    HMENU
    HMETAFILE
    HINSTANCE
    HPEN
    HTASK
    HWND

    These are just a few of the kind I would want, there are a lot more though.

    To check if it is a HWND, I try to use SendMessage() but that doesn't work.
    And SetPixel() doesn't seem to work for HDCs.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The only way out is to make your own function...
    Also the best way to check if it's a HWND is IsWindow().
    Also your list doesn't have HANDLE in it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I knew there was somthing like that, thats I thought there might be a IsBitmap() func.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Plus HANDLE may be different things...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    There is more than thirteen
    I stand corrected. I misread Dr. Newcomer's detailed description on the concept of handles in his Win32 Programming book. Per his description there are actually 26 handles defined in windows.h

  10. #10
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    You can differentiate between GDI object handles using the GetObjectType function.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Do you know the HMODULE of the owner process?

    It seems to me that HMODULE and HINSTANCE are the same things... sometimes I use GetModuleHandle() to get the HINSTANCE for some controls in my program...

    I'll try to complete this list
    HWND - IsWindow()
    HPEN,HDC,HMETAFILE,HBRUSH,HBITMAP,HFONT,HMETAFILE, HENHMETAFILE,HPALETTE,HRGN - GetObjectType()
    HCOLORSPACE - GetLogColorSpace()
    HPROFILE - GetColorProfileHeader()
    HMONITOR - GetMonitorInfo()
    HINSTANCE - OpenProcess() (GetLastError() should mention that the process doesn't exist...)
    HICON - GetIconInfo()
    HTASK - IsTask() ?? This seems to be a very old handle type... maybe from Win 3.x? IsTask() at least existed before Win95
    HMENU - GetMenuInfo()
    HDESK,HWINSTA - GetUserObjectInformation()
    HTERMINAL - MSDN search says nothing, it's defined, but I couldn't find any functions for it...
    HWINEVENTHOOK- IsWinEventHookInstalled()
    HACCEL - TranslateAccelerator()
    HGLRC - wglMakeCurrent()
    HKEY - RegQueryInfoKey()
    HRSRC - SizeOfResource()
    HKL - ActivateKeyboardLayout() (Couldn't find a better one...)
    HFILE - GetFileSize() (usually used just as HANDLE)
    HCURSOR - CopyCursor()

    And then you make a function that basically checks them all.
    Last edited by maxorator; 01-07-2007 at 11:38 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    That's a nice list, maxorator, like it, thanks.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You shouldn't ever have any ambiguity problems in the first place except for kernel object HANDLEs. Everything else has a distinct type, and you damn well shouldn't cast it in a way that this type information is lost.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by CornedBee
    and you damn well shouldn't cast it in a way that this type information is lost.
    Oh no, I would never loose my handle type inside of my own code. The thing is, this handle belongs to another process.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't understand this fascination with manipulating other processes. So many questions are about it. I've seen ReadProcessMemory() so often, but only on boards such as this one. People want to enumerate other apps' windows, find their windows, close the processes, start them, turn them inside out and upside down, inject code, extract data, do this and that ...

    ... and all the time I keep asking myself, why??? Isn't is much more satisfying to write a beautiful program of your own rather than messing with other programs?

    Sorry, I needed that rant. Don't take it too personal.


    How did you find the handle in the other process? Shouldn't the place where you found it give you a hint what it is?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with DB handles
    By pokks in forum C Programming
    Replies: 4
    Last Post: 01-04-2006, 12:06 PM
  2. organizing window handles
    By FOOTOO in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 11:53 PM
  3. PThread Handles Not Closing?
    By Geolingo in forum C++ Programming
    Replies: 7
    Last Post: 04-21-2005, 03:41 AM
  4. Handles to visible programs
    By Snip in forum Windows Programming
    Replies: 3
    Last Post: 03-26-2005, 08:52 PM
  5. one window, two handles
    By Devil Panther in forum Windows Programming
    Replies: 10
    Last Post: 01-15-2005, 09:45 AM