<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Scheduled window hibernating</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></span>
</font><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scheduled 
Windows Hibernating</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b><font size="3">This is <span lang="en-ca">almost a trivial practice of win32 api. However, I spent a whole night, literally, to implement it.</span></font></b></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<p ALIGN="LEFT"><span lang="en-ca"><b>I listen mp3 player and want to shut down 
my PC at a setup moment. What's more, I want my PC to hibernate instead of 
shutting down. Simple? A sort of, but the &quot;privilege&quot; in windows is a bit tricky 
and the timer is not so easy to use. (Actually there is a &quot;windows&quot; timer which 
doesn't work properly.)</b></span></p>
<div align="left">
  <p ALIGN="LEFT">　</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<p><span lang="en-ca">The only thing I must remember is that the &quot;PHANDLE&quot; or 
HANDLE* cannot be passed as parameter to &quot;OpenProcessToken&quot;. Instead you have to 
pass &amp;HANDLE. This is really strange thing and drives me crazy for whole night.</span></p>
<p>　</p>
<p>　</p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
	<pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left" style="width: 898; height: 86">
  <pre><font size="3"><b><span lang="en-ca">1. winshut.cpp</span></b></font></pre>
  <pre><font size="3" color="#FF0000"><b><span lang="en-ca">file name: winshut</span>.<span lang="en-ca">cpp</span></b></font></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &lt;mmsystem.h&gt;
#include &lt;process.h&gt;

//#include &lt;PowrProf.h&gt;

//

using namespace std;

#pragma comment (lib, &quot;winmm.lib&quot;)


typedef BOOL (*SetSuspendState_Type)(
  BOOL Hibernate,
  BOOL ForceCritical,
  BOOL DisableWakeEvent
);


void hibernate()
{
	HANDLE handle=NULL;
	HMODULE hmodule;
	TOKEN_PRIVILEGES tp;
	//DWORD length;
	LUID luid;
	SetSuspendState_Type proc;


	if (!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &amp;luid))
	{
		printf(&quot;lookup fails\n&quot;);
		exit(23);
	}

	if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &amp;handle)==0)
	{
		printf(&quot;open process token fails and error code is %d\n&quot;, GetLastError());	
		exit(2);
	}

	tp.PrivilegeCount=1;
	tp.Privileges[0].Luid=luid;
	tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;


	if (AdjustTokenPrivileges(handle, FALSE, &amp;tp, sizeof(TOKEN_PRIVILEGES),	NULL, NULL)==0)
	{
		printf(&quot;adjust token privileges fails error=%d \n&quot;, GetLastError());
		exit(5);
	}
/*
	if (ExitWindowsEx(EWX_POWEROFF, 0)==0)
	{
		printf(&quot;failed and error code is %d\n&quot;, GetLastError());
	}
*/
	if ((hmodule=LoadLibrary(&quot;powrprof.dll&quot;))==NULL)
	{
		printf(&quot;load library fails\n&quot;);
		exit(12);
	}
	
	proc=(SetSuspendState_Type)GetProcAddress(hmodule, &quot;SetSuspendState&quot;);

	proc(TRUE, FALSE, TRUE);
}


void CALLBACK timerProc(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
	printf(&quot;prepare to hibernating...\n&quot;);
	//hibernate();
}

LPTIMECALLBACK pTimerProc=timerProc;

 
int main(int argc, char** argv)
{
	UINT delay;
	char* myArgs[2];
	STARTUPINFO startInfo;
	PROCESS_INFORMATION pi;
	int hour, minute, second;
	if (argc==1)
	{
		hour=0;
		minute=0;
		second=3;
	}
	if (argc&gt;2)
	{
		sscanf(argv[1], &quot;%d:%d:%d&quot;, &amp;hour, &amp;minute, &amp;second);
	}
	if (argc==3)
	{
		myArgs[0]=argv[2];
		myArgs[1]=NULL;

		////////////////////////////
		memset(&amp;startInfo, 0, sizeof(STARTUPINFO));
		memset(&amp;pi, 0, sizeof(STARTUPINFO));
		startInfo.cb=sizeof(STARTUPINFO);
	

		if (CreateProcess(argv[2], NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, 
			&amp;startInfo, &amp;pi)==0)
		{
			printf(&quot;create process fails and error=%d\n&quot;, GetLastError());
			exit(9);
		}
	}

	delay=(hour*3600+minute*60+second)*1000;

	if (!timeSetEvent(delay, 0, pTimerProc, 45, TIME_ONESHOT))
	{
		printf(&quot;failed \n&quot;);
	}
	else
	{
		printf(&quot;successful\n&quot;);
	}
	Sleep(delay+5000);
	return 0;
}</pre>
<pre>　</pre>

<pre><span lang="en-ca">			</span></pre>

<pre><span lang="en-ca">				</span> <a href="game24.htm"><img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35"> </pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp;</p>

</body>

</html>