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

Is GetCurrentThread really a constant?

263 views
Skip to first unread message

Richard Russell

unread,
Oct 27, 2008, 2:39:36 PM10/27/08
to
Are there any circumstances, with any version of Windows, in which
GetCurrentThread can return a value other than the constant -2? Apart
from inelegance, is there any risk in using (HANDLE) -2 instead?

Richard.
http://www.rtrussell.co.uk/
To reply by email change 'news' to my forename.

Pavel A.

unread,
Oct 27, 2008, 2:47:05 PM10/27/08
to

From wdm.h:

#define NtCurrentThread() ( (HANDLE)(LONG_PTR) -2 )

So perhaps you can use this macro instead of -2.

Regards,
--PA

Volodymyr M. Shcherbyna

unread,
Oct 28, 2008, 4:51:23 AM10/28/08
to
Using macro in code instead of calling function can theoretically bring
problems in future when pseudohandle will change from -2 to -whatever.

--
Volodymyr, blog: http://www.shcherbyna.com/
(This posting is provided "AS IS" with no warranties, and confers no
rights)
"Pavel A." <pav...@NOfastmailNO.fm> wrote in message
news:%234qRMSG...@TK2MSFTNGP02.phx.gbl...

Pavel A.

unread,
Oct 28, 2008, 6:07:41 AM10/28/08
to
Volodymyr M. Shcherbyna wrote:
> Using macro in code instead of calling function can theoretically bring
> problems in future when pseudohandle will change from -2 to -whatever.
>

This is why linux kernel folks hate macros and typedefs...
Indeed, why someone needs another name for -2 ?

--PA

Volodymyr M. Shcherbyna

unread,
Oct 28, 2008, 6:19:55 AM10/28/08
to
> This is why linux kernel folks hate macros and typedefs...
> Indeed, why someone needs another name for -2 ?
>
> --PA

I don't know, but why someone changed PID for System process in Windows XP
to 4 from 8 (in Windows 2k)?

--
Volodymyr, blog: http://www.shcherbyna.com/
(This posting is provided "AS IS" with no warranties, and confers no
rights)
"Pavel A." <pav...@NOfastmailNO.fm> wrote in message

news:uQphnUOO...@TK2MSFTNGP06.phx.gbl...

Pavel A.

unread,
Oct 28, 2008, 12:45:20 PM10/28/08
to
Volodymyr M. Shcherbyna wrote:
>> This is why linux kernel folks hate macros and typedefs...
>> Indeed, why someone needs another name for -2 ?
>>
>> --PA
>
> I don't know, but why someone changed PID for System process in Windows XP
> to 4 from 8 (in Windows 2k)?

This could be part of boot optimization <g>

Seriously, system processes can be detected by other sane means
(PsInitialSystemProcess, PsIsSystemThread)

--PA

Volodymyr M. Shcherbyna

unread,
Oct 28, 2008, 12:52:53 PM10/28/08
to
So why in the hell someone has to rely on (HANDLE)2 if there is public
method (GetCurrentThread) to use? :)

--
Volodymyr, blog: http://www.shcherbyna.com/
(This posting is provided "AS IS" with no warranties, and confers no
rights)
"Pavel A." <pav...@NOfastmailNO.fm> wrote in message

news:e78Y0yRO...@TK2MSFTNGP02.phx.gbl...

Pavel A.

unread,
Oct 28, 2008, 5:54:51 PM10/28/08
to
Volodymyr M. Shcherbyna wrote:
> So why in the hell someone has to rely on (HANDLE)2 if there is public
> method (GetCurrentThread) to use? :)
>

GetCurrentThread is Win32 API, you can't call it in a driver.
NtCurrentThread is a native API (obviously, not mentioned in MSDN).
So yes, -2 is better, to avoid undocumented stuff ;)

--PA

Volodymyr M. Shcherbyna

unread,
Oct 28, 2008, 6:04:24 PM10/28/08
to
IIRC there were no talks about calling this function in kernel mode, so I
assume we speak about user mode :)

--
Volodymyr, blog: http://www.shcherbyna.com/
(This posting is provided "AS IS" with no warranties, and confers no
rights)
"Pavel A." <pav...@NOfastmailNO.fm> wrote in message

news:OlN0xfUO...@TK2MSFTNGP03.phx.gbl...

Volodymyr M. Shcherbyna

unread,
Oct 28, 2008, 6:21:57 PM10/28/08
to
2 Pavel,

Even if we speak about kernel mode, I would prefer to use NtCurrentThread
instead of using -2 define in my code, as if things will change in future
you will have a hidden bug which could be hard to trace.

On the other hand, If your driver will be using NtCurrentThread you will
discover problem at compilation stage (linker will fail if NtCurrentThread
will be removed from list of exported functions by ntoskrnl) or at customer
side driver just would not load and there will be apropriate entry in event
manager ...

--
Volodymyr, blog: http://www.shcherbyna.com/
(This posting is provided "AS IS" with no warranties, and confers no
rights)

"Volodymyr M. Shcherbyna" <v_sch...@online.mvps.org> wrote in message
news:edBglkUO...@TK2MSFTNGP03.phx.gbl...

Richard Russell

unread,
Oct 28, 2008, 6:28:50 PM10/28/08
to
On Oct 28, 4:52 pm, "Volodymyr M. Shcherbyna" wrote:
> So why in the hell someone has to rely on (HANDLE)2 if there is public
> method (GetCurrentThread) to use? :)

I don't suppose anybody *has* to rely on it, although there are
obvious (if small) advantages such as slightly shorter code and
slightly faster execution speed.

I would turn the question around. If existing code uses (HANDLE)-2,
is there a good reason to go to the trouble of changing it? My
reading of the replies in this thread is probably not. The
"theoretical" possibility of the value changing in the future doesn't
worry me, because almost certainly other things would require changing
then too.

Pavel A.

unread,
Oct 28, 2008, 6:38:46 PM10/28/08
to
Volodymyr M. Shcherbyna wrote:
> 2 Pavel,
>
> Even if we speak about kernel mode, I would prefer to use NtCurrentThread
> instead of using -2 define in my code, as if things will change in future
> you will have a hidden bug which could be hard to trace.
>
> On the other hand, If your driver will be using NtCurrentThread you will
> discover problem at compilation stage (linker will fail if NtCurrentThread
> will be removed from list of exported functions by ntoskrnl) or at customer
> side driver just would not load and there will be apropriate entry in event
> manager ...
>

Volodymyr, you know what... Microsoft will always find
a fresh and creative way to get us, no matter how we prepare.
The recent Cygwin issue is a good example.

--PA

Tim Roberts

unread,
Oct 29, 2008, 1:47:38 AM10/29/08
to
Richard Russell <ne...@rtrussell.co.uk> wrote:
>
>Are there any circumstances, with any version of Windows, in which
>GetCurrentThread can return a value other than the constant -2? Apart
>from inelegance, is there any risk in using (HANDLE) -2 instead?

Do the 16-bit systems matter to you? On Windows 95/98/ME, GetCurrentThread
does not return -2.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Richard Russell

unread,
Oct 29, 2008, 6:07:31 AM10/29/08
to
On Oct 29, 5:47 am, Tim Roberts <t...@probo.com> wrote:
> Do the 16-bit systems matter to you?  On Windows 95/98/ME, GetCurrentThread
> does not return -2.

GetCurrentThread *does* return -2 on Windows 95 (OSR2). At least, it
does here on the system I've just run up to check it.

Hang on a mo' while I fire up my WinMe system (considerably less
accessible, since it's in the attic!).... yep that returns -2 too.

If there are circumstances in which you believe Win9x might return a
value other than -2 I'd be interested to know what they are. Such
systems *do* matter to me.

Pavel A.

unread,
Oct 29, 2008, 11:09:35 AM10/29/08
to
ZwCurrentProcess() and ZwCurrentThread() _are_ documented in MSDN,
with following description:

"The returned value is not a true handle, but it is a special value that
always represents the current process. "

However, these macros are declared not like normal constants
(in uppercase) but like functions; maybe this suggests that the actual
value could change in various Windows versions and should be obtained in
runtime.

--PA

Ben Voigt [C++ MVP]

unread,
Oct 29, 2008, 3:45:35 PM10/29/08
to

You want to explain how a macro obtains anything at runtime? It doesn't
even survive until compile-time as it is substituted by the preprocessor.

>
> --PA


Pavel A.

unread,
Oct 30, 2008, 5:46:49 AM10/30/08
to

No, what I wanted to say is that this macro probably was intended
to hide the fact that it always evaluates to constant.

OTOH, someone from MS wrote that once a public API has been defined
as macro or inline function, it will never change later,
because this would break binary compatibility.

--PA

Tim Roberts

unread,
Oct 31, 2008, 12:26:31 AM10/31/08
to
Richard Russell <ne...@rtrussell.co.uk> wrote:
>
>On Oct 29, 5:47 am, Tim Roberts <t...@probo.com> wrote:
>> Do the 16-bit systems matter to you?  On Windows 95/98/ME, GetCurrentThread
>> does not return -2.
>
>GetCurrentThread *does* return -2 on Windows 95 (OSR2). At least, it
>does here on the system I've just run up to check it.

Hmmm. Was it Win32s where it didn't? That, I suppose, no longer matters
to ANYONE.

David Connet

unread,
Oct 31, 2008, 10:13:54 AM10/31/08
to
Tim Roberts <ti...@probo.com> wrote in
news:e62lg45rcovdp924k...@4ax.com:

> Richard Russell <ne...@rtrussell.co.uk> wrote:
>>
>>On Oct 29, 5:47 am, Tim Roberts <t...@probo.com> wrote:
>>> Do the 16-bit systems matter to you?  On Windows 95/98/ME,
>>> GetCurrentThread does not return -2.
>>
>>GetCurrentThread *does* return -2 on Windows 95 (OSR2). At least, it
>>does here on the system I've just run up to check it.
>
> Hmmm. Was it Win32s where it didn't? That, I suppose, no longer
> matters to ANYONE.

Oh G*d - that word - and on Halloween to boot! I'll have nightmares for
weeks! Win32s, Comm, thunking, AAHHHHHH

Dave Connet

0 new messages