<?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>Code Library &#187; message</title>
	<atom:link href="http://www.ucosoft.com/tag/message/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ucosoft.com</link>
	<description>Free Source Code and Program Tips</description>
	<lastBuildDate>Mon, 19 Jul 2010 04:51:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to catch the events when click on the app icon on taskbar</title>
		<link>http://www.ucosoft.com/events-when-click-ontaskbar.html</link>
		<comments>http://www.ucosoft.com/events-when-click-ontaskbar.html#comments</comments>
		<pubDate>Wed, 24 Sep 2008 07:55:05 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[message]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/2008/09/24/events-when-click-ontaskbar.html</guid>
		<description><![CDATA[How to catch the events when you click on the application's icon on the taskbar? It&#8217;s the WM_NC class messages. For example, you can handle the WM_NCLBUTTONDBLCLK message. And you can alse reponsible the&#160; WM_SYSCOMMAND messages.&#160; In particular, the special cases of SC_MINIMIZE and SC_MAXIMIZE are what are called from the system menu, which is]]></description>
			<content:encoded><![CDATA[<pre class="csharpcode">How to catch the events when you click on the application's icon on the taskbar?</pre>
<p><span id="more-110"></span></p>
<p>It&#8217;s the WM_NC class messages. For example, you can handle the WM_NCLBUTTONDBLCLK message.</p>
<p>And you can alse reponsible the&#160; WM_SYSCOMMAND messages.&#160; In particular, the special cases of SC_MINIMIZE and SC_MAXIMIZE are what are called from the system menu, which is what is available from the taskbar. </p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/message" title="message" rel="tag">message</a>, <a href="http://www.ucosoft.com/tag/mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/what-is-the-usage-of-wm_null.html" title="What is the usage of WM_NULL? (November 23, 2006)">What is the usage of WM_NULL?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/the-simplest-way-to-change-the-row-height-of-clistctrl.html" title="The Simplest way to change the row height of CListCtrl (January 14, 2008)">The Simplest way to change the row height of CListCtrl</a> (0)</li>
	<li><a href="http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html" title="Solution: Crash in Release build with self-defined message (November 28, 2006)">Solution: Crash in Release build with self-defined message</a> (0)</li>
	<li><a href="http://www.ucosoft.com/install-global-hook-without-dll.html" title="Install global hook without dll (January 3, 2007)">Install global hook without dll</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-use-an-derived-clistctrl-in-clistview.html" title="How to use an derived CListCtrl in CListView (October 22, 2007)">How to use an derived CListCtrl in CListView</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-set-the-backgroud-color-of-cpropertypage.html" title="How to set the backgroud color of CPropertyPage (October 18, 2007)">How to set the backgroud color of CPropertyPage</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-sendpost-message-to-cdocument.html" title="How to send/post message to CDocument? (October 24, 2007)">How to send/post message to CDocument?</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-select-the-item-of-listviewclistctrl.html" title="How to select the item of ListView(CListCtrl) (October 18, 2007)">How to select the item of ListView(CListCtrl)</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-adding-ctreectrl-in-clistctrl.html" title="How to Adding CTreeCtrl in CListCtrl (January 7, 2008)">How to Adding CTreeCtrl in CListCtrl</a> (0)</li>
	<li><a href="http://www.ucosoft.com/intermittent-work-in-ui-thread-2.html" title="Intermittent work in UI thread (October 16, 2007)">Intermittent work in UI thread</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/events-when-click-ontaskbar.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to send/post message to CDocument?</title>
		<link>http://www.ucosoft.com/how-to-sendpost-message-to-cdocument.html</link>
		<comments>http://www.ucosoft.com/how-to-sendpost-message-to-cdocument.html#comments</comments>
		<pubDate>Wed, 24 Oct 2007 05:46:52 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Document]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[PostMessage]]></category>
		<category><![CDATA[SendMessage]]></category>
		<category><![CDATA[Window]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/2007/10/24/how-to-sendpost-message-to-cdocument.html</guid>
		<description><![CDATA[Sometime you need to send/post message to CDoumnet, but CDocument is not a window, and it can&#8217;t receive message. How do that? There are 3 solutions: Solution 1: Send the message to the main window. Send/Post the message to the main frame or the view, and then call some functions of the document. But if]]></description>
			<content:encoded><![CDATA[<p>Sometime you need to send/post message to CDoumnet, but CDocument is not a window, and it can&#8217;t receive message. How do that?</p>
<p>There are 3 solutions:</p>
<p><strong>Solution 1: Send the message to the main window.</strong></p>
<p> Send/Post the message to the main frame or the view, and then call some functions of the document.</p>
<p>But if in a MDI app, it does not work, because the routing becomes too complex. Typically, you can send message to a view, but that has risks if there are multiple views on the same document and the one you post message to has been closed. </p>
<p><strong>Solution 2: Use WM_COMMAND message.</strong></p>
<p>A document can&#8217;t recerive a message, but it can recerives the commands through MFC&#8217;s command mechanism. Then you can use ::PostMessage to post WM_COMMAND message, and repose it in the document.</p>
<p>Solution 3:</p>
<p>Create an invisible top-level window in the CDocument, that represents its message sink. You send/post message to it. Thus will eliminate the complex route policy between the main frame and the views and you don&#8217;t worry about the view closing.</p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/document" title="Document" rel="tag">Document</a>, <a href="http://www.ucosoft.com/tag/how-to" title="How-to" rel="tag">How-to</a>, <a href="http://www.ucosoft.com/tag/message" title="message" rel="tag">message</a>, <a href="http://www.ucosoft.com/tag/postmessage" title="PostMessage" rel="tag">PostMessage</a>, <a href="http://www.ucosoft.com/tag/sendmessage" title="SendMessage" rel="tag">SendMessage</a>, <a href="http://www.ucosoft.com/tag/window" title="Window" rel="tag">Window</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/write-and-read-binary-data-in-variant.html" title="Write and read binary data in VARIANT (November 22, 2006)">Write and read binary data in VARIANT</a> (0)</li>
	<li><a href="http://www.ucosoft.com/what-is-the-usage-of-wm_null.html" title="What is the usage of WM_NULL? (November 23, 2006)">What is the usage of WM_NULL?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html" title="Solution: Crash in Release build with self-defined message (November 28, 2006)">Solution: Crash in Release build with self-defined message</a> (0)</li>
	<li><a href="http://www.ucosoft.com/set-the-head-backgroud-color-of-cpropertypage.html" title="Set the head backgroud color of CPropertyPage (October 18, 2007)">Set the head backgroud color of CPropertyPage</a> (0)</li>
	<li><a href="http://www.ucosoft.com/intermittent-work-in-ui-thread-2.html" title="Intermittent work in UI thread (October 16, 2007)">Intermittent work in UI thread</a> (0)</li>
	<li><a href="http://www.ucosoft.com/intermittent-work-in-ui-thread.html" title="Intermittent work in UI thread (October 16, 2007)">Intermittent work in UI thread</a> (0)</li>
	<li><a href="http://www.ucosoft.com/install-global-hook-without-dll.html" title="Install global hook without dll (January 3, 2007)">Install global hook without dll</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-translate-among-cstringstring-and-char-array.html" title="How to translate among CString,string and char array (June 22, 2007)">How to translate among CString,string and char array</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-subclass-ctreectrl-in-ctreeview.html" title="How to subclass CTreeCtrl in CTreeView? (October 25, 2007)">How to subclass CTreeCtrl in CTreeView?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-sort-items-of-clistctrl-or-clistview.html" title="How to sort items of CListCtrl or CListView? (October 22, 2007)">How to sort items of CListCtrl or CListView?</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/how-to-sendpost-message-to-cdocument.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Intermittent work in UI thread</title>
		<link>http://www.ucosoft.com/intermittent-work-in-ui-thread-2.html</link>
		<comments>http://www.ucosoft.com/intermittent-work-in-ui-thread-2.html#comments</comments>
		<pubDate>Tue, 16 Oct 2007 08:27:33 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[sdk]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/2007/10/16/intermittent-work-in-ui-thread-2.html</guid>
		<description><![CDATA[Pedro Ferreira I&#8217;m trying to create a UI thread to do constant background work, but I&#8217;m having some design problems. The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will be]]></description>
			<content:encoded><![CDATA[<ul><strong>Pedro Ferreira</strong></ul>
<p>I&#8217;m trying to create a UI thread to do constant background work, but I&#8217;m having some design problems. </p>
<p>The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will be controlled with thread messages (to start, pause, query&#8230;). </p>
<p>My problem is where should I put the processing work. I need it to be responsive between cycles, where the process waits for 1 minute, so I can stop it in that period. My only idea is using OnIdle to do the work.<br />
  But then, how should I make it real idle when waiting for 1 minute? </p>
<p>Could anyone give some hints on what is the way to go? </p>
<ul><strong>Scott McPhillips [VC++ MVP]</strong></ul>
<p>You can&#8217;t use Sleep and still be responsive to messages.  Instead of using Sleep you could use SetTimer in the main thread and post a message to the thread, or SetTimer to a hidden window within the thread, or you could use a loop within the thread and use MsgWaitForMultipleObjects within the loop. </p>
<p>MsgWaitForMultipleObjects has a timeout parameter that gives you the sleep period, but it also returns early if a message or event comes along.</p>
<ul><strong>Almond and Joseph M. Newcomer [MVP]</strong></ul>
<p>I would be strongly disinclined to use MsgWaitForMultipleObjects.  You don&#8217;t gain anything by using it over using WM_TIMER, and it requires that you have to write your own message pump, always a dangerous proposition.</p>
<ul><strong>BUT, SOME MORE DISCUSSION ABOUT THE UI THREAD AND WORKER THREAD&#8230;</strong></ul>
<ul><strong>Almond </strong></ul>
<p>Do not use UI threads for this kind of thing.</p>
<ul><strong>David</strong></ul>
<p>I would create a worker thread (not a UI thread) unless you need a UI thread for some specific purpose not apparent in this description.  The worker thread would do it&#8217;s work and then do a WaitForSingleObject() with a timeout of 1 minute (60000 ms), which causes WaitForSingleObject() to return when either the main thread sets an event for starting, pausing, querying, etc. or the 1 minute has elapsed.  If you need separate events each for starting, pausing, querying, etc. use WaitForMultipleObjects() instead of WaitForSingleObject().</p>
<ul><strong>Joseph M. Newcomer [MVP]</strong></ul>
<p>UI threads are actually ideal for this sort of thing.  Don&#8217;t be confused by the phrase &#8216;UI&#8217; meaning something with windows; think of it as &#8220;a thread with a built-in queuing mechanism&#8221;.  They&#8217;re really very good for things that require timed events as the OP described.</p>
<p>There are some advantages to UI threads; for example, a UI thread has a built-in queue.<br />
Also, for some kinds of interfacing require a message pump to deal with event handling.  I don&#8217;t do databases so I don&#8217;t know if databases have this requirement, but there are On-Anything handlers involved in the database interface, then a UI thread is required.</p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/message" title="message" rel="tag">message</a>, <a href="http://www.ucosoft.com/tag/sdk" title="sdk" rel="tag">sdk</a>, <a href="http://www.ucosoft.com/tag/thread" title="thread" rel="tag">thread</a>, <a href="http://www.ucosoft.com/tag/mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/what-is-the-usage-of-wm_null.html" title="What is the usage of WM_NULL? (November 23, 2006)">What is the usage of WM_NULL?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/the-simplest-way-to-change-the-row-height-of-clistctrl.html" title="The Simplest way to change the row height of CListCtrl (January 14, 2008)">The Simplest way to change the row height of CListCtrl</a> (0)</li>
	<li><a href="http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html" title="Solution: Crash in Release build with self-defined message (November 28, 2006)">Solution: Crash in Release build with self-defined message</a> (0)</li>
	<li><a href="http://www.ucosoft.com/install-global-hook-without-dll.html" title="Install global hook without dll (January 3, 2007)">Install global hook without dll</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-use-an-derived-clistctrl-in-clistview.html" title="How to use an derived CListCtrl in CListView (October 22, 2007)">How to use an derived CListCtrl in CListView</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-set-the-backgroud-color-of-cpropertypage.html" title="How to set the backgroud color of CPropertyPage (October 18, 2007)">How to set the backgroud color of CPropertyPage</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-sendpost-message-to-cdocument.html" title="How to send/post message to CDocument? (October 24, 2007)">How to send/post message to CDocument?</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-select-the-item-of-listviewclistctrl.html" title="How to select the item of ListView(CListCtrl) (October 18, 2007)">How to select the item of ListView(CListCtrl)</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-read-sectors-of-a-physical-drive-under-win32.html" title="How to read sectors of a physical drive under Win32? (January 15, 2007)">How to read sectors of a physical drive under Win32?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-get-a-fixed-code-for-serial-number2.html" title="How to get a fixed code for serial number(2) (January 10, 2007)">How to get a fixed code for serial number(2)</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/intermittent-work-in-ui-thread-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intermittent work in UI thread</title>
		<link>http://www.ucosoft.com/intermittent-work-in-ui-thread.html</link>
		<comments>http://www.ucosoft.com/intermittent-work-in-ui-thread.html#comments</comments>
		<pubDate>Tue, 16 Oct 2007 08:23:59 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[sdk]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/2007/10/16/intermittent-work-in-ui-thread.html</guid>
		<description><![CDATA[Pedro Ferreira said: I&#8217;m trying to create a UI thread to do constant background work, but I&#8217;m having some design problems. The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will]]></description>
			<content:encoded><![CDATA[<p><strong><u>Pedro Ferreira said:</u></strong></p>
<p>I&#8217;m trying to create a UI thread to do constant background work, but I&#8217;m having some design problems.
<p>The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will be controlled with thread messages (to start, pause, query&#8230;).
<p>My problem is where should I put the processing work. I need it to be <br />responsive between cycles, where the process waits for 1 minute, so I <br />can stop it in that period. My only idea is using OnIdle to do the work. <br />&nbsp; But then, how should I make it real idle when waiting for 1 minute?
<p>Could anyone give some hints on what is the way to go?
<p><strong><u>Scott McPhillips [VC++ MVP]</u></strong>
<p>You can&#8217;t use Sleep and still be responsive to messages.&nbsp; Instead of using Sleep you could use SetTimer in the main thread and post a message to the thread, or SetTimer to a hidden window within the thread, or you could use a loop within the thread and use MsgWaitForMultipleObjects within the loop.
<p>MsgWaitForMultipleObjects has a timeout parameter that gives you the sleep period, but it also returns early if a message or event comes along.
<p><strong><u>Almond and Joseph M. Newcomer [MVP]</u></strong>
<p>I would be strongly disinclined to use MsgWaitForMultipleObjects.&nbsp; You don&#8217;t gain anything by using it over using WM_TIMER, and it requires that you have to write your own message pump, always a dangerous proposition.
<p><strong><u>BUT, SOME MORE DISCUSSION ABOUT THE UI THREAD AND WORKER THREAD&#8230;</u></strong>
<p><strong><u>Almond </u></strong>
<p>Do not use UI threads for this kind of thing.
<p><strong><u>David</u></strong>
<p>I would create a worker thread (not a UI thread) unless you need a UI thread for some specific purpose not apparent in this description.&nbsp; The worker thread would do it&#8217;s work and then do a WaitForSingleObject() with a timeout of 1 minute (60000 ms), which causes WaitForSingleObject() to return when either the main thread sets an event for starting, pausing, querying, etc. or the 1 minute has elapsed.&nbsp; If you need separate events each for starting, pausing, querying, etc. use WaitForMultipleObjects() instead of WaitForSingleObject().
<p><strong><u>Joseph M. Newcomer [MVP]</u></strong>
<p>UI threads are actually ideal for this sort of thing.&nbsp; Don&#8217;t be confused by the phrase &#8216;UI&#8217; meaning something with windows; think of it as &#8220;a thread with a built-in queuing mechanism&#8221;.&nbsp; They&#8217;re really very good for things that require timed events as the OP described.
<p>There are some advantages to UI threads; for example, a UI thread has a built-in queue. <br />Also, for some kinds of interfacing require a message pump to deal with event handling.&nbsp; I don&#8217;t do databases so I don&#8217;t know if databases have this requirement, but there are On-Anything handlers involved in the database interface, then a UI thread is required. </p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/message" title="message" rel="tag">message</a>, <a href="http://www.ucosoft.com/tag/sdk" title="sdk" rel="tag">sdk</a>, <a href="http://www.ucosoft.com/tag/thread" title="thread" rel="tag">thread</a>, <a href="http://www.ucosoft.com/tag/mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/what-is-the-usage-of-wm_null.html" title="What is the usage of WM_NULL? (November 23, 2006)">What is the usage of WM_NULL?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/the-simplest-way-to-change-the-row-height-of-clistctrl.html" title="The Simplest way to change the row height of CListCtrl (January 14, 2008)">The Simplest way to change the row height of CListCtrl</a> (0)</li>
	<li><a href="http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html" title="Solution: Crash in Release build with self-defined message (November 28, 2006)">Solution: Crash in Release build with self-defined message</a> (0)</li>
	<li><a href="http://www.ucosoft.com/install-global-hook-without-dll.html" title="Install global hook without dll (January 3, 2007)">Install global hook without dll</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-use-an-derived-clistctrl-in-clistview.html" title="How to use an derived CListCtrl in CListView (October 22, 2007)">How to use an derived CListCtrl in CListView</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-set-the-backgroud-color-of-cpropertypage.html" title="How to set the backgroud color of CPropertyPage (October 18, 2007)">How to set the backgroud color of CPropertyPage</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-sendpost-message-to-cdocument.html" title="How to send/post message to CDocument? (October 24, 2007)">How to send/post message to CDocument?</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-select-the-item-of-listviewclistctrl.html" title="How to select the item of ListView(CListCtrl) (October 18, 2007)">How to select the item of ListView(CListCtrl)</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-read-sectors-of-a-physical-drive-under-win32.html" title="How to read sectors of a physical drive under Win32? (January 15, 2007)">How to read sectors of a physical drive under Win32?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-get-a-fixed-code-for-serial-number2.html" title="How to get a fixed code for serial number(2) (January 10, 2007)">How to get a fixed code for serial number(2)</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/intermittent-work-in-ui-thread.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install global hook without dll</title>
		<link>http://www.ucosoft.com/install-global-hook-without-dll.html</link>
		<comments>http://www.ucosoft.com/install-global-hook-without-dll.html#comments</comments>
		<pubDate>Wed, 03 Jan 2007 09:14:37 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[hook]]></category>
		<category><![CDATA[message]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/66.html</guid>
		<description><![CDATA[Chinese version author: peach@newsmth.net Userally, when you install a global hook, you need a dll. But now, you can install WH_KEYBOARD_LL or WH_MOUSE_LL global hook without dll&#8217;s help. The following code was tested under win2k3. And I it will also run on 2k/xp. #define _WIN32_WINNT 0400 #define STRICT #define WIN32_LEAN_AND_MEAN #include #include #include DWORD g_main_tid]]></description>
			<content:encoded><![CDATA[<p><P>Chinese version author: peach@newsmth.net</P><br />
<P>Userally, when you install a global hook, you need a dll. But now, you can install WH_KEYBOARD_LL or WH_MOUSE_LL global hook without dll&#8217;s help.</P><br />
<P>The following code was tested under win2k3. And I it will also run on 2k/xp.
<p>
<span id="more-66"></span><br />
<coolcode lang="cpp" linenum="no" download="src_hook_without_dll.c"><br />
#define _WIN32_WINNT    0400<br />
#define STRICT<br />
#define WIN32_LEAN_AND_MEAN</p>
<p>#include <stdio.h><br />
#include <stdlib.h><br />
#include <windows.h></p>
<p>DWORD   g_main_tid  = 0;<br />
HHOOK   g_kb_hook   = 0;</p>
<p>BOOL CALLBACK con_handler (DWORD)<br />
    {<br />
        PostThreadMessage (g_main_tid, WM_QUIT, 0, 0);<br />
        return TRUE;<br />
    };</p>
<p>LRESULT CALLBACK kb_proc (int code, WPARAM w, LPARAM l)</p>
<p>    {<br />
        PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l;<br />
        const char *info = NULL;<br />
        if (w == WM_KEYDOWN)<br />
            info = &#8220;key dn&#8221;;<br />
        else if (w == WM_KEYUP)<br />
            info = &#8220;key up&#8221;;<br />
        else if (w == WM_SYSKEYDOWN)<br />
            info = &#8220;sys key dn&#8221;;<br />
        else if (w == WM_SYSKEYUP)<br />
            info = &#8220;sys key up&#8221;;</p>
<p>        printf (&#8220;%s &#8211; vkCode [%04x], scanCode [%04x]\n&#8221;,<br />
            info, p->vkCode, p->scanCode);<br />
        //  always call next hook<br />
        return CallNextHookEx (g_kb_hook, code, w, l);</p>
<p>    };</p>
<p>int main (void)<br />
    {<br />
        g_main_tid = GetCurrentThreadId ();<br />
        SetConsoleCtrlHandler (&#038;con_handler, TRUE);</p>
<p>        g_kb_hook = SetWindowsHookEx (<br />
            WH_KEYBOARD_LL,<br />
            &#038;kb_proc,<br />
            GetModuleHandle (NULL), //ã€€It shouldn&#8217;t be NULL, or will fail<br />
            0);</p>
<p>        if (g_kb_hook == NULL)</p>
<p>        {<br />
            fprintf (stderr,<br />
                &#8220;SetWindowsHookEx failed with error %d\n&#8221;,<br />
                ::GetLastError ());<br />
            return 0;<br />
        };</p>
<p>        // The message loop is necesarry</p>
<p>        MSG msg;<br />
        while (GetMessage (&#038;msg, NULL, 0, 0))<br />
        {<br />
            TranslateMessage (&#038;msg);<br />
            DispatchMessage (&#038;msg);<br />
        };<br />
        UnhookWindowsHookEx (g_kb_hook);<br />
        return 0;<br />
    };<br />
</coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/dll" title="dll" rel="tag">dll</a>, <a href="http://www.ucosoft.com/tag/hook" title="hook" rel="tag">hook</a>, <a href="http://www.ucosoft.com/tag/message" title="message" rel="tag">message</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/what-is-the-usage-of-wm_null.html" title="What is the usage of WM_NULL? (November 23, 2006)">What is the usage of WM_NULL?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/ucospy.html" title="ucoSpy,a system enhancement tool like spy++ (November 22, 2006)">ucoSpy,a system enhancement tool like spy++</a> (0)</li>
	<li><a href="http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html" title="Solution: Crash in Release build with self-defined message (November 28, 2006)">Solution: Crash in Release build with self-defined message</a> (0)</li>
	<li><a href="http://www.ucosoft.com/intermittent-work-in-ui-thread-2.html" title="Intermittent work in UI thread (October 16, 2007)">Intermittent work in UI thread</a> (0)</li>
	<li><a href="http://www.ucosoft.com/intermittent-work-in-ui-thread.html" title="Intermittent work in UI thread (October 16, 2007)">Intermittent work in UI thread</a> (0)</li>
	<li><a href="http://www.ucosoft.com/how-to-sendpost-message-to-cdocument.html" title="How to send/post message to CDocument? (October 24, 2007)">How to send/post message to CDocument?</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-hook-the-file-related-event.html" title="How to hook the file related event (June 22, 2007)">How to hook the file related event</a> (0)</li>
	<li><a href="http://www.ucosoft.com/events-when-click-ontaskbar.html" title="How to catch the events when click on the app icon on taskbar (September 24, 2008)">How to catch the events when click on the app icon on taskbar</a> (0)</li>
	<li><a href="http://www.ucosoft.com/ccommodule-in-porting-atl-dll-project-to-vc8.html" title="CComModule in porting ATL DLL project to VC8 (June 22, 2007)">CComModule in porting ATL DLL project to VC8</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/install-global-hook-without-dll.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Solution: Crash in Release build with self-defined message</title>
		<link>http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html</link>
		<comments>http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html#comments</comments>
		<pubDate>Tue, 28 Nov 2006 15:05:02 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[Release]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/42.html</guid>
		<description><![CDATA[We often meet such case: I have an application with a modeless dialog. The dialog comes up perfectly in DEBUG build, But in RELEASE build, it crashed. Such problem often occures because of incorrect signature of message handle functions, that included into MFC message map for your CWnd based class.We should very carefully check signatures]]></description>
			<content:encoded><![CDATA[<p>We often meet such case: I have an application with a modeless dialog. The dialog comes up perfectly in DEBUG build, But in RELEASE build, it crashed.</p>
<p><!--adsense--></p>
<p>Such problem often occures because of incorrect signature of message handle functions, that included into MFC message map for your CWnd based class.<br />We should very carefully check signatures such functions in comparision with expected one by MFC library. For example, if you put follow handler into message map</p>
<p><coolcode lang="cpp" linenum="no"><br />
ON_MESSAGE(WM_SOME_USER_MESSAGE, OnMyMessage)<br />
</coolcode></p>
<p>and define OnMyMessage as:</p>
<p><coolcode lang="cpp" linenum="no"><br />
void CMyWnd::OnMyMessage()<br />
{<br />
 //do something<br />
}<br />
</coolcode></p>
<p>this code will be compiled without errors. DEBUG build can work without any problems, but RELEASE will crash besause ON_MESSAGE macro expect follow<br />signature:</p>
<p><coolcode lang="cpp" linenum="no"><br />
LRESULT CMyWnd::OnMyMessage(WPARAM /*wParam*/, LPARAM /*lParam*/)</coolcode></p>
<p>DEBUG build works because stack processing has differences for debug and release builds.</p>
<p>so, the solution is simple:</p>
<p><span id="more-42"></span></p>
<p>Solution 1:&nbsp; modify the&nbsp; message handle functions with the following style and it will be ok.</p>
<p><coolcode lang="cpp" linenum="no"><br />
void CMyWnd::OnMyMessage(WPARAM wParam, LPARAM lParam)<br />
</coolcode></p>
<p>Solution 2:&nbsp; don&#8217;t modify the message handle functios, and change the <font color="#0000ff"><strong>ON_MESSAGE macro </strong><font color="#000000">to</font><strong> </strong></font><font color="#0000ff"><strong>ON_MESSAGE_VOID</strong>.</font><br />
<coolcode lang="cpp" linenum="no"><br />
ON_MESSAGE_VOID(WM_SOME_USER_MESSAGE, OnMyMessage)<br />
</coolcode><br />
<br />This macro is in AfxPriv.h, but is largely undocumented.</p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/debug" title="Debug" rel="tag">Debug</a>, <a href="http://www.ucosoft.com/tag/message" title="message" rel="tag">message</a>, <a href="http://www.ucosoft.com/tag/release" title="Release" rel="tag">Release</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/write-and-read-binary-data-in-variant.html" title="Write and read binary data in VARIANT (November 22, 2006)">Write and read binary data in VARIANT</a> (0)</li>
	<li><a href="http://www.ucosoft.com/what-is-the-usage-of-wm_null.html" title="What is the usage of WM_NULL? (November 23, 2006)">What is the usage of WM_NULL?</a> (0)</li>
	<li><a href="http://www.ucosoft.com/ucogrid-a-great-grid-control-of-mfc.html" title="ucoGrid, a great grid control of MFC (November 26, 2006)">ucoGrid, a great grid control of MFC</a> (18)</li>
	<li><a href="http://www.ucosoft.com/sscanf-with-cstring.html" title="sscanf with CString (March 20, 2007)">sscanf with CString</a> (3)</li>
	<li><a href="http://www.ucosoft.com/simple-code-to-create-an-emf-or-bitmap-file-from-existed-draw-code.html" title="Simple code to create an EMF or Bitmap file from existed draw code (December 16, 2006)">Simple code to create an EMF or Bitmap file from existed draw code</a> (0)</li>
	<li><a href="http://www.ucosoft.com/simple-cedit-validation.html" title="Simple CEdit Validation (June 27, 2007)">Simple CEdit Validation</a> (0)</li>
	<li><a href="http://www.ucosoft.com/moving-selection-of-clistctrl.html" title="Moving selection of CListCtrl (December 3, 2006)">Moving selection of CListCtrl</a> (0)</li>
	<li><a href="http://www.ucosoft.com/microsoft-visual-c-tips-and-tricks2.html" title="Microsoft Visual C++ Tips (November 21, 2006)">Microsoft Visual C++ Tips</a> (0)</li>
	<li><a href="http://www.ucosoft.com/intermittent-work-in-ui-thread-2.html" title="Intermittent work in UI thread (October 16, 2007)">Intermittent work in UI thread</a> (0)</li>
	<li><a href="http://www.ucosoft.com/intermittent-work-in-ui-thread.html" title="Intermittent work in UI thread (October 16, 2007)">Intermittent work in UI thread</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is the usage of WM_NULL?</title>
		<link>http://www.ucosoft.com/what-is-the-usage-of-wm_null.html</link>
		<comments>http://www.ucosoft.com/what-is-the-usage-of-wm_null.html#comments</comments>
		<pubDate>Thu, 23 Nov 2006 13:30:02 +0000</pubDate>
		<dc:creator>hamo</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[message]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/36.html</guid>
		<description><![CDATA[Alex Rest found the WM_NULL message and was curious where somebody would need to use that. I have found a strange Windows message. It is described in MSDN so: WM_NULL: The WM_NULL message performs no operation. An application sends the WM_NULL message if it wants to post a message that the recipient window will ignore.]]></description>
			<content:encoded><![CDATA[<p><P>Alex Rest found the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Windows/WindowReference/WindowMessages/WM_NULL.asp">WM_NULL</A> message and was curious where somebody would need to use that.<br />
</P><br />
<coolcode linenum="no"><br />
I have found a strange Windows message.<br />
It is described in MSDN so:</p>
<p>WM_NULL:<br />
The WM_NULL message performs no operation. An application sends<br />
the WM_NULL message if it wants to post a message that the recipient<br />
window will ignore.</p>
<p>I propose a competition for the the best idea how to use it.<br />
</coolcode><br />
<P>This message really isn&#8217;t strange at all. It can be used to verify whether or not an application is still responding. A famous window that does that is the Windows Task Manager by using <a href="http://msdn.microsoft.com/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/MessagesandMessageQueues/MessagesandMessageQueuesReference/MessagesandMessageQueuesFunctions/SendMessageTimeout.asp">SendMessageTimeout</A> with WM_NULL. There are a few more methods were you have to use WM_NULL. JohnCz described them very well:</P><br />
<!--adsense--><br />
<P>&#8220;WM_NULL also is used to filter messages in the <A href="http://msdn.microsoft.com/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Hooks/AboutHooks.asp">WH_GETMESSAGE</A> hook. Returning 0 from a hook does not prevent you from processing a message. Replacing a message with WM_NULL will filter the message properly.&#8221;</P><br />
<P>Another use is an ActiveX control when you decide that it has a toolbar and tooltips. Normally, an application message pump calls PreTranslateMessage; because the ActiveX control does not have one, a message hook can call <A href="http://msdn.microsoft.com/library/en-us/vclib/html/_mfc_cwnd.3a3a.pretranslatemessage.asp">PreTranslateMessage</A> and, if handled, replace the original message with WM_NULL.</P></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/message" title="message" rel="tag">message</a></strong><br />

	<ul class="st-related-posts">
	<li><a href="http://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html" title="Solution: Crash in Release build with self-defined message (November 28, 2006)">Solution: Crash in Release build with self-defined message</a> (0)</li>
	<li><a href="http://www.ucosoft.com/intermittent-work-in-ui-thread-2.html" title="Intermittent work in UI thread (October 16, 2007)">Intermittent work in UI thread</a> (0)</li>
	<li><a href="http://www.ucosoft.com/intermittent-work-in-ui-thread.html" title="Intermittent work in UI thread (October 16, 2007)">Intermittent work in UI thread</a> (0)</li>
	<li><a href="http://www.ucosoft.com/install-global-hook-without-dll.html" title="Install global hook without dll (January 3, 2007)">Install global hook without dll</a> (2)</li>
	<li><a href="http://www.ucosoft.com/how-to-sendpost-message-to-cdocument.html" title="How to send/post message to CDocument? (October 24, 2007)">How to send/post message to CDocument?</a> (2)</li>
	<li><a href="http://www.ucosoft.com/events-when-click-ontaskbar.html" title="How to catch the events when click on the app icon on taskbar (September 24, 2008)">How to catch the events when click on the app icon on taskbar</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/what-is-the-usage-of-wm_null.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
