<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874541</guid>
        <pubDate>Thu, 25 Aug 2022 06:24:00 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874541</link>
        <description><![CDATA[SilverShield: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=429357&view=findpost&p=3874476'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Krid &#064; <time class="tag-quote__quoted-time" datetime="2022-08-23T19:22:38+03:00">23.08.22, 16:22</time></span><div class='quote '> во втором случае ты передаёшь в GetProcessImageFileName первым параметром PID, а надо handle. </div></div><br>
Благодарю&#33;  С дескриптором работает&#33;<br>
<br>
macomics, ЫукпШ, благодарю&#33;  Этот вариант тоже работает как надо&#33;<br>
<br>
Вопрос решен&#33;]]></description>
        <author>SilverShield</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874480</guid>
        <pubDate>Tue, 23 Aug 2022 18:21:34 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874480</link>
        <description><![CDATA[macomics: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=429357&view=findpost&p=3874479'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>ЫукпШ &#064; <time class="tag-quote__quoted-time" datetime="2022-08-23T18:15:59+00:00">23.08.22, 18:15</time></span><div class='quote '>Надо установить поле e.dwSize = sizeof( PROCESSENTRY32 )</div></div><br>
Спасибо, что уточнили. По памяти написал функцию - мой недочет.<br>
<br>
Рабочий вариант:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">function GetModuleNameByPID(PID: Integer): String;</div><div class="code_line">var</div><div class="code_line">&nbsp;&nbsp; h: THANDLE;</div><div class="code_line">&nbsp;&nbsp; e: TPROCESSENTRY32;</div><div class="code_line">begin</div><div class="code_line">&nbsp;&nbsp; Result := &#39;&#60;not found&#62;&#39;;</div><div class="code_line">&nbsp;&nbsp; h := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);</div><div class="code_line">&nbsp;&nbsp; if h &#60;&#62; INVALID_HANDLE_VALUE then</div><div class="code_line">&nbsp;&nbsp; begin</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;e.dwSize := SizeOf(TPROCESSENTRY32);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;if Process32First(h, e) then repeat</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if e.th32ProcessID = PID then begin</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := copy(e.szExeFile, Length(e.szExeFile));</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; end;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;until not Process32Next(h, e);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;CloseHandle(h);</div><div class="code_line">&nbsp;&nbsp; end;</div><div class="code_line">end;</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script>]]></description>
        <author>macomics</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874479</guid>
        <pubDate>Tue, 23 Aug 2022 18:15:59 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874479</link>
        <description><![CDATA[ЫукпШ: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=429357&view=findpost&p=3874443'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>SilverShield &#064; <time class="tag-quote__quoted-time" datetime="2022-08-23T09:54:28+00:00">23.08.22, 09:54</time></span><div class='quote '>if Process32First(h, e) then repeat<br>
Process32First возвращает false.</div></div><br>
Надо установить поле e.dwSize = sizeof( PROCESSENTRY32 )<br>
<a class='tag-url' href='https://docs.microsoft.com/en-us/windows/win32/toolhelp/taking-a-snapshot-and-viewing-processes' target='_blank'>Taking a Snapshot and Viewing Processes</a>]]></description>
        <author>ЫукпШ</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874476</guid>
        <pubDate>Tue, 23 Aug 2022 16:22:38 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874476</link>
        <description><![CDATA[Krid: <strong class='tag-b'>SilverShield</strong>, во втором случае ты передаёшь в GetProcessImageFileName первым параметром PID, а надо handle. Т.е. тебе нужно сначала открыть процесс (как в первом случае) и полученный дескриптор передать в эту ф-цию.<br>
Но в любом случае, чтобы получать имена всех (почти) процессов, твоя прога должна быть запущена с правами админа.]]></description>
        <author>Krid</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874450</guid>
        <pubDate>Tue, 23 Aug 2022 11:15:45 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874450</link>
        <description><![CDATA[macomics: У меня на Win 7 x64 этот способ отлично работал. Вы уверены, что у вас в системе нету программ, которые скрывают свое присутствие от taskmgr. Этим способом taskmgr получает список процессов. Он должен работать.]]></description>
        <author>macomics</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874447</guid>
        <pubDate>Tue, 23 Aug 2022 11:12:14 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874447</link>
        <description><![CDATA[SilverShield: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=429357&view=findpost&p=3874444'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>macomics &#064; <time class="tag-quote__quoted-time" datetime="2022-08-23T10:39:15+00:00">23.08.22, 10:39</time></span><div class='quote '></div></div><br>
к сожалению, аналогично. Уточню, что выполняю приложение на Вин 7 х64. Использование дальнейшее предполагается на Win 10-11]]></description>
        <author>SilverShield</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874444</guid>
        <pubDate>Tue, 23 Aug 2022 10:39:15 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874444</link>
        <description><![CDATA[macomics: Тогда попробуйте константу TH32CS_SNAPALL]]></description>
        <author>macomics</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874443</guid>
        <pubDate>Tue, 23 Aug 2022 09:54:28 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874443</link>
        <description><![CDATA[SilverShield: macomics, благодарю за ответ. Но к сожалению, этот код завершается на строке:<br>if Process32First(h, e) then repeat<br>Process32First возвращает false.]]></description>
        <author>SilverShield</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874433</guid>
        <pubDate>Tue, 23 Aug 2022 08:54:14 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874433</link>
        <description><![CDATA[macomics: Возможно стоит использовать вот этот способ<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">function GetModuleNameByPID(PID: Integer): String;</div><div class="code_line">var</div><div class="code_line">&nbsp;&nbsp; h: THANDLE;</div><div class="code_line">&nbsp;&nbsp; e: TPROCESSENTRY32;</div><div class="code_line">begin</div><div class="code_line">&nbsp;&nbsp; Result := &#39;&#60;not found&#62;&#39;;</div><div class="code_line">&nbsp;&nbsp; h := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);</div><div class="code_line">&nbsp;&nbsp; if h &#60;&#62; INVALID_HANDLE_VALUE then</div><div class="code_line">&nbsp;&nbsp; begin</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;if Process32First(h, e) then repeat</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if e.th32ProcessID = PID then begin</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := copy(e.szExeFile, Length(e.szExeFile));</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; end;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;until not Process32Next(h, e);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;CloseHandle(h);</div><div class="code_line">&nbsp;&nbsp; end;</div><div class="code_line">end;</div></ol></div></div></div></div>]]></description>
        <author>macomics</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874432</guid>
        <pubDate>Tue, 23 Aug 2022 08:41:07 +0000</pubDate>
        <title>Получение имени процесса по PID</title>
        <link>https://forum.sources.ru/index.php?showtopic=429357&amp;view=findpost&amp;p=3874432</link>
        <description><![CDATA[SilverShield: Доброго дня&#33; Коллеги, пытаюсь на Delphi XE3 и XE7 получить имя процесса по его PID.<br>
Нашел несколько вариантов как это сделать:<br>
1). сделать OpenProcess, а дальше GetModuleFileNameEx<br>
2). использовать GetProcessImageFileName<br>
<br>
Первый вариант работает для обычных приложений, но при попытке получить имя процесса браузера (Chrome, например), получаю ошибку:<br>
System error. Code: 299. Запрос ReadProcessMemory или WriteProcessMemory был выполнен только частично.<br>
<br>
Код как получаю:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">var</div><div class="code_line">&nbsp;&nbsp;AppPath: String;</div><div class="code_line">&nbsp;&nbsp;WinH: THandle;</div><div class="code_line">&nbsp;&nbsp;PrId: Cardinal;</div><div class="code_line">&nbsp;&nbsp;hProcess: THandle;</div><div class="code_line">&nbsp;&nbsp;path: array[0..MAX_PATH - 1] of char;</div><div class="code_line">&nbsp;&nbsp;cProcName:array[0..MAX_PATH] of Char;</div><div class="code_line">begin</div><div class="code_line">&nbsp;&nbsp;WinH:= GetForegroundWindow;</div><div class="code_line">&nbsp;&nbsp;GetWindowThreadProcessID(WinH, @PrId);</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp;hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, PrID);</div><div class="code_line">&nbsp;&nbsp;if hProcess &#60;&#62; 0 then</div><div class="code_line">&nbsp;&nbsp; &nbsp;try</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;if GetModuleFileNameEx(hProcess, 0, path, MAX_PATH) = 0 then</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;RaiseLastOSError;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;AppPath := path;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;Edit5.Text:= AppPath;</div><div class="code_line">&nbsp;&nbsp; &nbsp;finally</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;CloseHandle(hProcess)</div><div class="code_line">&nbsp;&nbsp; &nbsp;end</div><div class="code_line">&nbsp;&nbsp;else</div><div class="code_line">&nbsp;&nbsp; &nbsp;RaiseLastOSError;</div></ol></div></div></div></div><br>
<br>
Что касается второго способа, то для него пришлось объявить:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">function GetProcessImageFileName(hProcess:THandle; lpImageFileName:LPTSTR; nSize:DWORD):DWORD; stdcall;</div><div class="code_line">&nbsp;external &#39;PSAPI.dll&#39; name &#39;GetProcessImageFileNameW&#39;;</div></ol></div></div></div></div><br>
<br>
И использование:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">if GetProcessImageFileName(PrId,@cProcName,MAX_PATH) &#60;&#62; 0 then //SizeOf(cProcName));</div><div class="code_line">&nbsp;&nbsp;begin</div><div class="code_line">&nbsp;&nbsp; &nbsp;AppPath := cProcName;</div><div class="code_line">&nbsp;&nbsp; &nbsp;Edit6.Text:= AppPath;</div><div class="code_line">&nbsp;&nbsp;end else showmessage(SysErrorMessage(GetLastError()));</div></ol></div></div></div></div><br>
<br>
Но возвращает функция всегда 0. Сообщение об ошибке &quot;Неверный дескриптор&quot;.<br>
<br>
В обоих случаях не могу сообразить, как исправить положение ((<br>
<br>
Мне нужно починить любой из способов, который сможет выдавать имя процесса для любого из приложений, в том числе браузеров. Буду благодарен совету по сути вопроса.]]></description>
        <author>SilverShield</author>
        <category>Delphi: Система, Windows API</category>
      </item>
	
      </channel>
      </rss>
	