<?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>JEDI Windows API &#187; Vista</title>
	<atom:link href="http://blog.delphi-jedi.net/tag/vista/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.delphi-jedi.net</link>
	<description>Joint Endeavor of Delphi Innovators of Windows Programming</description>
	<lastBuildDate>Wed, 19 Oct 2011 18:52:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Translucent Windows with Aero</title>
		<link>http://blog.delphi-jedi.net/2008/05/01/translucent-windows-with-aero/</link>
		<comments>http://blog.delphi-jedi.net/2008/05/01/translucent-windows-with-aero/#comments</comments>
		<pubDate>Thu, 01 May 2008 12:00:25 +0000</pubDate>
		<dc:creator>MarcoWarm</dc:creator>
				<category><![CDATA[JEDI Windows API Headers]]></category>
		<category><![CDATA[JWA]]></category>
		<category><![CDATA[Vista]]></category>
		<category><![CDATA[Window]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/?p=179</guid>
		<description><![CDATA[&#8220;Have you ever seen the rain (on your background image through a Delphi window)?&#8221; Ok, sorry to all the Creedence Clearwater Revival fans out there . Today we wanna talk about how to achieve translucent Delphi windows with Windows Vista. I know this does not sound like a big deal for all the lucky owners [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Have you ever seen the rain (on your background image through a Delphi window)?&#8221; Ok, sorry to all the Creedence Clearwater Revival fans out there <img src='http://blog.delphi-jedi.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> . Today we wanna talk about how to achieve translucent Delphi windows with Windows Vista. I know this does not sound like a big deal for all the lucky owners of Delphi 2007 since this feature is already implemented in this release.</p>
<p>Now let&#8217;s have a look at the <strong>D</strong>esktop <strong>W</strong>indow <strong>M</strong>anager (<strong>DWM</strong>) API. The DWM is responsible for the composition of windows on the desktop and the DWM API allows developers to control how composition affects a particular window. As you will see, the DWM is responsible for much more than just “glass”.</p>
<p>When it comes to graphics, the terms can be confusing. Before working with transparency and translucency in Windows we should clarify some of them.</p>
<h3>Terminology</h3>
<p style="30px"><span style="bold;"><strong>Transparency</strong> </span>– Refers to the ability to see through something clearly and without obstruction. Think of it as clear glass. Some applications and APIs use the term transparency to refer to a scale that ranges from “completely” transparent to “completely” opaque.</p>
<p><span style="bold;"><strong>Translucency</strong> </span>– People often use translucency and transparency interchangeably but they actually mean very different things. Translucency refers to the ability to see through something where the background appears unclear whether it is out-of-focus or simply blurry in some way. Windows Vista refers to the glass effect as “transparent glass” when it technically should be called translucent glass.</p>
<p><span style="bold;"><strong><span style="color: #000000;">Opacity</span></strong> </span>– Opacity refers to the state of being opaque and opaque refers to something that is neither transparent nor translucent. Some applications and APIs use the term opacity to refer to a scale that ranges from completely opaque to completely transparent.</p>
<p><span style="bold;"><strong>Alpha</strong> <strong>Channel</strong> </span>– An alpha channel provides additional information for each pixel in an image that facilitates compositing images together.</p>
<p><span style="bold;"><strong>Window Regions</strong> </span>– A window’s region determines the area within the window where system permits painting. Although Windows 95 supported window regions, it was not until Windows XP that the default theme used regions to present windows with rounded corners. Although the default Windows Vista theme also presents windows with rounded corners, regions are no longer used unless you resort to the Windows Vista Basic theme.</p>
<p><span style="bold;"><strong>Glass</strong> </span>– Glass is the catchy marketing terms that Windows Vista uses to refer to translucency.</p>
<p><span style="bold;"><strong>Blur</strong> </span>– Some of the DWM APIs refer to blur and again this indicates translucency. Presumably, the Windows developers felt it was easier to spell and comprehend.</p>
<p><span style="bold;"><strong>Desktop</strong> <span style="color: #000000;"><strong>Composition</strong></span> </span>– The DWM performs desktop composition, enabling visual effects on the desktop such as glass, 3D window transitions, etc.</p>
<p><span style="bold;"><strong>RGB</strong> </span>– RGB is short for Red, Green and Blue. RGB values are typically packed into a COLORREF (which is just a Cardinal) as follows: $00BBGGRR. As you can see, the first byte is always zero and the remaining three bytes store the individual red, green and blue values in reverse order. Each color value ranges from zero through 255. If all three values are zero then the result is black. If all three values are 255 then the result is white. For example, to represent red specify $000000FF. As you can see, RGB does not provide an alpha channel.</p>
<p style="30px"><span style="bold;"><strong>ARGB</strong> </span>– ARGB is short for Alpha, Red, Green and Blue. ARGB values are typically packed into an ARGB (which is just a Cardinal) as follows: $AARRGGBB. The first byte stores the alpha value and the remaining three bytes store the red, green and blue values. Note that the color values are stored in the opposite order to RGB.</p>
<p><span style="bold;"><strong>GDI</strong> </span>– The Windows Graphics Device Interface (GDI) API is the original graphics interface used for 2D drawing in Windows. With the exception of a few newer functions, the GDI API does not honor the alpha channel in images. GDI uses RGB values to represent color. The GDI API is wrapped in our beloved Graphics unit.</p>
<p><span style="bold;"><strong>GDI+</strong> </span>– GDI+ was introduced with Windows XP (and Windows Server 2003) to provide a more capable programming model for 2D drawing, imaging and typography and fully supports alpha blending. GDI+ uses ARGB values to represent color. Although it is not necessary to have GDI+ support in Delphi it is a nice feature and you will need it for drawing black lines and text on &#8220;glass&#8221; windows. Why is that? You will see. You can obtain the units for GDI+ at <a href="http://www.progdigy.com/modules.php?name=gdiplus">http://www.progdigy.com/modules.php?name=gdiplus</a></p>
<h3>Now for some code</h3>
<p>If you do not want to read the following stuff you may want to look at the example project at: <a href="http://theunknownones.googlecode.com/svn/Libraries/DWMHelper/">http://theunknownones.googlecode.com/svn/Libraries/DWMHelper/</a></p>
<p>The first thing you&#8217;ll need is to put the following units in your uses clause</p>
<p style="30px"><a href="http://blog.delphi-jedi.net/wp-content/uploads/2008/04/jwadwmapi.zip">JwaDwmApi</a>,<br />
JwaUxTheme</p>
<p>The most important one of them is the first one since it wraps up all the DWM API has to offer. Let us now look how to achieve things. Don&#8217;t be afraid of looking into JwaDwmApi.pas. It looks much more complicated than it is. As always Microsoft developers seem to love passing records to their functions. I don&#8217;t love that all to much so I wrote some helper functions to make live easier. Those can also be found in TUO&#8217;s SVN <a href="http://theunknownones.googlecode.com/svn/Libraries/DWMHelper/">http://theunknownones.googlecode.com/svn/Libraries/DWMHelper/</a></p>
<p>The simplest thing to do is to blur the background behind a window:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">function</span> DWM_EnableBlurBehind<span class="br0">&#40;</span>hwnd : HWND; AEnable: <span class="kw4">Boolean</span>; hRgnBlur : HRGN = <span class="nu0">0</span>; ATransitionOnMaximized: <span class="kw4">Boolean</span> = <span class="kw2">False</span>; AFlags: <span class="kw4">Cardinal</span> = <span class="nu0">1</span><span class="br0">&#41;</span>: HRESULT;<br />
<span class="kw1">var</span><br />
&nbsp; bb : DWM_BLURBEHIND;<br />
<span class="kw1">begin</span><br />
&nbsp; bb.<span class="me1">dwFlags</span>:=AFlags;<br />
&nbsp; bb.<span class="me1">fEnable</span>:=AEnable;<br />
&nbsp; bb.<span class="me1">hRgnBlur</span>:=hRgnBlur;<br />
&nbsp; bb.<span class="me1">fTransitionOnMaximized</span>:=ATransitionOnMaximized;</p>
<p>&nbsp; Result:=DwmEnableBlurBehindWindow<span class="br0">&#40;</span>hwnd, bb<span class="br0">&#41;</span>;<br />
<span class="kw1">end</span>;</div>
<p>You can call the function this way:</p>
<div class="dean_ch" style="white-space: wrap;">DWM_EnableBlurBehind<span class="br0">&#40;</span>Form1.<span class="me1">Handle</span>, <span class="kw2">true</span><span class="br0">&#41;</span>;</div>
<p>Now your window looks like this, easy isn&#8217;t it`?</p>
<p><img src="http://i874.photobucket.com/albums/ab306/the_real_unknown_ones/Article%20Translucent%20with%20Aero/dwm4.jpg" alt="EnableBlurBehind" /></p>
<p><strong>HELP!!!</strong> My Window is opaque and/or white!!! What did I do wrong?<br />
There can be different reasons for that</p>
<ol>
<li>You do not have Windows Vista installed. (Ok this sounds stupid&#8230; but I really got that issue with some newbies)</li>
<li>You only have Vista Home Personal installed or the Destkop Window Manager Service is switched off. (Translucent Windows are only supported under Aero)</li>
<li>Your Form1.Color is different from clBlack=$00000000 (WHY THAT? Calm down <img src='http://blog.delphi-jedi.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  I&#8217;ll explain)</li>
</ol>
<h3>Why it works with black windows only</h3>
<p>As we have learned in the terminology section Delphi and Windows Vista use different color values (RGB vs. ARGB, remember?) Now Delphi draws only RGB colors with the help of GDI routines. Windows however expects ARGB values and assumes to receive one. So the color Windows assumes to be the right one is in most cases the wrong one, due to different byte order and missing alpha value.</p>
<p>How can we pass a valid value to Windows? As we want to receive a translucent window the color we should set to the window is <strong>transparent</strong>.<strong> </strong>The ARGB value for transparent is $00000000. Which color could we take in RGB to represent transparent? Right: BLACK. This is a major drawback for us. Most of the labels, buttons and edits etc. use black as their default foreground color. Now They all will become transparent. You may have or will read various attempts using SetLayeredWindowAttributes in order to change the transparent color but it seems this worked with some preview releases of Vista only.</p>
<p>How can you draw something black, transparent etc. on the window? Use GDI+&#8230;</p>
<h3>More Glass</h3>
<p>The vigilant reader may have observed that DWM_EnableBlurBehind takes a region as parameter. With this parameter you can pass a more or less complicated region to the DWM to tell it where do draw translucent. Try the following:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">var</span><br />
&nbsp; rgn : HRGN;<br />
<span class="kw1">begin</span><br />
&nbsp; rgn:=CreateEllipticRgn<span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">0</span>,Form1.<span class="me1">clientwidth</span>,Form1.<span class="me1">clientheight</span><span class="br0">&#41;</span>;<br />
&nbsp; DWM_EnableBlurBehind<span class="br0">&#40;</span>Form1.<span class="me1">Handle</span>, <span class="kw2">true</span>, rgn, <span class="kw2">false</span>, DWM_BB_ENABLE <span class="kw1">or</span> DWM_BB_BLURREGION<span class="br0">&#41;</span>;<br />
<span class="kw1">end</span>;</div>
<p>And voilà</p>
<p><img src="http://i874.photobucket.com/albums/ab306/the_real_unknown_ones/Article%20Translucent%20with%20Aero/dwm2.jpg" alt="EnableBlurBehind Elliptic Region" /></p>
<h3>Making the Frame thicker</h3>
<p>In order to control the drawing of the blurred window frame you gotta consider another function called DwmExtendFrameIntoClientArea. Because of my lazyness I introduces a little helper:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">function</span> DWM_ExtendFrameIntoClientArea<span class="br0">&#40;</span>hwnd: HWND; ATopHeight, ALeftWidth, ABottomHeight, ARightWidth: <span class="kw4">Integer</span><span class="br0">&#41;</span>: HRESULT;<br />
<span class="kw1">var</span><br />
&nbsp; lMargins : Margins;<br />
<span class="kw1">begin</span><br />
&nbsp; &nbsp;lMargins.<span class="me1">cyTopHeight</span> := ATopHeight;<br />
&nbsp; &nbsp;lMargins.<span class="me1">cyBottomHeight</span> := ABottomHeight;<br />
&nbsp; &nbsp;lMargins.<span class="me1">cxLeftWidth</span> := ALeftWidth;<br />
&nbsp; &nbsp;lMargins.<span class="me1">cxRightWidth</span> := ARightWidth;</p>
<p>&nbsp; &nbsp;Result := DwmExtendFrameIntoClientArea<span class="br0">&#40;</span>hwnd, lMargins<span class="br0">&#41;</span>;<br />
<span class="kw1">end</span>;</div>
<p>Calling it this way</p>
<div class="dean_ch" style="white-space: wrap;">DWM_ExtendFrameIntoClientArea<span class="br0">&#40;</span>Form1.<span class="me1">Handle</span>, <span class="nu0">50</span>, <span class="nu0">50</span>, <span class="nu0">50</span>, <span class="nu0">50</span><span class="br0">&#41;</span>;</div>
<p>Leads to that result:</p>
<p><img src="http://i874.photobucket.com/albums/ab306/the_real_unknown_ones/Article%20Translucent%20with%20Aero/dwm3.jpg" alt="DwmExtendFrameIntoClientArea" /></p>
<h3>I don&#8217;t want a Frame at all</h3>
<p>Sometimes this effect is referred to as &#8220;Sheet Of Glass&#8221; I don&#8217;t know how Microsoft developers call it since they &#8216;abuse&#8217; one of their own functions to reach that effect. I personally would have introduced a distinct function for that. But it shows that people at Microsoft put on their pants one leg at a time as well.</p>
<div class="dean_ch" style="white-space: wrap;">DWM_ExtendFrameIntoClientArea<span class="br0">&#40;</span>Form1.<span class="me1">Handle</span>, <span class="nu0">-1</span>, <span class="nu0">-1</span>, <span class="nu0">-1</span>, <span class="nu0">-1</span><span class="br0">&#41;</span>;</div>
<p>Now you sould see something like that:</p>
<p><img src="http://i874.photobucket.com/albums/ab306/the_real_unknown_ones/Article%20Translucent%20with%20Aero/dwm1.jpg" alt="Sheet of Glass" /></p>
<h3>Conclusion</h3>
<p>There isn&#8217;t really more magic behind it than that. Of course this is a very raw approach. For example the effect will collapse as soon as the screensaver switched on and back off. One could call the DWM_* routines in CreateWindowHandle for example.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/05/01/translucent-windows-with-aero/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Elevate application on Vista with JWSCL</title>
		<link>http://blog.delphi-jedi.net/2008/03/18/elevate-application-on-vista-with-jwscl/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/18/elevate-application-on-vista-with-jwscl/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 13:00:44 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[elevate]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[manifest]]></category>
		<category><![CDATA[UAC]]></category>
		<category><![CDATA[Vista]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/18/elevate-application-on-vista-with-jwscl/</guid>
		<description><![CDATA[This little article shows you how to elevate an application using JWSCL. There are two possible ways to elevate an application : Elevate the application from the beginning Elevate special parts of your application Both ways needs a manifest to be included into your application. 1. Elevate the application from the beginning The easiest way [...]]]></description>
			<content:encoded><![CDATA[<p>This little article shows you how to elevate an application using <strong>JWSCL</strong>.<br />
There are two possible ways to elevate an application :</p>
<ol>
<li>Elevate the application from the beginning</li>
<li>Elevate special parts of your application</li>
</ol>
<p>Both ways needs a manifest to be included into your application.</p>
<p><u>1. Elevate the application from the beginning</u></p>
<p>The easiest way to elevate an application is to tell Windows to do it for you. This is done by applying a manifest.</p>
<blockquote><p>A manifest is a text resource that tells Windows what needs to be prepared before your application starts. This includes theme support and privileges your application needs. Developers maybe know it from Windows XP where it enables themed window controls.</p></blockquote>
<p>Telling Windows to start your application as an administrator is done by using &#8220;requireAdministrator&#8221; as required security level in the manifest. However the code below is just an excerpt. You can get a full version of the manifest with a <a href="#ManifestCode" title="Find the whole manifest data.">warning</a>.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="sc3"><span class="re1">&lt;security<span class="re2">&gt;</span></span></span><br />
&nbsp; <span class="sc3"><span class="re1">&lt;requestedPrivileges<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;requestedExecutionLevel</span> <span class="re0">level</span>=<span class="st0">&quot;requireAdministrator&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; <span class="sc3"><span class="re1">&lt;/requestedPrivileges<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/security<span class="re2">&gt;</span></span></span></div>
<p>If you are going to use this manifest in your application, you have to consider that your application needs to be elevated all the time. Thus users without administrator access cannot launch your application at all.</p>
<p>However there is a second way which does not need a manifest at all. Some applications do start their own process with elevated rights. They start a second instance, close the actual one and go on elevated.<br />
Examples are :</p>
<ul>
<li>the <em>Taskmanager </em>can elevate itself by clicking &#8220;Show processes from all users&#8221;<br />
<img src="http://blog.delphi-jedi.net/wp-content/uploads/2008/03/bild-13.jpg" alt="Bild" height="35" width="212" /></li>
<li><a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx"><em>ProcessExplorer</em></a><br />
<img src="http://blog.delphi-jedi.net/wp-content/uploads/2008/03/bild-15.jpg" alt="Bild" height="141" width="267" /></li>
</ul>
<p>JWSCL contains a unit called <a href="http://jwscldoc.delphi-jedi.net/JwsclElevation.html"><em>JwsclElevation</em></a> which provides the necessary functions to allow this simple elevation.</p>
<p>function <em><a href="http://jwscldoc.delphi-jedi.net/JwsclElevation.html#JwShellExecute">JwShellExecute</a></em>(const hWnd: HWND; FileName, Parameters,<br />
Directory: TJwString; ShowCmd: Integer): HINST;<br />
This function just works like the usual ShellExecute but let&#8217;s you elevate any program or your application. The return value In the following way you can start the command line prompt with elevated privileges.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">var</span> hProc : <span class="kw4">THandle</span>;<br />
<span class="kw1">begin</span><br />
&nbsp; hProc := JwShellExecute<span class="br0">&#40;</span><span class="nu0">0</span>, <span class="st0">&#8216;cmd&#8217;</span>, <span class="st0">&#8221;</span>, <span class="st0">&#8221;</span>, SW_SHOW<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">if</span> hProc &lt;&gt; <span class="nu0">0</span> <span class="kw1">then</span><br />
&nbsp; &nbsp; CloseHandle<span class="br0">&#40;</span>hProc<span class="br0">&#41;</span>;</div>
<p>The developer version (revision &gt; 317) does have some more extension, less bugs and a different behavior.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">function</span> JwShellExecute<span class="br0">&#40;</span><span class="kw1">const</span> hWnd: HWND;<br />
&nbsp; &nbsp; FileName,<br />
&nbsp; &nbsp; Parameters,<br />
&nbsp; &nbsp; Directory: TJwString;<br />
&nbsp; &nbsp; ShowCmd: <span class="kw4">Integer</span>;<br />
&nbsp; &nbsp; Flags : TJwShellExecuteFlags = <span class="br0">&#91;</span>sefNoClosehProcess<span class="br0">&#93;</span><span class="br0">&#41;</span>: HANDLE;</div>
<p>Let me list the differences :</p>
<ul>
<li><em>JwShellExecute</em> raises <em><a href="http://jwscldoc.delphi-jedi.net/JwsclExceptions.EJwsclWinCallFailedException.html">EJwsclWinCallFailedException</a></em> when the call to ShellExecuteEx fails.</li>
<li>The new optional parameter Flags controls some aspects
<ul>
<li><strong>sefNoUi </strong>does not allow ShellExecute to show error messages on the user&#8217;s screen</li>
<li><strong>sefIgnoreElevationIfNotAvailable</strong>. Usually JwShellExecute checks whether the user is already elevated and if so just starts the application. However this call will fail on preVista systems. If this flag is set the function will not fail on preVista systems and just execute the application.</li>
<li><strong>sefFixDirWithRunAs</strong>. ShellExecute ignores the directory parameter if it is called with the &#8220;runas&#8221; verb. However with this flag set, the JEDI function uses a trick to start the application in the correct directory. In this way there maybe a command window visible.</li>
<li><strong>sefNoClosehProcess</strong>. The newer version of JwShellExecute closes the returned process handle for you if this flag is not set. If you need that process handle (e.g. waiting for the process to end) you should remove it.</li>
</ul>
</li>
<li>The return value is a handle to the process. It is now automatically closed for your if you do not deny it through the flag parameter.</li>
</ul>
<p>The following code shows how to start the application with elevated privileges from whithin the same app.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">var</span> NewProcessHandle : HANDLE;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; NewProcessHandle := JwShellExecute<span class="br0">&#40;</span>ForeGroundWindow,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">ParamStr</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>, Parameter, Directory, SW_NORMAL,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span>sefNoUi, sefFixDirWithRunAs,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sefIgnoreElevationIfNotAvailable, sefNoClosehProcess<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">except</span><br />
&nbsp; &nbsp; <span class="kw1">On</span> E : EJwsclWinCallFailedException <span class="kw1">do</span><br />
&nbsp; &nbsp; <span class="kw1">begin</span><br />
&nbsp; &nbsp; &nbsp; LastError := E.<span class="me1">LastError</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="co1">//do error stuff here</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw3">exit</span>;<br />
&nbsp; &nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="co1">//make sure the app has started</span><br />
&nbsp; <span class="kw1">if</span> NewProcessHandle &lt;&gt; <span class="nu0">0</span> <span class="kw1">then</span><br />
&nbsp; <span class="kw1">begin</span><br />
&nbsp; &nbsp; <span class="co1">//wait for the app to be initialized and read to get user input</span><br />
&nbsp; &nbsp; <span class="co1">//or just remove it &#8211; what you like</span><br />
&nbsp; &nbsp; WaitForInputIdle<span class="br0">&#40;</span>NewProcessHandle, yourTimeHere<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; CloseHandle<span class="br0">&#40;</span>NewProcessHandle<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="co1">//terminate your application here</span><br />
&nbsp; <span class="kw1">end</span>;<br />
&#8230;</div>
<p><u>2. Elevate special parts of your application</u></p>
<p>To elevate special parts of your application is the prefered way for a Vista application. This is done by separate the parts of your application that needs administrative rights, into an external process. We do not need to create a new application and do the inter-process communication stuff. Windows Vista uses an external COM process for this task. So the solutions goes with a COM DLL that we have to implement. In addition to that we also have to add a manifest &#8211; of course.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="sc3"><span class="re1">&lt;security<span class="re2">&gt;</span></span></span><br />
&nbsp; <span class="sc3"><span class="re1">&lt;requestedPrivileges<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;requestedExecutionLevel</span> <span class="re0">level</span>=<span class="st0">&quot;asInvoker&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; <span class="sc3"><span class="re1">&lt;/requestedPrivileges<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/security<span class="re2">&gt;</span></span></span></div>
<p>I will not tell the whole story about how to create a COM DLL and do all the stuff that has to be done. You should read <a href="http://developersoven.blogspot.com/2007/02/leveraging-vistas-uac-with-delphi-part.html">Aleksander Oven&#8217;s tutorial</a> how to accomplish that.<br />
I&#8217;m going to show you how you can use the <strong>JWSCL</strong> methods.</p>
<p>Create your COM Dll and implement the COM interface. This may look like this (from the <a href="http://blog.delphi-jedi.net/2008/03/13/run-a-program-elevated/">RunEl</a> example in JWSCL):</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> jwaWindows, ActiveX, Dialogs,<br />
&nbsp; Classes, ComObj, RunElCOM_TLB, StdVcl; &nbsp; &nbsp; &nbsp;</p>
<p><span class="kw1">type</span><br />
&nbsp;TJwRunElevated = <span class="kw1">class</span><span class="br0">&#40;</span>TTypedComObject, IJwRunElevated<span class="br0">&#41;</span><br />
&nbsp;<span class="kw1">protected</span><br />
&nbsp; <span class="kw1">function</span> RunAppElevated<span class="br0">&#40;</span>AppName: <span class="kw4">PWideChar</span>;<br />
&nbsp; &nbsp; &nbsp;Parameter: <span class="kw4">PWideChar</span>; Dir: <span class="kw4">PWideChar</span>;<br />
&nbsp; &nbsp; &nbsp;ClientProcessID: <span class="kw4">LongWord</span>; out NewThreadHandle: <span class="kw4">LongWord</span>;<br />
&nbsp; &nbsp; &nbsp;out NewProcessHandle: <span class="kw4">LongWord</span>;<br />
&nbsp; &nbsp; &nbsp;out ResultValue: <span class="kw4">LongWord</span><span class="br0">&#41;</span>: HResult; &nbsp;<span class="kw1">stdcall</span>; <span class="kw1">end</span>;<br />
&#8230;</div>
<p>The important stuff comes here.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">initialization</span><br />
&nbsp; TElevationClassFactory.<span class="me1">Create</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; @ElevationDescription, <span class="kw2">true</span>,<br />
&nbsp; &nbsp; ComServer, TJwRunElevated,<br />
&nbsp; &nbsp; CLASS_JwRunElevated,<br />
&nbsp; &nbsp; iMultiInstance<span class="br0">&#41;</span>;</div>
<p>Use the <a href="http://jwscldoc.delphi-jedi.net/JwsclElevation.TElevationClassFactory.html"><em>TElevationClassFactory</em></a> or <em>TJwElevationClassFactory</em> (in developer version &gt; 317) for registering the COM class in Windows. After you registered the COM class using regsvr32 (see Tutorial) you can retrieve an instance of the COM class with <em><a href="http://jwscldoc.delphi-jedi.net/JwsclElevation.html#JwCoCreateInstanceAsAdmin">JwCoCreateInstanceAsAdmin</a></em>.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">var</span><br />
&nbsp; ElevatedObject: IJwRunElevated;<br />
&nbsp; Result : HRESULT;<br />
<span class="kw1">begin</span><br />
&nbsp; Result := JwCoCreateInstanceAsAdmin<span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;ForeGroundWindow,<br />
&nbsp; &nbsp; &nbsp; &nbsp;CLASS_JwRunElevated,<br />
&nbsp; &nbsp; &nbsp; &nbsp;IID_IJwRunElevated,<br />
&nbsp; &nbsp; &nbsp; &nbsp;ElevatedObject<span class="br0">&#41;</span>;</div>
<p>If the thread has already elevated privileges the function JwCoCreateInstanceAsAdmin simply returns an instance without using the elevation prompt.</p>
<p>In developer version (revision &gt; 317) the returned result value can contain information about the status.</p>
<ul>
<li><strong>ERROR_SUCCESS</strong> &#8211; No error</li>
<li>(revision &gt; 317) <strong>ERROR_CANCELLED</strong> &#8211; the user has canceled the UAC prompt.</li>
<li>(revision &gt; 317) <strong>E_CLASS_IS_NOT_SETUP</strong> &#8211; the requested COM class has not been setup to be used for elevation.</li>
<li><em>CoGetObject (if elevation is required) </em>or <em>CoCreateInstance </em>(if elevation is not required) may return an error value</li>
</ul>
<p><u><strong>Be warned:<br />
</strong></u>If you try to execute this function on a preVista system, the function is going to fail with EJwsclUnsupportedWindowsVersionException.</p>
<hr size="2" width="100%" />You can find the shown source code in the repository of <strong>JWSCL</strong>. The library has two sample applications that use the technics describes above:</p>
<ul>
<li><a href="http://blog.delphi-jedi.net/2008/03/13/run-a-program-elevated/">RunEl</a> (<em>JwShellExecute </em>and COM DLL) or <a href="https://jedi-apilib.svn.sourceforge.net/svnroot/jedi-apilib/jwscl/trunk/examples/runel/">Current repository</a>.</li>
<li>VistaElevation (COM DLL). <a href="http://blog.delphi-jedi.net/category/downloads/jwscl-downloads/">Download JWSCL</a> or <a href="https://jedi-apilib.svn.sourceforge.net/svnroot/jedi-apilib/jwscl/trunk/examples/VistaElevation/">Current repository</a>.</li>
</ul>
<p><u><strong>Warning:</strong></u><a name="ManifestCode" title="ManifestCode"></a><br />
There is a problem with the manifest scanner on Windows XP. If you try to start an application with a manifest created for Vista you&#8217;re goingt to probably have a <a href="http://en.wikipedia.org/wiki/BSOD"><font color="#0000ff">Blue Screen of Death</font></a>. So you have to get a correct manifest from <a href="http://www.delphipraxis.net/topic130450_vista+uac+programm+mit+adminrechten+starten.html">here</a> (<a href="http://translate.google.com/translate?u=http%3A%2F%2Fwww.delphipraxis.net%2Ftopic130450_vista%2Buac%2Bprogramm%2Bmit%2Badminrechten%2Bstarten.html&amp;langpair=de%7Cen&amp;hl=de&amp;ie=UTF-8">english translation</a>).</p>
<p><strong>Tell me how you liked this blog entry by adding a comment.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/03/18/elevate-application-on-vista-with-jwscl/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Run a program elevated.</title>
		<link>http://blog.delphi-jedi.net/2008/03/13/run-a-program-elevated/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/13/run-a-program-elevated/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 12:53:21 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[Common]]></category>
		<category><![CDATA[JEDI Windows API Headers]]></category>
		<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[certificate]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[DLL]]></category>
		<category><![CDATA[elevated]]></category>
		<category><![CDATA[elevation]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[RunEl]]></category>
		<category><![CDATA[sign]]></category>
		<category><![CDATA[Vista]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/13/run-a-program-elevated/</guid>
		<description><![CDATA[Today I want to introduce a really useful application from the examples in JEDI Windows Security Code Library. In Windows Vista there is no possible way to run a program elevated from the command line. But today there is a cure. I call it RunEl, which stands for Run Elevated. This simple program uses Delphi, [...]]]></description>
			<content:encoded><![CDATA[<p>Today I want to introduce a really useful application from the examples in JEDI Windows Security Code Library. In Windows Vista there is no possible way to run a program elevated from the command line. But today there is a cure. I call it RunEl, which stands for <strong>Run El</strong>evated. This simple program uses Delphi, the JEDI API and JEDI WSCL. It runs only in Windows Vista and Server 2008 with activated UAC. If there is no UAC, it simply starts the requested application.</p>
<p><span id="more-60"></span><br />
If you start the program without any parameter, you&#8217;ll get the help screen.</p>
<blockquote><p>RunEl V1.0 &#8211; Run application elevated<br />
by Christian Wimmer @ 2008<br />
Visit us at http://blog.delphi-jedi.net</p>
<p>RunEl [/INSTALL][/UNINSTALL][/W[D][G]] [AppName] [Parameters]</p>
<p>Parameters:<br />
/INSTALL &#8211; installs the RunElCOM.dll. Needed for parameter /D<br />
RunElCOM.dll must be in the same folder as RunEl.<br />
/UNINSTALL &#8211; uninstalls the RunElCOM.dll.<br />
W &#8211; Wait for called process to be finished.<br />
D &#8211; uses own elevation COM Class. Before using must be setup with /INSTALL .<br />
G &#8211; uses foreground window to display UAC Prompt on.<br />
Parameters W, D and G must be mixed up with only one &#8220;/&#8221;</p></blockquote>
<p>Despite the INSTALL parameter, you do not have to install the program! I implemented two different ways to elevate an application. There is on the one side ShellExecute, which can elevate an application by passing &#8220;runas&#8221; as a parameter. On the other hand, I implemented the usual way by registering a COM DLL. In the last case, you have to register the DLL first. We get back there later.</p>
<p>Let&#8217;s see how we can use it. Open a command line window and type:</p>
<blockquote><p>RunEl cmd</p></blockquote>
<p>This command starts the Windows command line interpreter as an Administrator. You get the UAC prompt, allow the action and a new cmd window is opened. If you disallow the action, RunEl will return an error value through its process return value. This us useful if you want to check the errorlevel in a batch file.</p>
<blockquote><p>Elevation failed. (2147943623) The operation was canceled by the user.</p></blockquote>
<p>If you need to wait for the process to be closed, you can add the <strong>w</strong>ait parameter. This is also mandatory if you want to check your application&#8217;s return value. Because it is clear that we have to wait until the process&#8217; end to get the return value.</p>
<blockquote><p>RunEl /w cmd</p></blockquote>
<p>Sometimes the UAC prompt does not appear and just blinks in the task to get attention. This is because there is no available window. In this case you can specify parameter &#8220;g&#8221;, which uses the foreground window. The UAC prompt will appear again then.</p>
<blockquote><p>RunEl /g cmd</p></blockquote>
<p>Of course you can also combine both parameters. However there must only be one slash.<br />
All these combinations are the same, so it ignores the case-sensitivity and the order of the three options letters.</p>
<blockquote><p>RunEl /wg cmd<br />
RunEl /gw cmd<br />
RunEl /Gw cmd<br />
RunEl /GW cmd</p></blockquote>
<p>The last parameter &#8220;d&#8221; takes the usual way to get elevated privileges. It runs a COM method as the elevated user, creates the process, returns to non-elevated status and then eventually waits for the process or returns immediately.<br />
However you have to install the provided DLL <em>RunElCOM.dll</em> at first. This is done by using RunEl.</p>
<blockquote><p>RunEl /install<br />
The COM DLL was successfully installed.</p></blockquote>
<p>Now you are able to run applications with the &#8220;d&#8221; parameter in the nearly same way as shown above.</p>
<blockquote><p>Run /d cmd<br />
Run /dw cmd<br />
Run /dwg cmd<br />
&#8230;</p></blockquote>
<p>I say nearly because of two issues:</p>
<ol>
<li>You always have to use /d parameter. Otherwise ShellExecute is used</li>
<li>A major problem of ShellExecute is that it ignores the current directory if it is used for elevation. I circumnavigated this problem by using the following command line internally.<br />
<blockquote><p>cmd /C cd /d &lt;current dir&gt; &amp; &lt;your application&gt; &lt;parameters&gt;</p></blockquote>
<p>The disadvantage of this solution is that you do not get the application&#8217;s return value. Cmd is the main process, so we get its return value instead of &lt;your application&gt;.<br />
If you need the correct return value directly, you have to use /d switch.</li>
</ol>
<p>In the end you can uninstall the COM library.</p>
<blockquote><p>runel /uninstall<br />
The COM DLL was successfully uninstalled.</p></blockquote>
<p>If you try to run an application using runel /d you&#8217;ll get the following error message.</p>
<blockquote><p>RunEl /d cmd<br />
Elevation failed. (2148007959) The class is not configured to support Elevated activation.</p></blockquote>
<hr size="2" width="100%" /><u>Statement:</u><br />
I did not sign my application. So you&#8217;ll always get a nasty UAC dialog. But there are several solutions:</p>
<ol>
<li>Do nothing and accept this dialog</li>
<li>Wait until I buy or get the very expensive sign certificate. I tell you that it will last a really long time.</li>
<li>Use your own certificate to sign the file</li>
</ol>
<p><strong><u>Download and Sourecode:</u></strong></p>
<p>You get the newest version of RunEl in the <a href="http://blog.delphi-jedi.net/2008/03/13/download-runel/" title="Go get RunEl.">JWSCL Download section of RunEl</a>.</p>
<p>RunEl is also available through Subversion repository:</p>
<blockquote><p><a href="https://jedi-apilib.svn.sourceforge.net/svnroot/jedi-apilib/jwscl/trunk/examples/runel">https://jedi-apilib.svn.sourceforge.net/svnroot/jedi-apilib/jwscl/trunk/examples/runel</a></p></blockquote>
<p>You can browse the repository or download it with a Subversion client.</p>
<p>Please send bugs to <a href="mailto:mail@delphi-jedi.net">mail@delphi-jedi.net</a> , the <a href="http://sourceforge.net/mail/?group_id=121894" title="Go get more information about our mailinglists.">mailinglist</a> or the <a href="http://sourceforge.net/tracker/?group_id=121894" title="Go to tracker on SF.">forum</a>.</p>
<hr size="2" width="100%" /><strong>Tell me how you liked this blog entry by adding a comment.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/03/13/run-a-program-elevated/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Get the Windows Vista twin token</title>
		<link>http://blog.delphi-jedi.net/2008/03/04/get-the-windows-vista-twin-token/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/04/get-the-windows-vista-twin-token/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 11:29:11 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[integrity]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[Sid]]></category>
		<category><![CDATA[Token]]></category>
		<category><![CDATA[UAC]]></category>
		<category><![CDATA[Vista]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/04/get-the-windows-vista-twin-token/</guid>
		<description><![CDATA[Windows Vista contains a new feature that allows an administrator to work with less privileges. Every time a user who belongs to the administrator group logs on, the LogonUser API creates two tokens. One tokens contains the real power of the user and the second contains only restricted access. We call such a token restricted [...]]]></description>
			<content:encoded><![CDATA[<p>Windows Vista contains a new feature that allows an administrator to work with less privileges. Every time a user who belongs to the administrator group logs on, the LogonUser API creates two tokens. One tokens contains the real power of the user and the second contains only restricted access. We call such a token restricted token. This feature was implemented way back in Windows 2000 . The changes on the restricted token starts with removed privileges and ends with setting the administrator group in the token groups to use for deny only. A deny only Sid is only used for access control entries that deny access. So in our case the access to a file which allows Administrators full access may be disallowed if there is not any other positive element that grants us the access.<br />
Back to the topic. The token groups contains a special Sid that is called an integrity Sid. The token that has the administrator group enabled receives a high integrity Sid. Tthe medium integrity Sid goes to the groups of the restricted token.<br />
The token returned bei LogonUser is always the restricted one. Although you can retrieve the the twin token, you cannot do anything with it if you are not a SYSTEM process or an administrator. However a SYSTEM process may return the powerful token to create a process with full administrator rights. UAC does not do anything else. It gets the user&#8217;s credentials, logs on and uses the twin token (if any, otherwise it prompts for an administrator account credentials) to create the process.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> JwaWindows, JwsclExceptions, JwsclToken;var Token,<br />
&nbsp; &nbsp; TwinToken : TJwSecurityToken;<br />
<span class="kw1">begin</span><br />
&nbsp; Token := TJwSecurityToken.<span class="me1">CreateTokenEffective</span><span class="br0">&#40;</span>MAXIMUM_ALLOWED<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; TwinToken := Token.<span class="me1">LinkedToken</span>;<br />
&nbsp; &nbsp; PrintTokenInfo<span class="br0">&#40;</span>LToken<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">except</span><br />
&nbsp; &nbsp; <span class="kw1">On</span> E : EJwsclSecurityException <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp; <span class="co1">//error logic here</span><br />
&nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; <span class="co1">//do stuff here</span><br />
&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; TwinToken.<span class="me1">Free</span>;<br />
&nbsp; &nbsp; Token.<span class="me1">Free</span>;<br />
&nbsp; <span class="kw1">end</span>;</div>
<p>Both tokens are linked together. Thus the following conditions are true.</p>
<blockquote><p> Token.LinkedToken = TwinToken<br />
TwinToken.LinkedToken = Token</p></blockquote>
<p>You can use the twin token in any way you treat a token if you are powerful enough. Use it in CreateProcess or impersonate the user to do things as the user.</p>
<p>At the end I post a quite useful piece of code that displays primary information about a token.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> &#8230;, JwsclSecurityId,&#8230;;<br />
<span class="kw1">procedure</span> PrintTokenInfo<span class="br0">&#40;</span><span class="kw1">const</span> Token : TJwSecurityToken<span class="br0">&#41;</span>;<br />
<span class="kw1">var</span> SID, SID2 : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span><span class="st0">&#8216;Access: &#8216;</span>,JwFormatAccessRights<span class="br0">&#40;</span>Token.<span class="me1">AccessMask</span>,TokenMapping <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; Sid := Token.<span class="me1">TokenUser</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span><span class="st0">&#8216;TokenUser: &#8216;</span>,SID.<span class="me1">GetText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; Sid.<span class="me1">Free</span>;</p>
<p>&nbsp; Sid := Token.<span class="me1">TokenOwner</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span><span class="st0">&#8216;TokenOwner: &#8216;</span>,SID.<span class="me1">GetText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; Sid.<span class="me1">Free</span>;</p>
<p>&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span><span class="st0">&#8216;TokenGroups: &#8216;</span><span class="re1">#<span class="nu0">13</span></span><span class="re1">#<span class="nu0">10</span></span>,Token.<span class="me1">TokenGroups</span>.<span class="me1">GetText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">end</span>;</div>
<p><strong>Tell me how you liked this blog entry by adding a comment.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/03/04/get-the-windows-vista-twin-token/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

