<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; WSH: как из скрипта определить его идентификатор процесса (PID)]]></title>
	<link rel="self" href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=2454&amp;type=atom" />
	<updated>2014-07-15T13:39:27Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.script-coding.com/viewtopic.php?id=2454</id>
		<entry>
			<title type="html"><![CDATA[Re: WSH: как из скрипта определить его идентификатор процесса (PID)]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=84790#p84790" />
			<content type="html"><![CDATA[<p>Ещё чуть-чуть оптимизировал код:</p><div class="codebox"><pre><code>
MsgBox GetCurrentProcessID()

Function GetCurrentProcessID()
    With GetObject(&quot;winmgmts:root\cimv2:win32_process.Handle=&#039;&quot; &amp;_
        CreateObject(&quot;WScript.Shell&quot;).Exec(&quot;rundll32 kernel32,Sleep&quot;).ProcessId &amp; &quot;&#039;&quot;)
        GetCurrentProcessID = .ParentProcessId
        .Terminate
    End With
End Function
</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Xameleon]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=8836</uri>
			</author>
			<updated>2014-07-15T13:39:27Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=84790#p84790</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: WSH: как из скрипта определить его идентификатор процесса (PID)]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=50284#p50284" />
			<content type="html"><![CDATA[<p>старый финт подобен первому методу <br /></p><div class="codebox"><pre><code>set test=GetObject(&quot;winmgmts:\\.\root\cimv2:win32_process.Handle=&#039;&quot; &amp; CreateObject(&quot;WScript.Shell&quot;).Exec(&quot;rundll32 kernel32,Sleep&quot;).ProcessId &amp; &quot;&#039;&quot;)
msgbox test.ParentProcessId
test.Terminate</code></pre></div>]]></content>
			<author>
				<name><![CDATA[smaharbA]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=2911</uri>
			</author>
			<updated>2011-07-30T11:05:48Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=50284#p50284</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: WSH: как из скрипта определить его идентификатор процесса (PID)]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=17770#p17770" />
			<content type="html"><![CDATA[<p>Немного ужатые в целях экономии <img src="//forum.script-coding.com/img/smilies/wink.png" width="15" height="15" /> варианты скрипта <strong>Poltergeyst</strong>&#039;a. Язык - JScript.</p><p>С выводом в диалоговое окно:<br /></p><div class="codebox"><pre><code>with(new ActiveXObject(&quot;DynamicWrapperX&quot;)) {
  Register(&quot;kernel32&quot;,&quot;GetCurrentProcessId&quot;,&quot;r=u&quot;)
  Register(&quot;kernel32&quot;,&quot;GetModuleHandle&quot;,&quot;i=u&quot;,&quot;r=h&quot;)
  Register(&quot;kernel32&quot;,&quot;GetModuleFileName&quot;,&quot;i=hSu&quot;)
  FileName = Space(255)
  PID = GetCurrentProcessId()
  hModule = GetModuleHandle(0)
  GetModuleFileName(hModule, FileName, 510)
}

WScript.Echo(&quot;PID: &quot; + PID + &quot;\nМодуль: &quot; + hModule + &quot;\nФайл модуля: &quot; + FileName)</code></pre></div><p>С выводом в консоль:<br /></p><div class="codebox"><pre><code>with(new ActiveXObject(&quot;DynamicWrapperX&quot;)) {
  Register(&quot;kernel32&quot;,&quot;GetCurrentProcessId&quot;,&quot;r=u&quot;)
  Register(&quot;kernel32&quot;,&quot;GetModuleHandle&quot;,&quot;i=u&quot;,&quot;r=h&quot;)
  Register(&quot;kernel32&quot;,&quot;GetModuleFileName&quot;,&quot;i=hSu&quot;)
  Register(&quot;msvcrt&quot;,&quot;system&quot;,&quot;i=s&quot;)
  FileName = Space(255)
  PID = GetCurrentProcessId()
  hModule = GetModuleHandle(0)
  GetModuleFileName(hModule, FileName, 510)
  system(&quot;Echo PID: &quot; + PID +
         &quot; &amp; Echo Модуль: &quot; + hModule + 
         &quot; &amp; Echo Файл модуля: &quot; + FileName +
         &quot; &amp; Echo.&quot; +
         &quot; &amp; Pause&quot;)
}</code></pre></div><p>В последнем примере использована функция system из msvcrt.dll. Переданную ей строку она отправляет командному интерпретатору (cmd.exe) и тот выполняет содержащиеся в ней команды.</p>]]></content>
			<author>
				<name><![CDATA[YMP]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=81</uri>
			</author>
			<updated>2009-01-02T09:28:38Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=17770#p17770</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: WSH: как из скрипта определить его идентификатор процесса (PID)]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=17755#p17755" />
			<content type="html"><![CDATA[<p>Получение характеристик текущего процесса с помощью Win32 API и <a href="http://www.script-coding.com/dynwrapx.html">DynamicWrapperX</a>, пример на JScript.<br /></p><div class="codebox"><pre><code>DW=new ActiveXObject(&quot;DynamicWrapperX&quot;)
DW.Register(&quot;KERNEL32.DLL&quot;,&quot;GetCurrentProcessId&quot;,&quot;f=s&quot;,&quot;r=l&quot;)
DW.Register(&quot;KERNEL32.DLL&quot;,&quot;GetModuleHandle&quot;,&quot;i=l&quot;,&quot;f=s&quot;,&quot;r=l&quot;)
DW.Register(&quot;KERNEL32.DLL&quot;,&quot;GetModuleFileNameA&quot;,&quot;i=lll&quot;,&quot;f=s&quot;,&quot;r=l&quot;)


//-------------------------------------------------------------
PID    =DW.GetCurrentProcessId()    //Идентификатор процесса
//-------------------------------------------------------------
hModule    =DW.GetModuleHandle(0)    //Основной модуль процесса
//-------------------------------------------------------------
sPtr    =DW.StrPtr(DW.Space(255),&quot;s&quot;)    //Имя файла модуля
hRes    =DW.GetModuleFileNameA(hModule,sPtr,255)
FileName=DW.StrGet(sPtr,&quot;s&quot;)
//------------------------------------------------------------
WScript.Echo(    &#039;[Идентификатор текущего процесса]    \n&#039;+
        PID+    &#039;                \n&#039;+
        &#039;[Модуль:]                 \n&#039;+
        hModule+&#039;                \n&#039;+
        &#039;[Файл модуля:]                \n&#039;+
        FileName)
WScript.Quit()
//------------------------------------------------------------</code></pre></div><p>Автор примера - <strong>Poltergeyst</strong>.</p>]]></content>
			<author>
				<name><![CDATA[The gray Cardinal]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=2</uri>
			</author>
			<updated>2009-01-01T12:43:58Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=17755#p17755</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[WSH: как из скрипта определить его идентификатор процесса (PID)]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=15930#p15930" />
			<content type="html"><![CDATA[<p>Из скрипта, посредством объекта WshScriptExec, запускается дочерний процесс «mshta.exe». Затем, посредством WMI, извлекается экземпляр Win32_Process объекта SWbemObjectEx запущенного дочернего процесса «mshta.exe», основываясь на полученном из объекта WshScriptExec PID этого дочернего процесса. После чего, из полученного экземпляра Win32_Process берётся родительский PID (.ParentProcessID), который и будет являться идентификатором процесса скрипта. Последним действием будет закрытие запущенного дочернего процесса через объект WshScriptExec.</p><p><strong><em>Замечание:</em></strong> приложение «mshta.exe» и его специальным образом подобранная командная строка позволяют создать скрытое окно, чтобы GUI окно (или окно консоли) дочернего процесса не отображалось на экране и не мешало работе.<br /></p><div class="codebox"><pre><code>Option Explicit

WScript.Echo GetScriptProcessID()

WScript.Quit 0
&#039;=============================================================================

&#039;=============================================================================
Function GetScriptProcessID()
    Dim objWshShell
    Dim objWshScriptExec
    Dim objSWbemObjectEx_ChildProcess
    
    Dim strPath
    
    
    strPath = &quot;&quot;&quot;mshta.exe&quot;&quot; &quot; &amp; _
        &quot;&quot;&quot;&lt;HTML&gt;&lt;HEAD&gt;&lt;HTA:APPLICATION APPLICATIONNAME=&#039;GetParentPID&#039; &quot; &amp; _
        &quot;WINDOWSTATE=&#039;minimize&#039; SHOWINTASKBAR=&#039;no&#039;&lt;/HEAD&gt;&lt;/HTML&gt;&quot;&quot;&quot;
    
    Set objWshShell                   = WScript.CreateObject(&quot;WScript.Shell&quot;)
    Set objWshScriptExec              = objWshShell.Exec(strPath)
    Set objSWbemObjectEx_ChildProcess = GetObject( _
        &quot;winmgmts:\\.\root\CIMV2:Win32_Process.Handle=&#039;&quot; &amp; CStr(objWshScriptExec.ProcessID) &amp;&quot;&#039;&quot;)
    
    GetScriptProcessID = objSWbemObjectEx_ChildProcess.ParentProcessID
    objWshScriptExec.Terminate
    
    Set objSWbemObjectEx_ChildProcess = Nothing
    Set objWshScriptExec              = Nothing
    Set objWshShell                   = Nothing
End Function
&#039;=============================================================================</code></pre></div><p>Отдельное спасибо коллеге <strong>Slav</strong> за ценное замечание.</p>]]></content>
			<author>
				<name><![CDATA[alexii]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=1844</uri>
			</author>
			<updated>2008-11-14T21:42:30Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=15930#p15930</id>
		</entry>
</feed>
