<?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; Why Sys Admins Hate Me</title>
	<atom:link href="http://www.unifieddiff.com/category/why-sys-admins-hate-me/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>Dipping Duck</title>
		<link>http://www.unifieddiff.com/2009/05/12/dipping-duck/</link>
		<comments>http://www.unifieddiff.com/2009/05/12/dipping-duck/#comments</comments>
		<pubDate>Wed, 13 May 2009 03:53:04 +0000</pubDate>
		<dc:creator>Bob</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Security]]></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=164</guid>
		<description><![CDATA[So I was trying to read a document on my computer at work today, while making notes on a piece of paper. But every five minutes the screensaver would activate, causing me to stop writing, type my password, and find my place in the document again. Needless to say it was quite annoying.


Now you might [...]]]></description>
			<content:encoded><![CDATA[<p>So I was trying to read a document on my computer at work today, while making notes on a piece of paper. But every five minutes the screensaver would activate, causing me to stop writing, type my password, and find my place in the document again. Needless to say it was quite annoying.</p>
<p align="center"><img src="/images/dilbert2045782050802.gif" alt="Dilbert comic" /></p>
<span id="more-164"></span>
<p>Now you might be wondering why I don&#8217;t just change the screensaver time-out or turn off the requirement for a password. Well even though I&#8217;m a local administrator, there is a domain-wide GPO that prevents me from doing so. (Yes I know I can edit the registry, but that setting doesn&#8217;t survive a GP refresh.) I understand the reason for the policy, but five minutes seems a bit too short.</p>
<p>I wanted to fix this problem AND keep my job at the same time. Alice&#8217;s &#8220;dipping duck&#8221; inspired me to write a simple program to simulate mouse movement.</p>


<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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;windows.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> WINAPI WinMain<span style="color: #008000;">&#40;</span> HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, <span style="color: #0000ff;">int</span> nCmdShow <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    HANDLE htmr <span style="color: #000080;">=</span> CreateWaitableTimer<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>, TRUE, L<span style="color: #FF0000;">&quot;CheckIdle&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    LARGE_INTEGER lidt<span style="color: #008080;">;</span>
    LASTINPUTINFO lii<span style="color: #008080;">;</span>
&nbsp;
    __int64 qwdt <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">30</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">10000000</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 30 seconds</span>
    lidt.<span style="color: #007788;">LowPart</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>DWORD<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span>qwdt <span style="color: #000040;">&amp;</span> <span style="color: #208080;">0xFFFFFFFF</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    lidt.<span style="color: #007788;">HighPart</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>LONG<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span>qwdt <span style="color: #000080;">&gt;&gt;</span> <span style="color: #0000dd;">32</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> TRUE <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        SetWaitableTimer<span style="color: #008000;">&#40;</span>htmr, <span style="color: #000040;">&amp;</span>lidt, <span style="color: #0000dd;">0</span>, <span style="color: #0000ff;">NULL</span>, <span style="color: #0000ff;">NULL</span>, FALSE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        WaitForSingleObject<span style="color: #008000;">&#40;</span>htmr, INFINITE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        RtlZeroMemory<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>lii, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>LASTINPUTINFO<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        lii.<span style="color: #007788;">cbSize</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>LASTINPUTINFO<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        BOOL ret <span style="color: #000080;">=</span> GetLastInputInfo<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>lii<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">int</span> threshold <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">60</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 3 minutes</span>
        <span style="color: #0000ff;">int</span> idletime <span style="color: #000080;">=</span> ret <span style="color: #008080;">?</span> <span style="color: #008000;">&#40;</span>GetTickCount<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> lii.<span style="color: #007788;">dwTime</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">1000</span> <span style="color: #008080;">:</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
        BOOL scrnsvr <span style="color: #000080;">=</span> FALSE<span style="color: #008080;">;</span>
        SystemParametersInfo<span style="color: #008000;">&#40;</span>SPI_GETSCREENSAVERRUNNING, <span style="color: #0000dd;">0</span>, <span style="color: #000040;">&amp;</span>scrnsvr, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> idletime <span style="color: #000080;">&gt;</span> threshold <span style="color: #000040;">&amp;&amp;</span> <span style="color: #000040;">!</span>scrnsvr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            MOUSEINPUT mi<span style="color: #008080;">;</span>
            RtlZeroMemory<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>mi, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>MOUSEINPUT<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            mi.<span style="color: #007788;">dwFlags</span> <span style="color: #000080;">=</span> MOUSEEVENTF_MOVE<span style="color: #008080;">;</span>
            mi.<span style="color: #007788;">dx</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
            mi.<span style="color: #007788;">dy</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
            INPUT in<span style="color: #008080;">;</span>
            in.<span style="color: #007788;">type</span> <span style="color: #000080;">=</span> INPUT_MOUSE<span style="color: #008080;">;</span>
            in.<span style="color: #007788;">mi</span> <span style="color: #000080;">=</span> mi<span style="color: #008080;">;</span>
&nbsp;
            SendInput<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #000040;">&amp;</span>in, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>in<span style="color: #008000;">&#41;</span><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;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>



<p>Every thirty seconds the program checks to see if the computer is idle. After three minutes of inactivity (and if the screensaver isn&#8217;t running), it moves the mouse cursor one pixel down and to the right. If I lock the workstation or manually activate the screensaver, the program won&#8217;t do anything. To compile this program, create a new empty C++ Win32 application project. Add a new cpp file, drop in the code above, and hit &#8220;Build Solution&#8221;.</p>
<p>Now the screensaver won&#8217;t be a nuisance when I&#8217;m trying to read. I just have to make sure to hit Win-L before I leave my desk!</p>]]></content:encoded>
			<wfw:commentRss>http://www.unifieddiff.com/2009/05/12/dipping-duck/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
