<?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>GSaraiva&#039;s Projects &#187; Process32Next</title>
	<atom:link href="http://projects.pro.br/gsaraiva/tag/process32next/feed/" rel="self" type="application/rss+xml" />
	<link>http://projects.pro.br/gsaraiva</link>
	<description>Step by step</description>
	<lastBuildDate>Tue, 01 Sep 2009 13:14:24 +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>Delphi: Utils :: AppRunningCount</title>
		<link>http://projects.pro.br/gsaraiva/2009/07/delphi-utils-apprunningcount/</link>
		<comments>http://projects.pro.br/gsaraiva/2009/07/delphi-utils-apprunningcount/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 20:13:38 +0000</pubDate>
		<dc:creator>Gilberto Saraiva</dc:creator>
				<category><![CDATA[articles]]></category>
		<category><![CDATA[AppRunningCount]]></category>
		<category><![CDATA[CreateToolhelp32Snapshot]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Process32First]]></category>
		<category><![CDATA[Process32Next]]></category>
		<category><![CDATA[TH32CS_SNAPPROCESS]]></category>
		<category><![CDATA[TProcessEntry32]]></category>
		<category><![CDATA[Usefull Function]]></category>

		<guid isPermaLink="false">http://projects.pro.br/gsaraiva/?p=470</guid>
		<description><![CDATA[Folks,
For everybody thats need to limit the instance number of your application:
uses SysUtils, TLHelp32;&#160;function AppRunningCount&#40;const AExeName: string&#41;: integer;var&#160;&#160;hSnap: THandle;&#160;&#160;p32: TProcessEntry32;begin&#160;&#160;Result := 0;&#160;&#160;hSnap := CreateToolHelp32SnapShot&#40;TH32CS_SNAPPROCESS, 0&#41;;&#160;&#160;p32.dwSize := Sizeof&#40;TProcessEntry32&#41;;&#160;&#160;Process32First&#40;hSnap, p32&#41;;&#160;&#160;repeat&#160;&#160;&#160;&#160;if SameText&#40;AExeName, ExtractFileName&#40;p32.szExeFile&#41;&#41; then&#160;&#160;&#160;&#160;&#160;&#160;inc&#40;Result&#41;;&#160;&#160;until not Process32Next&#40;hSnap, p32&#41;;&#160;&#160;CloseHandle&#40;hSnap&#41;;end;
Use:
// On DPR// Limit to 1 instancebegin &#160;&#160;if AppRunningCount&#40;ExtractFileName&#40;ParamStr&#40;0&#41;&#41;&#41; = 1 then&#160;&#160;begin&#160;&#160;// Application codes&#160;&#160;end;end.
Explaining more about this approach:
In a situation of multiples [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Folks,</strong></p>
<p>For everybody thats need to limit the instance number of your application:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Delphi"><div class="devcodeoverflow"><ol><li><span style="color: #000000; font-weight: bold;">uses</span> SysUtils<span style="color: #000066;">,</span> TLHelp32<span style="color: #000066;">;</span></li><li>&nbsp;</li><li><span style="color: #000000; font-weight: bold;">function</span> AppRunningCount<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> AExeName<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">string</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">integer</span><span style="color: #000066;">;</span></li><li><span style="color: #000000; font-weight: bold;">var</span></li><li>&nbsp;&nbsp;hSnap<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">THandle</span><span style="color: #000066;">;</span></li><li>&nbsp;&nbsp;p32<span style="color: #000066;">:</span> TProcessEntry32<span style="color: #000066;">;</span></li><li><span style="color: #000000; font-weight: bold;">begin</span></li><li>&nbsp;&nbsp;Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">;</span></li><li>&nbsp;&nbsp;hSnap <span style="color: #000066;">:</span><span style="color: #000066;">=</span> CreateToolHelp32SnapShot<span style="color: #000066;">&#40;</span>TH32CS_SNAPPROCESS<span style="color: #000066;">,</span> 0<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></li><li>&nbsp;&nbsp;p32<span style="color: #000066;">.</span><span style="color: #006600;">dwSize</span> <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000066;">Sizeof</span><span style="color: #000066;">&#40;</span>TProcessEntry32<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></li><li>&nbsp;&nbsp;Process32First<span style="color: #000066;">&#40;</span>hSnap<span style="color: #000066;">,</span> p32<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></li><li>&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">repeat</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #000066;">SameText</span><span style="color: #000066;">&#40;</span>AExeName<span style="color: #000066;">,</span> <span style="color: #000066;">ExtractFileName</span><span style="color: #000066;">&#40;</span>p32<span style="color: #000066;">.</span><span style="color: #006600;">szExeFile</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #000066;">inc</span><span style="color: #000066;">&#40;</span>Result<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></li><li>&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">until</span> <span style="color: #000000; font-weight: bold;">not</span> Process32Next<span style="color: #000066;">&#40;</span>hSnap<span style="color: #000066;">,</span> p32<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></li><li>&nbsp;&nbsp;CloseHandle<span style="color: #000066;">&#40;</span>hSnap<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></li><li><span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Use:<br />
<!--DEVFMTCODE--><pre class="devcodeblock" title="Delphi"><div class="devcodeoverflow"><ol><li><span style="color: #808080; font-style: italic;">// On DPR</span></li><li><span style="color: #808080; font-style: italic;">// Limit to 1 instance</span></li><li><span style="color: #000000; font-weight: bold;">begin</span> </li><li>&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">if</span> AppRunningCount<span style="color: #000066;">&#40;</span><span style="color: #000066;">ExtractFileName</span><span style="color: #000066;">&#40;</span><span style="color: #000066;">ParamStr</span><span style="color: #000066;">&#40;</span>0<span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">=</span> 1 <span style="color: #000000; font-weight: bold;">then</span></li><li>&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">begin</span></li><li>&nbsp;&nbsp;<span style="color: #808080; font-style: italic;">// Application codes</span></li><li>&nbsp;&nbsp;<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></li><li><span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">.</span></li><li></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Explaining more about this approach:<br />
In a situation of multiples logons at the same machine the Mutex cannot provide a correctly singleton structure because the mutex handle still only under the local scope of the user. So the way to control the instance number is counting the number of times the process appear on the process list of the machine.</p>
]]></content:encoded>
			<wfw:commentRss>http://projects.pro.br/gsaraiva/2009/07/delphi-utils-apprunningcount/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
