Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get handle type?

29 views
Skip to first unread message

Le Tuan

unread,
Jun 23, 2003, 4:28:07 AM6/23/03
to
There are many types of Handle. for example, we are:
 
  • Access token
  • Communications device
  • Console input
  • Console screen buffer
  • Event
  • File
  • File mapping
  • Job
  • Mailslot
  • Mutex
  • Named pipe
  • Process
  • Semaphore
  • Socket
  • Thread
 
when i have a HANDLE, how can i know its type?

please help me,

Tuan Le

Bob Moore

unread,
Jun 23, 2003, 8:14:01 AM6/23/03
to
On Mon, 23 Jun 2003 15:28:07 +0700, "Le Tuan" <lt...@tma.com.vn>
wrote:

>when i have a HANDLE, how can i know its type?

A handle is an opaque "cookie". I don't think the question is
meaningful : it doesn't have a "type" - it's only meaningful to the
API(s) that you're using it with.

lallous

unread,
Jun 24, 2003, 6:44:41 AM6/24/03
to
Hello Bob,

I guess Le asked a good question.
suppose I did this:

h1 = CreateEvent(...) , h2 = CreateFile(...) , h3 = FindFirstFile() , hN =
....(...)

Now I want to have a function like: handleType(HANDLE h)
so when I call it as:
handleType(h1) -> output is: 'h1' is a handle to an event object
handleType(h2) -> h2 is a handle to a file object
handleType(h3) -> h3 is a handle to findfirst opaque data....

I guess the type can be extracted from inside the NT Object manager ...don't
know yet how.

Regards,
Elias

"Bob Moore" <bo...@mvps.org> wrote in message
news:ekrdfv8hp7n7uhrg4...@4ax.com...

Andras Tantos [MSFT]

unread,
Jul 8, 2003, 2:14:06 PM7/8/03
to
> Hello Bob,
>
> I guess Le asked a good question.
> suppose I did this:
>
> h1 = CreateEvent(...) , h2 = CreateFile(...) , h3 = FindFirstFile() , hN =
> ....(...)
>
> Now I want to have a function like: handleType(HANDLE h)
> so when I call it as:
> handleType(h1) -> output is: 'h1' is a handle to an event object
> handleType(h2) -> h2 is a handle to a file object
> handleType(h3) -> h3 is a handle to findfirst opaque data....
>
> I guess the type can be extracted from inside the NT Object manager
..don't
> know yet how.

As far as I know, there's no direct way to do that. There's a WINAPI call
'GetHandleInformation' but that won't give you much information. For
file-handles, there's a 'GetFileType' API but that obviously works only for
file handles. One solution I think would be to create a function wich tries
a variaty of API calls and the one that returns something useful identifies
the handle type. Something like this:

#define TYPE_UNKNOWN 0
#define TYPE_FILE 1
#define TYPE_PROCESS 2
#define TYPE_THREAD 3

DWORD GetHandleType(HANDLE aHandle) {
DWORD RetVal;
RetVal = GetFileType(aHandle);
if (RetVal != FILE_TYPE_UNKNOWN) return TYPE_FILE;
RetVal = GetPorcessId(aHandle);
if (RetVal != 0) return TYPE_PROCESS;
RetVal = GetThreadId(aHandle);
if (RetVal != 0) return TYPE_THREAD;
....
....
return TYPE_UNKNOWN;
}

--
Andras Tantos, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.

0 new messages