<?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>Unified Diff &#187; How To</title>
	<atom:link href="http://www.unifieddiff.com/category/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.unifieddiff.com</link>
	<description>I should do that! How hard could it be?!</description>
	<lastBuildDate>Sat, 30 Jan 2010 01:23:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Block Windows Shut Down</title>
		<link>http://www.unifieddiff.com/2010/01/28/block-windows-shut-down/</link>
		<comments>http://www.unifieddiff.com/2010/01/28/block-windows-shut-down/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 18:51:27 +0000</pubDate>
		<dc:creator>Bob</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Why Sys Admins Hate Me]]></category>
		<category><![CDATA[Win32 & MFC]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.unifieddiff.com/?p=252</guid>
		<description><![CDATA[It&#8217;s hard to describe just how much I HATE rebooting my computer. If I have to use a Windows computer for any extended period of time, then I always change update policies to disallow automatic reboots. In fact, I usually click the irritating &#8220;Remind me in ten minutes&#8221; button every ten minutes for three weeks [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s hard to describe just how much I <strong><u>HATE</u></strong> rebooting my computer. If I have to use a Windows computer for any extended period of time, then I always change update policies to disallow automatic reboots. In fact, I usually click the irritating <em>&#8220;Remind me in ten minutes&#8221;</em> button every ten minutes for three weeks before I finally allow Windows to restart (or until I stop the Automatic Updates service).</p>
<p>So you can imagine how annoyed I was to come into work twice this week to the blue Windows logon screen. Every time this happens it takes me twenty minutes to figure out what I was doing the day before, what I have to do today, and where I stopped with my work. And really what made this so much more painful was that it happened without any advanced warning.</p>
<span id="more-252"></span>
<p>And that&#8217;s what got me thinking: could I <em>block</em> restart requests? I researched the Windows shut down process online and then went to work on a prototype. From what I read, calling <a href="http://msdn.microsoft.com/en-us/library/aa376868(VS.85).aspx">ExitWindowsEx</a> sends <em>WM_QUERYENDSESSION</em> to all top-level windows. Applications that are not ready to shut down should return <em>false</em>. I figured the best strategy was to install a system-wide hook and filter the message.</p>
<p>Initially I attempted to capture <em>WM_QUERYENDSESSION</em> with the <em>WH_GETMESSAGE</em> hook and replace it with <em>WM_NULL</em>, but trial-and-error revealed that it&#8217;s sent through <a href="http://msdn.microsoft.com/en-us/library/ms644950(VS.85).aspx">SendMessage</a> and not posted to the window&#8217;s queue. This meant that I couldn&#8217;t filter out the message.</p>
<p>I switched to <em>WH_CALLWNDPROC</em> and was able to capture the message, but not actually modify it. Since my DLL is memory-mapped into the local process space, it seemed like the only way to filter the message was to create a new <em>WindowProc</em> function that handles <em>WM_QUERYENDSESSION</em> and always returns <em>false</em>. Then inside the hook procedure, I could intercept the message and call <a href="http://msdn.microsoft.com/en-us/library/ms633591(VS.85).aspx">SetWindowLong</a> to replace the window&#8217;s message procedure.</p>
<p>This demonstrates the basic concept:


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">LRESULT CALLBACK CallWndProc<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> nCode, WPARAM wParam, LPARAM lParam <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> nCode <span style="color: #000080;">==</span> HC_ACTION <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      CWPSTRUCT <span style="color: #000040;">*</span>msg <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>CWPSTRUCT<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>lParam<span style="color: #008080;">;</span>
&nbsp;
      <span style="color: #ff0000; font-style: italic;">/* hijack the window proc when we see a shut down message */</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> msg<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>message <span style="color: #000080;">==</span> WM_QUERYENDSESSION <span style="color: #008000;">&#41;</span>
          oldwndproc <span style="color: #000080;">=</span> SetWindowLong<span style="color: #008000;">&#40;</span>msg<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>hwnd, GWL_WNDPROC, <span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span>WindowProc<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> CallNextHookEx<span style="color: #008000;">&#40;</span>g_callwndhk, nCode, wParam, lParam<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
LRESULT CALLBACK WindowProc<span style="color: #008000;">&#40;</span> HWND hWnd, UINT uiMessage, 
        WPARAM wParam, LPARAM lParam <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #ff0000; font-style: italic;">/* intercept shut down messages */</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> uiMessage <span style="color: #000080;">==</span> WM_QUERYENDSESSION <span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> DefWindowProc<span style="color: #008000;">&#40;</span>hWnd, uiMessage, wParam, lParam<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>


</p>
<p>When my little application starts, it calls <a href="http://msdn.microsoft.com/en-us/library/ms686227(VS.85).aspx">SetProcessShutdownParameters</a> with level <em>0&#215;4FF</em> to increase the chances of trapping the message first. I figured this was a good idea since I know its <em>WindowProc</em> function can be safely hijacked. Now when Windows sends <em>WM_QUERYENDSESSION</em> the response is always &#8220;NO!&#8221;. The added exclamation there is a call to <a href="http://msdn.microsoft.com/en-us/library/aa376630(VS.85).aspx">AbortSystemShutdown</a>, which is probably unnecessary but I do it just to be safe. Also, I added an alert message box to warn me when a reboot is triggered.</p>
<p>I&#8217;m sort of amazed this actually worked. Some day I&#8217;ll test it against <a href="http://msdn.microsoft.com/en-us/library/aa376873(VS.85).aspx">InitiateSystemShutdown</a> and <a href="http://msdn.microsoft.com/en-us/library/aa376868(VS.85).aspx">ExitWindowsEx</a> with <em>EWX_FORCE</em> to see how it holds up. Interestingly, Windows Vista/7 provides <a href="http://msdn.microsoft.com/en-us/library/aa376877(VS.85).aspx">ShutdownBlockReasonCreate</a> for seemingly outright blocking shut down attempts.</p>
<p>You can obtain the sources to this project <a href="http://www.unifieddiff.com/svn/sandbox/LifeGuard/">here</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.unifieddiff.com/2010/01/28/block-windows-shut-down/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Amarok 1.4 on Leopard</title>
		<link>http://www.unifieddiff.com/2008/06/30/building-amarok-14-on-leopard/</link>
		<comments>http://www.unifieddiff.com/2008/06/30/building-amarok-14-on-leopard/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 18:02:39 +0000</pubDate>
		<dc:creator>Bob</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://www.robertjcarroll.com/?p=31</guid>
		<description><![CDATA[I love Amarok and wanted to install it on my MacBook. With Qt4 on the horizon, it will be possible to run Amarok natively in OS X without an X server. But I didn&#8217;t want to wait for Amarok2 to become stable, so I looked into compiling Amarok 1.4 and running it under X.
I tried [...]]]></description>
			<content:encoded><![CDATA[<p>I love <a href="http://amarok.kde.org/">Amarok</a> and wanted to install it on my MacBook. With Qt4 on the horizon, it will be possible to run Amarok natively in OS X without an X server. But I didn&#8217;t want to wait for Amarok2 to become stable, so I looked into compiling Amarok 1.4 and running it under X.</p>
<p>I tried following the <a href="http://amarok.kde.org/wiki/On_OS_X">On OS X</a> guide with limited success. Apparently, ffmpeg won&#8217;t compile on Leopard and that caused the entire libxine compilation to fail. I tried various combinations of configure flags, but none seemed to help. I looked at the source to try and fix it, but unfortunately I&#8217;m not terribly familiar with x86 assembly.</p>
<span id="more-31"></span>
<p>The problem code had to do with video rendering, so since Amarok doesn&#8217;t use it I just cut it out of the build process. Edit <strong>src/combined/ffmpeg/Makefile</strong> and find<br />
<pre>xineplug_LTLIBRARIES = xineplug_decode_ff.la xineplug_decode_dvaudio.la</pre>
and replace with<br />
<pre>xineplug_LTLIBRARIES = xineplug_decode_dvaudio.la</pre></p>
<p>After that, edit <strong>src/post/Makefile</strong> and find<br />
<pre>SUBDIRS = planar goom visualizations mosaico deinterlace audio</pre>
and replace with<br />
<pre>SUBDIRS = goom visualizations mosaico audio</pre>
Now libxine will build.</p>
<p>With libxine installed, I had a few more steps to build Amarok. I installed taglib<br />
<pre>$ sudo fink install taglib</pre>
I also had to specify the location of the OpenGL framework I wanted to use<br />
<pre>
LDFLAGS=&quot;-Wl,-dylib_file,/System/Library/Frameworks/OpenGL.framework/Versions/ \
  A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/ \
  A/Libraries/libGL.dylib&quot;
</pre>
And finally, this is how I configured Amarok<br />
<pre>./configure --prefix=/sw --with-qt-includes=/sw/include/qt \
  --with-qt-libraries=/sw/lib/qt3/lib</pre></p>
<p>I now have Amarok successfully compiled and running on Leopard. This took me quite a bit of time to get right, so I&#8217;m hoping someone else will find it useful.</p>]]></content:encoded>
			<wfw:commentRss>http://www.unifieddiff.com/2008/06/30/building-amarok-14-on-leopard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Draw ListView with Double Buffering</title>
		<link>http://www.unifieddiff.com/2008/05/30/custom-draw-listview-with-double-buffering/</link>
		<comments>http://www.unifieddiff.com/2008/05/30/custom-draw-listview-with-double-buffering/#comments</comments>
		<pubDate>Sat, 31 May 2008 04:33:19 +0000</pubDate>
		<dc:creator>Bob</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C++/CLI]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Win32 & MFC]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Add new tag]]></category>

		<guid isPermaLink="false">http://www.robertjcarroll.com/?p=29</guid>
		<description><![CDATA[For one of my pet projects, I needed to add a graphic to certain ListView rows. I considered owner-drawing the control, but that seemed messy. What I really wanted was a way to draw on top of whatever Windows painted. I couldn&#8217;t find a nifty .NET way to do this, so remembering a technique from [...]]]></description>
			<content:encoded><![CDATA[<p>For one of my pet projects, I needed to add a graphic to certain ListView rows. I considered owner-drawing the control, but that seemed messy. What I really wanted was a way to draw on top of whatever Windows painted. I couldn&#8217;t find a nifty .NET way to do this, so remembering a technique from the MFC days, I decided to <a href="http://msdn.microsoft.com/en-us/library/bb761817(VS.85).aspx">custom draw</a> the ListView. This would let me inject my own drawing code and let Windows handle the rest. Perfect!</p>
<p>I had originally implemented the ListView using a control style to <a href="http://geekswithblogs.net/cpound/archive/2006/02/27/70834.aspx">eliminate the annoying flicker</a> every time the list was repainted. Once I started custom drawing the control, I saw odd artifacts in the ListView&#8217;s client area when I scrolled or moved the mouse. If I turned off double buffering, it worked fine.</p>
<span id="more-29"></span>
<p>I studied the custom draw process looking for my mistake, but I couldn&#8217;t find any. I even used the <a href="http://www.aisto.com/roeder/dotnet/">.NET Reflector</a> to look at Microsoft&#8217;s implementation. The message I&#8217;m filtering has a handle to the control&#8217;s back-buffer. So it seemed like I should just be able to draw directly into that. But no matter what I did, it produced the same artifacts.</p>
<p>After about three days of banging my head, I managed to make it work by drawing into my own buffer and then using <a href="http://msdn.microsoft.com/en-us/library/ms532278(VS.85).aspx">BitBlt()</a> to copy my image to the control&#8217;s buffer. I also had to set the double buffer style with a window message rather than using the built-in property, although admittedly, I&#8217;m not sure why.</p>
<p>I like visual aids, so here&#8217;s a code snippet from my project. I changed names and removed a bunch of unnecessary lines for easier reading.


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;stdafx.h&quot;</span>
<span style="color: #339900;">#include &quot;MyListViewControl.h&quot;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> MyListViewControl<span style="color: #008080;">;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> System<span style="color: #008080;">::</span><span style="color: #007788;">Drawing</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> System<span style="color: #008080;">::</span><span style="color: #007788;">Windows</span><span style="color: #008080;">::</span><span style="color: #007788;">Forms</span><span style="color: #008080;">;</span>
&nbsp;
MyListViewControl<span style="color: #008080;">::</span><span style="color: #007788;">MyListViewControl</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> MyListViewControl<span style="color: #008080;">::</span><span style="color: #007788;">CustomDraw</span><span style="color: #008000;">&#40;</span> Message<span style="color: #000040;">%</span> m <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	System<span style="color: #008080;">::</span><span style="color: #007788;">Drawing</span><span style="color: #008080;">::</span><span style="color: #007788;">Rectangle</span><span style="color: #000040;">^</span> mprc<span style="color: #008080;">;</span>
	System<span style="color: #008080;">::</span><span style="color: #007788;">String</span><span style="color: #000040;">^</span> cchQueueIndex<span style="color: #008080;">;</span>
	ListViewItem<span style="color: #000040;">^</span> mpItem<span style="color: #008080;">;</span>
	LPNMCUSTOMDRAW lpnmcd<span style="color: #008080;">;</span>
	Graphics<span style="color: #000040;">^</span> mpgfx<span style="color: #008080;">;</span>
	HDC hmemdc<span style="color: #008080;">;</span>
	RECT sOrderRect<span style="color: #008080;">;</span>
	RECT sStopRect<span style="color: #008080;">;</span>
	Brush<span style="color: #000040;">^</span> mpbgbr<span style="color: #008080;">;</span>
	Brush<span style="color: #000040;">^</span> mpfgbr<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> nOrderLen<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> nOrderOffset<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> nRightEdge<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> nRectWidth<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> nItem<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Grab the custom draw info</span>
	lpnmcd <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LPNMCUSTOMDRAW<span style="color: #008000;">&#41;</span> m.<span style="color: #007788;">LParam</span>.<span style="color: #007788;">ToPointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Figure out which drawing stage we're in</span>
	<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span> lpnmcd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dwDrawStage <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>	
&nbsp;
	<span style="color: #666666;">// Control Pre-paint</span>
	<span style="color: #0000ff;">case</span><span style="color: #008000;">&#40;</span> CDDS_PREPAINT <span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span>
&nbsp;
		<span style="color: #666666;">// Request to be notified during item draw</span>
		__super<span style="color: #008080;">::</span><span style="color: #007788;">WndProc</span><span style="color: #008000;">&#40;</span> m <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		m.<span style="color: #007788;">Result</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>System<span style="color: #008080;">::</span><span style="color: #007788;">IntPtr</span><span style="color: #008000;">&#41;</span> CDRF_NOTIFYITEMDRAW<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
	<span style="color: #666666;">// List Item Pre-paint</span>
	<span style="color: #0000ff;">case</span><span style="color: #008000;">&#40;</span> CDDS_ITEMPREPAINT <span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span>
&nbsp;
		<span style="color: #666666;">// Request to be notified after item draw</span>
		__super<span style="color: #008080;">::</span><span style="color: #007788;">WndProc</span><span style="color: #008000;">&#40;</span> m <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		m.<span style="color: #007788;">Result</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>System<span style="color: #008080;">::</span><span style="color: #007788;">IntPtr</span><span style="color: #008000;">&#41;</span> CDRF_NOTIFYPOSTPAINT<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
	<span style="color: #666666;">// List Item Post-paint</span>
	<span style="color: #0000ff;">case</span><span style="color: #008000;">&#40;</span> CDDS_ITEMPOSTPAINT <span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span>
&nbsp;
		__super<span style="color: #008080;">::</span><span style="color: #007788;">WndProc</span><span style="color: #008000;">&#40;</span> m <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #666666;">// Get the current item</span>
		nItem <span style="color: #000080;">=</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #008000;">&#40;</span> lpnmcd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>dwItemSpec <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		mpItem <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>ListViewItem<span style="color: #000040;">^</span><span style="color: #008000;">&#41;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Items<span style="color: #008000;">&#91;</span>nItem<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>		
&nbsp;
		<span style="color: #666666;">// Get the device context and item bounds</span>
		mpgfx <span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>CreateGraphics<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		mprc <span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetItemRect<span style="color: #008000;">&#40;</span> nItem, ItemBoundsPortion<span style="color: #008080;">::</span><span style="color: #007788;">Label</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #666666;">// *** Your custom drawing code here ***</span>
&nbsp;
		<span style="color: #666666;">// Copy the bubble to the screen DC</span>
		hmemdc <span style="color: #000080;">=</span> <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>HDC<span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #008000;">&#40;</span>mpgfx<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetHdc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		BitBlt<span style="color: #008000;">&#40;</span> 
			lpnmcd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>hdc, 
			sOrderRect.<span style="color: #007788;">left</span>, 
			mprc<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Y, 
			nRectWidth, 
			mprc<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Height, 
			hmemdc, 
			sOrderRect.<span style="color: #007788;">left</span>, 
			mprc<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Y, 
			SRCCOPY <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		mpgfx<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ReleaseHdc<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span>System<span style="color: #008080;">::</span><span style="color: #007788;">IntPtr</span><span style="color: #008000;">&#41;</span> hmemdc <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #666666;">// Let Windows redraw the border</span>
		m.<span style="color: #007788;">Result</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>System<span style="color: #008080;">::</span><span style="color: #007788;">IntPtr</span><span style="color: #008000;">&#41;</span> CDRF_DODEFAULT<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> MyListViewControl<span style="color: #008080;">::</span><span style="color: #007788;">WndProc</span><span style="color: #008000;">&#40;</span> Message<span style="color: #000040;">%</span> m <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	LPNMHDR lpnmhdr<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span> m.<span style="color: #007788;">Msg</span> <span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #666666;">// Reflected Notifications</span>
	<span style="color: #0000ff;">case</span><span style="color: #008000;">&#40;</span> OCM_NOTIFY <span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span>
&nbsp;
		<span style="color: #666666;">// Get the notification</span>
		lpnmhdr <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LPNMHDR<span style="color: #008000;">&#41;</span> m.<span style="color: #007788;">LParam</span>.<span style="color: #007788;">ToPointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> lpnmhdr <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #666666;">// We want custom draw messages</span>
		<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> lpnmhdr<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>code <span style="color: #000080;">==</span> NM_CUSTOMDRAW  
			<span style="color: #000040;">&amp;&amp;</span> lpnmhdr<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>hwndFrom <span style="color: #000080;">==</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Handle.<span style="color: #007788;">ToPointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>CustomDraw<span style="color: #008000;">&#40;</span>m<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #008000;">&#125;</span>
&nbsp;
	__super<span style="color: #008080;">::</span><span style="color: #007788;">WndProc</span><span style="color: #008000;">&#40;</span> m <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>


</p>
<p>And here&#8217;s the header file with the double buffering message call:


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#pragma once</span>
&nbsp;
<span style="color: #0000ff;">namespace</span> MyListView
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span> ref <span style="color: #0000ff;">class</span> MyListView <span style="color: #008080;">:</span> 
		<span style="color: #0000ff;">public</span> System<span style="color: #008080;">::</span><span style="color: #007788;">Windows</span><span style="color: #008080;">::</span><span style="color: #007788;">Forms</span><span style="color: #008080;">::</span><span style="color: #007788;">ListView</span>
	<span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
		<span style="color: #0000ff;">bool</span> CustomDraw<span style="color: #008000;">&#40;</span> System<span style="color: #008080;">::</span><span style="color: #007788;">Windows</span><span style="color: #008080;">::</span><span style="color: #007788;">Forms</span><span style="color: #008080;">::</span><span style="color: #007788;">Message</span><span style="color: #000040;">%</span> m <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		<span style="color: #0000ff;">virtual</span> property <span style="color: #0000ff;">bool</span> DoubleBuffered
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">bool</span> get<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> override
			<span style="color: #008000;">&#123;</span>
				LRESULT lStyles <span style="color: #000080;">=</span> <span style="color: #008080;">::</span><span style="color: #007788;">SendMessage</span><span style="color: #008000;">&#40;</span> 
					<span style="color: #008000;">&#40;</span>HWND<span style="color: #008000;">&#41;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Handle.<span style="color: #007788;">ToPointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, 
					LVM_GETEXTENDEDLISTVIEWSTYLE, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				<span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>lStyles <span style="color: #000040;">&amp;</span> LVS_EX_DOUBLEBUFFER<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #0000ff;">void</span> set<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">bool</span> fValue <span style="color: #008000;">&#41;</span> override
			<span style="color: #008000;">&#123;</span>
				<span style="color: #666666;">// I tried using the DoubleBuffered property, but I</span>
				<span style="color: #666666;">// get artifacts. So, let's do this the MFC way...</span>
				<span style="color: #008080;">::</span><span style="color: #007788;">SendMessage</span><span style="color: #008000;">&#40;</span> 
					<span style="color: #008000;">&#40;</span>HWND<span style="color: #008000;">&#41;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Handle.<span style="color: #007788;">ToPointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, 
					LVM_SETEXTENDEDLISTVIEWSTYLE, 
					LVS_EX_DOUBLEBUFFER, 
					LVS_EX_DOUBLEBUFFER <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
		<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> WndProc<span style="color: #008000;">&#40;</span> System<span style="color: #008080;">::</span><span style="color: #007788;">Windows</span><span style="color: #008080;">::</span><span style="color: #007788;">Forms</span><span style="color: #008080;">::</span><span style="color: #007788;">Message</span><span style="color: #000040;">%</span> m <span style="color: #008000;">&#41;</span> override<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		MyListViewControl<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>


</p>
<p>I spend days searching online and couldn&#8217;t find anyone who talked about custom drawing a .NET ListView with double buffering. I hope somebody will find this useful.</p>]]></content:encoded>
			<wfw:commentRss>http://www.unifieddiff.com/2008/05/30/custom-draw-listview-with-double-buffering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HOW TO: Samba as an AD Domain Member</title>
		<link>http://www.unifieddiff.com/2006/10/04/samba-ad-domain-member/</link>
		<comments>http://www.unifieddiff.com/2006/10/04/samba-ad-domain-member/#comments</comments>
		<pubDate>Thu, 05 Oct 2006 01:30:55 +0000</pubDate>
		<dc:creator>Bob</dc:creator>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Samba]]></category>

		<guid isPermaLink="false">http://www.robertjcarroll.com/2006/10/05/samba-ad-domain-member/</guid>
		<description><![CDATA[There are probably a million and one articles about how to make Samba 3 an Active Directory domain member. But with all of that, this process still seems to require hours of research. So I&#8217;ve decided to compile my latest experiences here.
The domain member box is running Gentoo Linux. So you may need to adjust [...]]]></description>
			<content:encoded><![CDATA[<p>There are probably a million and one articles about how to make Samba 3 an Active Directory domain member. But with all of that, this process still seems to require hours of research. So I&#8217;ve decided to compile my latest experiences here.</p>
<p>The domain member box is running Gentoo Linux. So you may need to adjust the steps to fit your flavor. Make sure the USE flags kerberos, ldap, samba, ssl, and winbind are set. Start by installing an NTP client.
<pre># emerge ntp</pre></p>
<span id="more-24"></span>
<p>The purpose of NTP is to keep your computer&#8217;s clock in-sync with the domain controller. Edit your /etc/ntp.conf file to use your domain controller as the time server. Then sync your clock, start the NTP client, and install Samba:</p>
<pre>
# ntpdate ad01.rit.edu
# /etc/init.d/ntpd start
# rc-update add ntpd default
# emerge samba
</pre>
<p>Now you can configure kerberos. Open your /etc/krb5.conf file and make it look like this:</p>
<pre>
[libdefaults]
  ticket_lifetime = 600
  default_realm = RIT.EDU
  clockskew = 120

[realms]
  RIT.EDU = {
    kdc = ad01.rit.edu
    default_domain = RIT.EDU
  }

[domain_realm]
  .rit.edu = RIT.EDU
  rit.edu = RIT.EDU
</pre>
<p>Test your kerberos setup by requesting a ticket from your domain controller.</p>
<pre>
# kinit administrator
# klist
# kdestroy</pre>
<p>Edit your Samba configuration.</p>
<pre>
[global]
  workgroup = RIT
  realm = RIT.EDU
  server string =

  log file = /var/log/samba/log.%m
  max log size = 50

  hosts allow = 127.0.0.1 129.21.0.0/16   
  hosts deny = 0.0.0.0/0
  security = ADS
  allow trusted domains = yes
  password server = ad01.rit.edu
  encrypt passwords = yes
  min protocol = NT1

  winbind enum users = yes
  winbind enum groups = yes
  winbind cache time = 600
  winbind use default domain = yes
  template homedir = /home/%U
  obey pam restrictions = yes
  template shell = /bin/bash

  socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192
  domain master = no
  local master = no

  idmap uid = 10000-99999
  idmap gid = 10000-99999
</pre>
<p>Add winbind to the daemon_list variable in /etc/conf.d/samba.</p>
<pre>daemon_list="smbd nmbd <strong>winbind</strong>"</pre>
<p>Join the domain and start Samba.</p>
<pre>
# net ads join -U administrator
# /etc/init.d/samba start
</pre>
<p>Add winbind to the passwd and group lines in /etc/nsswitch.conf.</p>
<pre>
passwd:      compat <strong>winbind</strong>
shadow:      compat
group:       compat <strong>winbind</strong>
</pre>
<p>Test user resolution and add Samba to the startup:</p>
<pre>
# getent passwd administrator
# rc-update add samba default
</pre>
<p>Add the bolded lines to your /etc/pam.d/system-auth file.</p>
<pre>
auth required pam_env.so
<strong>auth sufficient	pam_winbind.so</strong>
auth sufficient	pam_unix.so likeauth nullok <strong>use_first_pass</strong>
auth required pam_deny.so

account	required pam_access.so
<strong>account	sufficient pam_winbind.so</strong>
account	required pam_unix.so

password required pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3
password sufficient pam_unix.so nullok md5 shadow use_authtok
password required pam_deny.so

session	required pam_limits.so
session	required pam_unix.so
<strong>session required pam_mkhomedir.so</strong>
</pre>
<p>That&#8217;s it! Now all you need to do is start samba and your box is an AD domain member.</p>]]></content:encoded>
			<wfw:commentRss>http://www.unifieddiff.com/2006/10/04/samba-ad-domain-member/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HOW TO: Work Least-Privileged in Windows XP</title>
		<link>http://www.unifieddiff.com/2006/02/25/howto-least-privileged-winxp/</link>
		<comments>http://www.unifieddiff.com/2006/02/25/howto-least-privileged-winxp/#comments</comments>
		<pubDate>Sat, 25 Feb 2006 23:48:53 +0000</pubDate>
		<dc:creator>Bob</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.robertjcarroll.com/2006/02/25/howto-least-privileged-winxp/</guid>
		<description><![CDATA[Have you ever been the victim of a computer virus, worm, or other malicious software program? If so, then you understand what a pain it is to recover. Often times, the only way to fix the problems is to do a clean re-install of Windows. Of course you can buy anti-virus and anit-spyware products, but [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever been the victim of a computer virus, worm, or other malicious software program? If so, then you understand what a pain it is to recover. Often times, the only way to fix the problems is to do a clean re-install of Windows. Of course you can buy anti-virus and anit-spyware products, but these programs are all meant for after-the-fact. Wouldn&#8217;t it be nice to be able to stop malicious software (malware) in its tracks <u>before</u> damage is done?</p>
<p>Enter the &#8220;least privilege&#8221; security model. This approach to computing is very simple &#8212; only give yourself enough privileges to accomplish the task you need to perform. For example: do you need to be able to delete all of your system files while you write a letter in Microsoft Word? The answer would be &#8220;no, of course not!&#8221; But running with full privileges, you would be able to do so.</p>
<span id="more-13"></span>
<p>This is how malware spreads. Users visit websites that exploit known vulnerabilities in Internet Explorer. Since the default setup in Windows XP is to be an administrator, these programs can freely install themselves without your knowledge or consent. Working least-privileged solves this problem because without the privileges to tamper with system files, malware can&#8217;t invade your computer (easily).</p>
<p>Are you sold yet? Great! Now let&#8217;s set it up. Traditionally, working with with a non-privileged account has a real pain. But there are some tricks to help you.</p>
&nbsp;<br />
<p><b>1. Creating an Unprivileged Account</b></p>
<p>This step really depends on how you are setup right now. If you already have an account, then we need to adjust your privileges. Either way, you&#8217;ll need to logoff (Start > Logoff). Once you&#8217;re at the Welcome screen, press and hold the Control and Alt keys and the press the Delete key twice. You should now be presented with a login window. Type &#8220;administrator&#8221; for the user name and your password (or leave it empty).</p>
<p>Once logged in, pop yourself into the &#8220;User Accounts&#8221; control panel (Start > Control Panel > User Accounts). Select your own user account and choose &#8220;Change my account type&#8221;. Select &#8220;Limited User&#8221; and click OK. If you don&#8217;t have a password on your &#8220;administrator&#8221; account, you will want to set one now.</p>
<p><b>2. Turn-off Simple File Sharing (Optional)</b></p>
<p>This may fall more into the category of preference, but I think it makes life easier. Windows manages permission on securable objects (this includes files and folders) with Access Control Lists (ACL). When Simple File Sharing is turned-on, Windows will handle editing these lists for you. Doing this yourself gives you greater flexibility, but can cause you to lose data if you make a mistake.</p>
<p>To do this, open &#8220;My Computer&#8221;. From the &#8220;Tools&#8221; menu, choose &#8220;Folder Options&#8221;. Click on the &#8220;View&#8221; tab and you should see of list with checkboxes. Scroll to the bottom of this list, uncheck &#8220;Use Simple File Sharing (Recommended)&#8221;, and click OK.</p>
<p><b>3. Changing the Default Owner (XP Pro Only)</b></p>
<p>When a member of the Administrators group creates a file or folder, that user is the default owner. This can cause headaches when running least-privileged because you could end-up with write access to system files. By making the Administrators group the default owner, you eliminate that risk.</p>
<p>To accomplish this, you will need to open the Local Security Policy editor (Control Panel > Administrative Tools > Local Security Policy). In the left-hand pane, choose &#8220;Local Policies&#8221; and then &#8220;Security Options&#8221;. In the right-hand pane, find &#8220;System objects: Default owner for objects created by members of the administrators group&#8221;. Choose &#8220;Administrators Group&#8221; from the drop-down list and click OK.</p>
<p><b>4. Secondary Logon Service</b></p>
<p>What happens when you&#8217;re working and you need to make a change? Should you logoff and logon as an administrator? No!&#8230; there is an easier way. The Secondary Logon service allows you to  run any application as another user by right-clicking on the file and choosing &#8220;Run As&#8230;&#8221;.</p>
<p>This should be enabled by default, but lets just make sure. Run the Computer Management applet (Control Panel > Administrative Tools > Computer Management). Choose &#8220;Services &#038; Applications&#8221; and then &#8220;Services&#8221;. In the right-hand pane, find &#8220;Secondary Logon&#8221;, right-click, and choose &#8220;Properties&#8221;. Make sure startup type is &#8220;Automatic&#8221; and then click &#8220;Start&#8221; if the service is not already started.</p>
<p><b>5. Elevate Your Own Account (Optional)</b></p>
<p>Somtimes you need to make changes to your own account, but require administive privileges. You could change your account privileges (using Run As) and then logoff and logon again. But there is an easier way&#8230; <a href="http://www.robertjcarroll.com/projects/general-utilities#makeadmin">Make Admin</a>. This program will allow you to change your account privileges on-the-fly for specific applications.</p>
<br/>
<p>Well, that was relatively painless (I hope). It will take some getting used to, but in the long-run, you&#8217;ll be glad you did. Oh, one more tip: you should do this on a clean install of Windows for best results.</p>]]></content:encoded>
			<wfw:commentRss>http://www.unifieddiff.com/2006/02/25/howto-least-privileged-winxp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
