<?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; sdk</title>
	<atom:link href="http://www.ucosoft.com/tag/sdk/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ucosoft.com</link>
	<description>Free Source Code and Program Tips</description>
	<lastBuildDate>Thu, 22 Jul 2010 05:17:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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 />
]]></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 />
]]></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>How to read sectors of a physical drive under Win32?</title>
		<link>http://www.ucosoft.com/how-to-read-sectors-of-a-physical-drive-under-win32.html</link>
		<comments>http://www.ucosoft.com/how-to-read-sectors-of-a-physical-drive-under-win32.html#comments</comments>
		<pubDate>Mon, 15 Jan 2007 15:44:52 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[sdk]]></category>
		<category><![CDATA[win32]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/74.html</guid>
		<description><![CDATA[How to read the sectors of physical drive under win32? The API ReadFile is most useful function, and with it, you can read the physical sectors directly. Here is a sample function, BOOL ReadSectors(&#8230;) BOOL ReadSectors( BYTE bDrive, //drive index DWORD dwStartSector, //start sector WORD wSectors, //sectors number LPBYTE lpSectBuff) { ã€€if (bDrive == 0) [...]]]></description>
			<content:encoded><![CDATA[<p><P>How to read the sectors of physical drive under win32? </p>
<p>The API ReadFile is most useful function, and with it, you can read the physical sectors directly.</p>
<p>Here is a sample function, <b>BOOL ReadSectors(&#8230;)</B><br />
</P><br />
<coolcode lang="cpp" linenum="no" download="ReadSector.cpp"><br />
BOOL ReadSectors(<br />
    BYTE bDrive,  //drive index<br />
    DWORD dwStartSector, //start sector<br />
    WORD wSectors,         //sectors number<br />
    LPBYTE lpSectBuff)<br />
{<br />
ã€€if (bDrive == 0) return 0;<br />
ã€€char devName[] = &#8220;\\\\.\\A:&#8221;;<br />
ã€€devName[4] =&#8217;A&#8217; + bDrive &#8211; 1;<br />
ã€€HANDLE hDev = CreateFile(devName, GENERIC_READ,<br />
                         FILE_SHARE_WRITE, NULL,<br />
                         OPEN_EXISTING, 0, NULL);<br />
ã€€if (hDev == INVALID_HANDLE_VALUE) return 0;<br />
ã€€SetFilePointer(hDev, 512 * dwStartSector,<br />
            0, FILE_BEGIN);<br />
ã€€DWORD dwCB;<br />
ã€€BOOL bRet = ReadFile(hDev, lpSectBuff,<br />
                 512 * wSectors, &#038;dwCB, NULL);<br />
ã€€CloseHandle(hDev);<br />
ã€€return bRet;<br />
}<br />
</coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/api" title="API" rel="tag">API</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/sdk" title="sdk" rel="tag">sdk</a>, <a href="http://www.ucosoft.com/tag/win32" title="win32" rel="tag">win32</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/how-to-read-sectors-of-a-physical-drive-under-win32.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get a fixed code for serial number(2)</title>
		<link>http://www.ucosoft.com/how-to-get-a-fixed-code-for-serial-number2.html</link>
		<comments>http://www.ucosoft.com/how-to-get-a-fixed-code-for-serial-number2.html#comments</comments>
		<pubDate>Thu, 11 Jan 2007 00:41:45 +0000</pubDate>
		<dc:creator>support</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[sdk]]></category>
		<category><![CDATA[serial number]]></category>
		<category><![CDATA[shareware]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/73.html</guid>
		<description><![CDATA[I have post an article about &#8220;How to get a fixed code for serial number&#8221; at http://www.ucosoft.com/archives/51.html. In that article, I suggest you&#160;try WMI class to retrieve &#160;some hardware ID. But there is some limitation of this method. The WMI redist package is not always available on user&#8217;s system, for example, Windows NT4 and Windows [...]]]></description>
			<content:encoded><![CDATA[<p>I have post an article about &#8220;How to get a fixed code for serial number&#8221; at <a href="http://www.ucosoft.com/archives/51.html">http://www.ucosoft.com/archives/51.html</a>. In that article, I suggest you&nbsp;try <a href="http://www.ucosoft.com/archives/60.html">WMI class</a> to retrieve &nbsp;some hardware ID. But there is some limitation of this method.</p>
<p>The WMI redist package is not always available on user&#8217;s system, for example, Windows NT4 and Windows 95/98.&nbsp; And the <a href="http://en.wikipedia.org/wiki/Volume_serial_number">Volume Serial Number of HDD</a> can be easily changed while the&nbsp;CPU ID is not always available. </p>
<p>As <em>Walter Wang</em> said, the manufacturer of harddisk&nbsp;stored a unique Firmware Serial Number in the harddisk when it&#8217;s built. It doesn&#8217;t change with Operating System Changes or HDD formatting. So I should serve your purpose.</p>
<p>For source code example on how to retrieve this HDD Firmware Serial Number using DeviceIoControl, you can refer to following <a href="http://www.winsim.com/diskid32/diskid32.html">this article</a>. It support IDE, SATA and SCSI drive type.</p>
<p><a href="http://www.winsim.com/diskid32/diskid32.html">http://www.winsim.com/diskid32/diskid32.html</a></p>
<p>
Local download: <span id="more-73"></span><a href="http://www.ucosoft.com/wp-content/download/diskid32.zip">diskid32.zipp</a><br />
Simple output of diskid32:
</p>
<p><coolcode lang="cpp" linenum="no"><br />
Primary Controller &#8211; Master drive</p>
<p>Drive Model Number________________: ST380011A<br />
Drive Serial Number_______________: 3JV6TBXW<br />
Drive Controller Revision Number__: 3.04<br />
Controller Buffer Size on Drive___: 2097152 bytes<br />
Drive Type________________________: Fixed<br />
Drive Size________________________: 76264796672 bytes</p>
<p>Trying to read the drive IDs using the SCSI back door</p>
<p>Primary Controller &#8211; Master drive</p>
<p>Drive Model Number________________: ST380011A<br />
Drive Serial Number_______________: 3JV6TBXW<br />
Drive Controller Revision Number__: 3.04<br />
Controller Buffer Size on Drive___: 2097152 bytes<br />
Drive Type________________________: Fixed<br />
Drive Size________________________: 76264796672 bytes</p>
<p>Trying to read the drive IDs using physical access wi</p>
<p>**** STORAGE_DEVICE_DESCRIPTOR for drive 0 ****<br />
Vendor Id = (<br />
Product Id = ST380011A<br />
Product Revision = 3.04<br />
Serial Number = 3JV6TBXW</p>
<p>Hard Drive Serial Number__________: 3JV6TBXW<br />
Hard Drive Model Number___________: ST380011A<br />
Computer ID_______________________: 652190462<br />
MAC Address: 00-FF-5B-FE-6B-20<br />
MAC Address: 00-0C-76-74-DA-50<br />
</coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/api" title="API" rel="tag">API</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/sdk" title="sdk" rel="tag">sdk</a>, <a href="http://www.ucosoft.com/tag/serial-number" title="serial number" rel="tag">serial number</a>, <a href="http://www.ucosoft.com/tag/shareware" title="shareware" rel="tag">shareware</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/how-to-get-a-fixed-code-for-serial-number2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

