<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Visual C++ Tips &#187; GetCurrentProcess</title>
	<atom:link href="http://weseetips.com/tag/getcurrentprocess/feed/" rel="self" type="application/rss+xml" />
	<link>http://weseetips.com</link>
	<description>Gold mine of Visual C++ tricks!</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>GetCurrentThread() returns pseudo handle, not the real handle.</title>
		<link>http://weseetips.com/2008/03/26/getcurrentthread-returns-pseudo-handle-not-the-real-handle/</link>
		<comments>http://weseetips.com/2008/03/26/getcurrentthread-returns-pseudo-handle-not-the-real-handle/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 16:58:40 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[Debugging Tips]]></category>
		<category><![CDATA[DuplicateHandle]]></category>
		<category><![CDATA[GetCurrentProcess]]></category>
		<category><![CDATA[GetCurrentThread]]></category>
		<category><![CDATA[handle]]></category>
		<category><![CDATA[Pseudo handle]]></category>
		<category><![CDATA[threads]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[Visual C++]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=37</guid>
		<description><![CDATA[For getting our thread handle, we usually call GetCurrentThread() api. But actually it returns a pseudo handle. A pseudo handle, is a special handle which represents the handle of current thread which uses it. Please see the following e.g. for more clarify. 1) Thread A gets its threadID by calling GetCurrentThread and Passed to Thread [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://siteground205.com/~weseetip/wp-content/uploads/2008/03/icon_description.jpg" alt="Icon Description" /><br />
For getting our thread handle, we usually call GetCurrentThread() api. But actually it returns a pseudo handle. A pseudo handle, is a special handle which represents the handle of current thread which uses it. Please see the following e.g. for more clarify.</p>
<blockquote><p>1) Thread A gets its threadID by calling GetCurrentThread and Passed to Thread B.<br />
2) Thread B needs to terminate Thread A. So it calls TerminateThread( handle passed by Thread A ).<br />
3) But instead of terminating Thread A, Thread B will be terminated, because the handle passed by Thread A was pseudo and it will become Thread B&#8217;s handle when Thread B uses it.</p></blockquote>
<p>Debug one GetCurrentThread() call and GetCurrentProcess() call and watch its return values. You can see what they returns are follows,</p>
<ul>
<li>
<div>GetCurrentThread() &#8211; <span style="color:#ff0000;">0xfffffffe</span></div>
</li>
<li>
<div>GetCurrentProcess() &#8211; <span style="color:#ff0000;">0xffffffff</span></div>
</li>
</ul>
<p>Ofcourse pseudo&#8230; isn&#8217;t it ?</p>
<p><img src="http://siteground205.com/~weseetip/wp-content/uploads/2008/03/icon_howcanidoit.jpg" alt="Icon How Can I Do It?" /><br />
For getting a real handle which represents your thread, create a duplicate handle by calling DuplicateHandle(). Please see the code block below.</p>
<pre>HANDLE hRealHandle = 0;
DuplicateHandle( GetCurrentProcess(), // Source Process Handle.
                 GetCurrentThread(),  // Source Handle to dup.
                 GetCurrentProcess(), // Target Process Handle.
                 &amp;hRealHandle,        // Target Handle pointer.
                 0,                   // Options flag.
                 TRUE,                // Inheritable flag
                 DUPLICATE_SAME_ACCESS );// Options
// Now the hRealHandle contains a real handle for your thread.</pre>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/03/26/getcurrentthread-returns-pseudo-handle-not-the-real-handle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

